toss/examples/hello_world.rs
Joscha 8fae7d2bf1 Return Redraw enum instead of bool
Boolean values were too easy to accidentally interpret the wrong way.
2022-05-29 13:18:25 +02:00

48 lines
1.2 KiB
Rust

use crossterm::event::Event;
use crossterm::style::{ContentStyle, Stylize};
use toss::frame::{Frame, Pos};
use toss::terminal::{Redraw, Terminal};
fn draw(f: &mut Frame) {
f.write(
Pos::new(0, 0),
"Hello world!",
ContentStyle::default().green(),
);
f.write(
Pos::new(0, 1),
"Press any key to exit",
ContentStyle::default().on_dark_blue(),
);
f.show_cursor(Pos::new(16, 0));
}
fn render_frame(term: &mut Terminal) {
loop {
// Must be called before rendering, otherwise the terminal has out-of-date
// size information and will present garbage.
term.autoresize().unwrap();
draw(term.frame());
if term.present().unwrap() == Redraw::NotRequired {
break;
}
}
}
fn main() {
// Automatically enters alternate screen and enables raw mode
let mut term = Terminal::new().unwrap();
loop {
// Render and display a frame. A full frame is displayed on the terminal
// once this function exits.
render_frame(&mut term);
// Exit if the user presses any buttons
if !matches!(crossterm::event::read().unwrap(), Event::Resize(_, _)) {
break;
}
}
}