Add widget hello world example
This commit is contained in:
parent
f793ec79ac
commit
6a0c0474ec
1 changed files with 51 additions and 0 deletions
51
examples/hello_world_widgets.rs
Normal file
51
examples/hello_world_widgets.rs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
use std::convert::Infallible;
|
||||
|
||||
use crossterm::event::Event;
|
||||
use crossterm::style::{ContentStyle, Stylize};
|
||||
use toss::widgets::Text;
|
||||
use toss::{Styled, Terminal, Widget};
|
||||
|
||||
fn widget() -> impl Widget<Infallible> {
|
||||
Text::new(
|
||||
Styled::new("Hello world!", ContentStyle::default().green())
|
||||
.then_plain("\n")
|
||||
.then(
|
||||
"Press any key to exit",
|
||||
ContentStyle::default().on_dark_blue(),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
widget().draw(term.frame()).unwrap();
|
||||
term.present().unwrap();
|
||||
|
||||
if term.measuring_required() {
|
||||
term.measure_widths().unwrap();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Automatically enters alternate screen and enables raw mode
|
||||
let mut term = Terminal::new().unwrap();
|
||||
term.set_measuring(true);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue