ratatui/examples/custom_widget.rs

43 lines
955 B
Rust
Raw Normal View History

extern crate tui;
2016-11-06 17:49:57 +00:00
use tui::{Terminal, TermionBackend};
use tui::widgets::Widget;
use tui::buffer::Buffer;
use tui::layout::Rect;
use tui::style::Color;
struct Label<'a> {
text: &'a str,
}
impl<'a> Default for Label<'a> {
fn default() -> Label<'a> {
Label { text: "" }
}
}
impl<'a> Widget for Label<'a> {
fn draw(&self, area: &Rect, buf: &mut Buffer) {
buf.set_string(area.left(),
area.top(),
self.text,
Color::Reset,
Color::Reset);
}
}
impl<'a> Label<'a> {
fn text(&mut self, text: &'a str) -> &mut Label<'a> {
self.text = text;
self
}
}
fn main() {
2016-11-06 17:49:57 +00:00
let mut terminal = Terminal::new(TermionBackend::new().unwrap()).unwrap();
let size = terminal.size().unwrap();
2016-11-03 22:59:04 +00:00
terminal.clear().unwrap();
2016-11-06 17:49:57 +00:00
Label::default().text("Test").render(&mut terminal, &size);
2016-11-03 22:59:04 +00:00
terminal.draw().unwrap();
}