A simple TUI Library written in Rust
1use std::thread;
2use std::time::Duration;
3
4use sly::block::{Block, BorderStyle};
5use sly::style::{Span, Style};
6use sly::terminal;
7use sly::view::View;
8
9fn make_block(width: usize) -> Block {
10 let inner = Block::text(vec![
11 vec![Span::styled(
12 format!("Terminal width: {width} columns"),
13 Style::new().bold(),
14 )],
15 vec![Span::plain("Press Ctrl+C to exit.")],
16 ]);
17 Block::boxed(inner, BorderStyle::Rounded, None)
18}
19
20fn main() {
21 terminal::install_resize_handler();
22
23 let mut width = terminal::get_size()
24 .map(|(cols, _)| cols as usize)
25 .unwrap_or(80);
26
27 let mut view = View::create(&make_block(width), width);
28
29 loop {
30 thread::sleep(Duration::from_millis(50));
31 if terminal::take_resize() {
32 let new_width = terminal::get_size()
33 .map(|(cols, _)| cols as usize)
34 .unwrap_or(width);
35 width = new_width;
36 view.resize(&make_block(width), width);
37 }
38 }
39}