mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-22 12:43:16 +00:00
Add example for implementing a custom widget
This commit is contained in:
parent
a5b632aeb0
commit
d42dfaa710
1 changed files with 41 additions and 0 deletions
41
examples/custom_widget.rs
Normal file
41
examples/custom_widget.rs
Normal file
|
@ -0,0 +1,41 @@
|
|||
extern crate tui;
|
||||
|
||||
use tui::Terminal;
|
||||
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() {
|
||||
let mut terminal = Terminal::new().unwrap();
|
||||
terminal.clear();
|
||||
Label::default().text("Test").render(&Terminal::size().unwrap(), &mut terminal);
|
||||
terminal.finish();
|
||||
}
|
Loading…
Reference in a new issue