mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-10 07:04:17 +00:00
6cb57f5d2a
Most widgets can be drawn directly based on the input parameters. However, some features may require some kind of associated state to be implemented. For example, the `List` widget can highlight the item currently selected. This can be translated in an offset, which is the number of elements to skip in order to have the selected item within the viewport currently allocated to this widget. The widget can therefore only provide the following behavior: whenever the selected item is out of the viewport scroll to a predefined position (make the selected item the last viewable item or the one in the middle). Nonetheless, if the widget has access to the last computed offset then it can implement a natural scrolling experience where the last offset is reused until the selected item is out of the viewport. To allow such behavior within the widgets, this commit introduces the following changes: - Add a `StatefulWidget` trait with an associated `State` type. Widgets that can take advantage of having a "memory" between two draw calls needs to implement this trait. - Add a `render_stateful_widget` method on `Frame` where the associated state is given as a parameter. The chosen approach is thus to let the developers manage their widgets' states themselves as they are already responsible for the lifecycle of the wigets (given that the crate exposes an immediate mode api). The following changes were also introduced: - `Widget::render` has been deleted. Developers should use `Frame::render_widget` instead. - `Widget::background` has been deleted. Developers should use `Buffer::set_background` instead. - `SelectableList` has been deleted. Developers can directly use `List` where `SelectableList` features have been back-ported.
150 lines
4.8 KiB
Rust
150 lines
4.8 KiB
Rust
#[allow(dead_code)]
|
|
mod util;
|
|
|
|
use std::io;
|
|
|
|
use termion::event::Key;
|
|
use termion::input::MouseTerminal;
|
|
use termion::raw::IntoRawMode;
|
|
use termion::screen::AlternateScreen;
|
|
use tui::backend::TermionBackend;
|
|
use tui::layout::{Constraint, Corner, Direction, Layout};
|
|
use tui::style::{Color, Modifier, Style};
|
|
use tui::widgets::{Block, Borders, List, Text};
|
|
use tui::Terminal;
|
|
|
|
use crate::util::{
|
|
event::{Event, Events},
|
|
StatefulList,
|
|
};
|
|
|
|
struct App<'a> {
|
|
items: StatefulList<&'a str>,
|
|
events: Vec<(&'a str, &'a str)>,
|
|
info_style: Style,
|
|
warning_style: Style,
|
|
error_style: Style,
|
|
critical_style: Style,
|
|
}
|
|
|
|
impl<'a> App<'a> {
|
|
fn new() -> App<'a> {
|
|
App {
|
|
items: StatefulList::with_items(vec![
|
|
"Item0", "Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item8",
|
|
"Item9", "Item10", "Item11", "Item12", "Item13", "Item14", "Item15", "Item16",
|
|
"Item17", "Item18", "Item19", "Item20", "Item21", "Item22", "Item23", "Item24",
|
|
]),
|
|
events: vec![
|
|
("Event1", "INFO"),
|
|
("Event2", "INFO"),
|
|
("Event3", "CRITICAL"),
|
|
("Event4", "ERROR"),
|
|
("Event5", "INFO"),
|
|
("Event6", "INFO"),
|
|
("Event7", "WARNING"),
|
|
("Event8", "INFO"),
|
|
("Event9", "INFO"),
|
|
("Event10", "INFO"),
|
|
("Event11", "CRITICAL"),
|
|
("Event12", "INFO"),
|
|
("Event13", "INFO"),
|
|
("Event14", "INFO"),
|
|
("Event15", "INFO"),
|
|
("Event16", "INFO"),
|
|
("Event17", "ERROR"),
|
|
("Event18", "ERROR"),
|
|
("Event19", "INFO"),
|
|
("Event20", "INFO"),
|
|
("Event21", "WARNING"),
|
|
("Event22", "INFO"),
|
|
("Event23", "INFO"),
|
|
("Event24", "WARNING"),
|
|
("Event25", "INFO"),
|
|
("Event26", "INFO"),
|
|
],
|
|
info_style: Style::default().fg(Color::White),
|
|
warning_style: Style::default().fg(Color::Yellow),
|
|
error_style: Style::default().fg(Color::Magenta),
|
|
critical_style: Style::default().fg(Color::Red),
|
|
}
|
|
}
|
|
|
|
fn advance(&mut self) {
|
|
let event = self.events.pop().unwrap();
|
|
self.events.insert(0, event);
|
|
}
|
|
}
|
|
|
|
fn main() -> Result<(), failure::Error> {
|
|
// Terminal initialization
|
|
let stdout = io::stdout().into_raw_mode()?;
|
|
let stdout = MouseTerminal::from(stdout);
|
|
let stdout = AlternateScreen::from(stdout);
|
|
let backend = TermionBackend::new(stdout);
|
|
let mut terminal = Terminal::new(backend)?;
|
|
terminal.hide_cursor()?;
|
|
|
|
let events = Events::new();
|
|
|
|
// App
|
|
let mut app = App::new();
|
|
|
|
loop {
|
|
terminal.draw(|mut f| {
|
|
let chunks = Layout::default()
|
|
.direction(Direction::Horizontal)
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
|
.split(f.size());
|
|
|
|
let style = Style::default().fg(Color::Black).bg(Color::White);
|
|
|
|
let items = app.items.items.iter().map(|i| Text::raw(*i));
|
|
let items = List::new(items)
|
|
.block(Block::default().borders(Borders::ALL).title("List"))
|
|
.style(style)
|
|
.highlight_style(style.fg(Color::LightGreen).modifier(Modifier::BOLD))
|
|
.highlight_symbol(">");
|
|
f.render_stateful_widget(items, chunks[0], &mut app.items.state);
|
|
|
|
let events = app.events.iter().map(|&(evt, level)| {
|
|
Text::styled(
|
|
format!("{}: {}", level, evt),
|
|
match level {
|
|
"ERROR" => app.error_style,
|
|
"CRITICAL" => app.critical_style,
|
|
"WARNING" => app.warning_style,
|
|
_ => app.info_style,
|
|
},
|
|
)
|
|
});
|
|
let events_list = List::new(events)
|
|
.block(Block::default().borders(Borders::ALL).title("List"))
|
|
.start_corner(Corner::BottomLeft);
|
|
f.render_widget(events_list, chunks[1]);
|
|
})?;
|
|
|
|
match events.next()? {
|
|
Event::Input(input) => match input {
|
|
Key::Char('q') => {
|
|
break;
|
|
}
|
|
Key::Left => {
|
|
app.items.unselect();
|
|
}
|
|
Key::Down => {
|
|
app.items.next();
|
|
}
|
|
Key::Up => {
|
|
app.items.previous();
|
|
}
|
|
_ => {}
|
|
},
|
|
Event::Tick => {
|
|
app.advance();
|
|
}
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|