ratatui/examples/custom_widget.rs

40 lines
882 B
Rust
Raw Normal View History

extern crate tui;
2017-09-03 13:34:05 +00:00
use tui::backend::MouseBackend;
use tui::buffer::Buffer;
use tui::layout::Rect;
2016-11-06 20:41:32 +00:00
use tui::style::Style;
2018-05-06 10:59:24 +00:00
use tui::widgets::Widget;
2018-06-09 09:02:37 +00:00
use tui::Terminal;
struct Label<'a> {
text: &'a str,
}
impl<'a> Default for Label<'a> {
fn default() -> Label<'a> {
Label { text: "" }
}
}
impl<'a> Widget for Label<'a> {
2017-09-03 10:16:34 +00:00
fn draw(&mut self, area: &Rect, buf: &mut Buffer) {
2016-11-06 20:41:32 +00:00
buf.set_string(area.left(), area.top(), self.text, &Style::default());
}
}
impl<'a> Label<'a> {
fn text(&mut self, text: &'a str) -> &mut Label<'a> {
self.text = text;
self
}
}
fn main() {
2017-09-03 13:34:05 +00:00
let mut terminal = Terminal::new(MouseBackend::new().unwrap()).unwrap();
2016-11-06 17:49:57 +00:00
let size = terminal.size().unwrap();
2016-11-03 22:59:04 +00:00
terminal.clear().unwrap();
Label::default().text("Test").render(&mut terminal.get_frame(), &size);
2016-11-03 22:59:04 +00:00
terminal.draw().unwrap();
}