2024-01-24 19:50:18 +00:00
|
|
|
//! # [Ratatui] List example
|
|
|
|
//!
|
|
|
|
//! The latest version of this example is available in the [examples] folder in the repository.
|
|
|
|
//!
|
|
|
|
//! Please note that the examples are designed to be run against the `main` branch of the Github
|
|
|
|
//! repository. This means that you may not be able to compile with the latest release version on
|
|
|
|
//! crates.io, or the one that you have installed locally.
|
|
|
|
//!
|
|
|
|
//! See the [examples readme] for more information on finding examples that match the version of the
|
|
|
|
//! library you are using.
|
|
|
|
//!
|
|
|
|
//! [Ratatui]: https://github.com/ratatui-org/ratatui
|
|
|
|
//! [examples]: https://github.com/ratatui-org/ratatui/blob/main/examples
|
|
|
|
//! [examples readme]: https://github.com/ratatui-org/ratatui/blob/main/examples/README.md
|
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
use std::{error::Error, io};
|
2023-06-12 05:07:15 +00:00
|
|
|
|
2024-05-28 20:23:39 +00:00
|
|
|
use ratatui::{
|
2024-06-24 08:37:22 +00:00
|
|
|
backend::Backend,
|
2024-05-29 11:42:29 +00:00
|
|
|
buffer::Buffer,
|
2024-08-11 00:43:13 +00:00
|
|
|
crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind},
|
2024-06-24 08:37:22 +00:00
|
|
|
layout::{Constraint, Layout, Rect},
|
|
|
|
style::{
|
|
|
|
palette::tailwind::{BLUE, GREEN, SLATE},
|
|
|
|
Color, Modifier, Style, Stylize,
|
2024-05-28 20:23:39 +00:00
|
|
|
},
|
2024-06-24 08:37:22 +00:00
|
|
|
symbols,
|
2024-05-29 11:42:29 +00:00
|
|
|
text::Line,
|
|
|
|
widgets::{
|
|
|
|
Block, Borders, HighlightSpacing, List, ListItem, ListState, Padding, Paragraph,
|
|
|
|
StatefulWidget, Widget, Wrap,
|
|
|
|
},
|
2024-08-02 11:18:00 +00:00
|
|
|
Terminal,
|
2021-11-01 21:27:46 +00:00
|
|
|
};
|
2024-01-23 16:22:37 +00:00
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
const TODO_HEADER_STYLE: Style = Style::new().fg(SLATE.c100).bg(BLUE.c800);
|
|
|
|
const NORMAL_ROW_BG: Color = SLATE.c950;
|
|
|
|
const ALT_ROW_BG_COLOR: Color = SLATE.c900;
|
|
|
|
const SELECTED_STYLE: Style = Style::new().bg(SLATE.c800).add_modifier(Modifier::BOLD);
|
|
|
|
const TEXT_FG_COLOR: Color = SLATE.c200;
|
|
|
|
const COMPLETED_TEXT_FG_COLOR: Color = GREEN.c500;
|
2024-01-23 16:22:37 +00:00
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
|
|
tui::init_error_hooks()?;
|
|
|
|
let terminal = tui::init_terminal()?;
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
let mut app = App::default();
|
|
|
|
app.run(terminal)?;
|
|
|
|
|
|
|
|
tui::restore_terminal()?;
|
|
|
|
Ok(())
|
2024-01-23 16:22:37 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
/// This struct holds the current state of the app. In particular, it has the `todo_list` field
|
|
|
|
/// which is a wrapper around `ListState`. Keeping track of the state lets us render the
|
|
|
|
/// associated widget with its state and have access to features such as natural scrolling.
|
|
|
|
///
|
|
|
|
/// Check the event handling at the bottom to see how to change the state on incoming events. Check
|
|
|
|
/// the drawing logic for items on how to specify the highlighting style for selected items.
|
|
|
|
struct App {
|
|
|
|
should_exit: bool,
|
|
|
|
todo_list: TodoList,
|
2024-05-24 22:09:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct TodoList {
|
|
|
|
items: Vec<TodoItem>,
|
2024-06-24 08:37:22 +00:00
|
|
|
state: ListState,
|
2021-11-11 14:56:37 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct TodoItem {
|
|
|
|
todo: String,
|
|
|
|
info: String,
|
|
|
|
status: Status,
|
2024-01-23 16:22:37 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
enum Status {
|
|
|
|
Todo,
|
|
|
|
Completed,
|
2024-01-23 16:22:37 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
impl Default for App {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
should_exit: false,
|
|
|
|
todo_list: TodoList::from_iter([
|
|
|
|
(Status::Todo, "Rewrite everything with Rust!", "I can't hold my inner voice. He tells me to rewrite the complete universe with Rust"),
|
|
|
|
(Status::Completed, "Rewrite all of your tui apps with Ratatui", "Yes, you heard that right. Go and replace your tui with Ratatui."),
|
|
|
|
(Status::Todo, "Pet your cat", "Minnak loves to be pet by you! Don't forget to pet and give some treats!"),
|
|
|
|
(Status::Todo, "Walk with your dog", "Max is bored, go walk with him!"),
|
|
|
|
(Status::Completed, "Pay the bills", "Pay the train subscription!!!"),
|
|
|
|
(Status::Completed, "Refactor list example", "If you see this info that means I completed this task!"),
|
|
|
|
]),
|
|
|
|
}
|
|
|
|
}
|
2024-01-23 16:22:37 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
impl FromIterator<(Status, &'static str, &'static str)> for TodoList {
|
|
|
|
fn from_iter<I: IntoIterator<Item = (Status, &'static str, &'static str)>>(iter: I) -> Self {
|
|
|
|
let items = iter
|
|
|
|
.into_iter()
|
|
|
|
.map(|(status, todo, info)| TodoItem::new(status, todo, info))
|
|
|
|
.collect();
|
|
|
|
let state = ListState::default();
|
|
|
|
Self { items, state }
|
|
|
|
}
|
2024-01-23 16:22:37 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
impl TodoItem {
|
|
|
|
fn new(status: Status, todo: &str, info: &str) -> Self {
|
|
|
|
Self {
|
|
|
|
status,
|
|
|
|
todo: todo.to_string(),
|
|
|
|
info: info.to_string(),
|
|
|
|
}
|
|
|
|
}
|
2024-01-23 16:22:37 +00:00
|
|
|
}
|
|
|
|
|
2024-05-24 22:09:24 +00:00
|
|
|
impl App {
|
2024-06-24 08:37:22 +00:00
|
|
|
fn run(&mut self, mut terminal: Terminal<impl Backend>) -> io::Result<()> {
|
|
|
|
while !self.should_exit {
|
2024-08-06 03:15:14 +00:00
|
|
|
terminal.draw(|f| f.render_widget(&mut *self, f.area()))?;
|
2024-06-24 08:37:22 +00:00
|
|
|
if let Event::Key(key) = event::read()? {
|
|
|
|
self.handle_key(key);
|
|
|
|
};
|
2024-01-23 16:22:37 +00:00
|
|
|
}
|
2024-06-24 08:37:22 +00:00
|
|
|
Ok(())
|
2024-01-23 16:22:37 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
fn handle_key(&mut self, key: KeyEvent) {
|
|
|
|
if key.kind != KeyEventKind::Press {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
match key.code {
|
|
|
|
KeyCode::Char('q') | KeyCode::Esc => self.should_exit = true,
|
|
|
|
KeyCode::Char('h') | KeyCode::Left => self.select_none(),
|
|
|
|
KeyCode::Char('j') | KeyCode::Down => self.select_next(),
|
|
|
|
KeyCode::Char('k') | KeyCode::Up => self.select_previous(),
|
|
|
|
KeyCode::Char('g') | KeyCode::Home => self.select_first(),
|
|
|
|
KeyCode::Char('G') | KeyCode::End => self.select_last(),
|
|
|
|
KeyCode::Char('l') | KeyCode::Right | KeyCode::Enter => {
|
|
|
|
self.toggle_status();
|
2024-01-23 16:22:37 +00:00
|
|
|
}
|
2024-06-24 08:37:22 +00:00
|
|
|
_ => {}
|
2024-01-23 16:22:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
fn select_none(&mut self) {
|
|
|
|
self.todo_list.state.select(None);
|
2024-01-23 16:22:37 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
fn select_next(&mut self) {
|
|
|
|
self.todo_list.state.select_next();
|
|
|
|
}
|
|
|
|
fn select_previous(&mut self) {
|
|
|
|
self.todo_list.state.select_previous();
|
2024-01-23 16:22:37 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
fn select_first(&mut self) {
|
|
|
|
self.todo_list.state.select_first();
|
|
|
|
}
|
2024-01-23 16:22:37 +00:00
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
fn select_last(&mut self) {
|
|
|
|
self.todo_list.state.select_last();
|
2024-01-23 16:22:37 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
/// Changes the status of the selected list item
|
|
|
|
fn toggle_status(&mut self) {
|
|
|
|
if let Some(i) = self.todo_list.state.selected() {
|
|
|
|
self.todo_list.items[i].status = match self.todo_list.items[i].status {
|
|
|
|
Status::Completed => Status::Todo,
|
|
|
|
Status::Todo => Status::Completed,
|
|
|
|
}
|
|
|
|
}
|
2024-01-23 16:22:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-24 22:09:24 +00:00
|
|
|
impl Widget for &mut App {
|
2024-01-23 16:22:37 +00:00
|
|
|
fn render(self, area: Rect, buf: &mut Buffer) {
|
2024-06-24 08:37:22 +00:00
|
|
|
let [header_area, main_area, footer_area] = Layout::vertical([
|
2024-01-23 16:22:37 +00:00
|
|
|
Constraint::Length(2),
|
2024-06-24 08:37:22 +00:00
|
|
|
Constraint::Fill(1),
|
|
|
|
Constraint::Length(1),
|
|
|
|
])
|
|
|
|
.areas(area);
|
|
|
|
|
|
|
|
let [list_area, item_area] =
|
|
|
|
Layout::vertical([Constraint::Fill(1), Constraint::Fill(1)]).areas(main_area);
|
|
|
|
|
|
|
|
App::render_header(header_area, buf);
|
|
|
|
App::render_footer(footer_area, buf);
|
|
|
|
self.render_list(list_area, buf);
|
|
|
|
self.render_selected_item(item_area, buf);
|
2024-01-23 16:22:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
/// Rendering logic for the app
|
2024-05-24 22:09:24 +00:00
|
|
|
impl App {
|
2024-06-24 08:37:22 +00:00
|
|
|
fn render_header(area: Rect, buf: &mut Buffer) {
|
|
|
|
Paragraph::new("Ratatui List Example")
|
|
|
|
.bold()
|
|
|
|
.centered()
|
|
|
|
.render(area, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_footer(area: Rect, buf: &mut Buffer) {
|
|
|
|
Paragraph::new("Use ↓↑ to move, ← to unselect, → to change status, g/G to go top/bottom.")
|
|
|
|
.centered()
|
|
|
|
.render(area, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_list(&mut self, area: Rect, buf: &mut Buffer) {
|
|
|
|
let block = Block::new()
|
|
|
|
.title(Line::raw("TODO List").centered())
|
|
|
|
.borders(Borders::TOP)
|
|
|
|
.border_set(symbols::border::EMPTY)
|
|
|
|
.border_style(TODO_HEADER_STYLE)
|
|
|
|
.bg(NORMAL_ROW_BG);
|
2024-01-23 16:22:37 +00:00
|
|
|
|
|
|
|
// Iterate through all elements in the `items` and stylize them.
|
|
|
|
let items: Vec<ListItem> = self
|
2024-06-24 08:37:22 +00:00
|
|
|
.todo_list
|
2024-01-23 16:22:37 +00:00
|
|
|
.items
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2024-06-24 08:37:22 +00:00
|
|
|
.map(|(i, todo_item)| {
|
|
|
|
let color = alternate_colors(i);
|
|
|
|
ListItem::from(todo_item).bg(color)
|
|
|
|
})
|
2024-01-23 16:22:37 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Create a List from all list items and highlight the currently selected one
|
2024-06-24 08:37:22 +00:00
|
|
|
let list = List::new(items)
|
|
|
|
.block(block)
|
|
|
|
.highlight_style(SELECTED_STYLE)
|
2024-01-23 16:22:37 +00:00
|
|
|
.highlight_symbol(">")
|
|
|
|
.highlight_spacing(HighlightSpacing::Always);
|
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
// We need to disambiguate this trait method as both `Widget` and `StatefulWidget` share the
|
|
|
|
// same method name `render`.
|
|
|
|
StatefulWidget::render(list, area, buf, &mut self.todo_list.state);
|
2024-01-23 16:22:37 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
fn render_selected_item(&self, area: Rect, buf: &mut Buffer) {
|
2024-01-23 16:22:37 +00:00
|
|
|
// We get the info depending on the item's state.
|
2024-06-24 08:37:22 +00:00
|
|
|
let info = if let Some(i) = self.todo_list.state.selected() {
|
|
|
|
match self.todo_list.items[i].status {
|
|
|
|
Status::Completed => format!("✓ DONE: {}", self.todo_list.items[i].info),
|
|
|
|
Status::Todo => format!("☐ TODO: {}", self.todo_list.items[i].info),
|
2024-01-23 16:22:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-06-24 08:37:22 +00:00
|
|
|
"Nothing selected...".to_string()
|
2024-01-23 16:22:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// We show the list item's info under the list in this paragraph
|
2024-06-24 08:37:22 +00:00
|
|
|
let block = Block::new()
|
|
|
|
.title(Line::raw("TODO Info").centered())
|
|
|
|
.borders(Borders::TOP)
|
|
|
|
.border_set(symbols::border::EMPTY)
|
|
|
|
.border_style(TODO_HEADER_STYLE)
|
|
|
|
.bg(NORMAL_ROW_BG)
|
|
|
|
.padding(Padding::horizontal(1));
|
2024-01-23 16:22:37 +00:00
|
|
|
|
|
|
|
// We can now render the item info
|
2024-06-24 08:37:22 +00:00
|
|
|
Paragraph::new(info)
|
|
|
|
.block(block)
|
|
|
|
.fg(TEXT_FG_COLOR)
|
|
|
|
.wrap(Wrap { trim: false })
|
|
|
|
.render(area, buf);
|
2024-01-23 16:22:37 +00:00
|
|
|
}
|
2024-03-02 09:06:53 +00:00
|
|
|
}
|
2024-01-23 16:22:37 +00:00
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
const fn alternate_colors(i: usize) -> Color {
|
|
|
|
if i % 2 == 0 {
|
|
|
|
NORMAL_ROW_BG
|
|
|
|
} else {
|
|
|
|
ALT_ROW_BG_COLOR
|
2021-11-11 14:56:37 +00:00
|
|
|
}
|
2024-06-24 08:37:22 +00:00
|
|
|
}
|
2021-11-11 14:56:37 +00:00
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
impl From<&TodoItem> for ListItem<'_> {
|
|
|
|
fn from(value: &TodoItem) -> Self {
|
|
|
|
let line = match value.status {
|
|
|
|
Status::Todo => Line::styled(format!(" ☐ {}", value.todo), TEXT_FG_COLOR),
|
|
|
|
Status::Completed => {
|
|
|
|
Line::styled(format!(" ✓ {}", value.todo), COMPLETED_TEXT_FG_COLOR)
|
2021-11-11 14:56:37 +00:00
|
|
|
}
|
|
|
|
};
|
2024-06-24 08:37:22 +00:00
|
|
|
ListItem::new(line)
|
2021-11-11 14:56:37 +00:00
|
|
|
}
|
2024-06-24 08:37:22 +00:00
|
|
|
}
|
2021-11-11 14:56:37 +00:00
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
mod tui {
|
|
|
|
use std::{io, io::stdout};
|
|
|
|
|
|
|
|
use color_eyre::config::HookBuilder;
|
|
|
|
use ratatui::{
|
|
|
|
backend::{Backend, CrosstermBackend},
|
|
|
|
crossterm::{
|
|
|
|
terminal::{
|
|
|
|
disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
|
|
|
|
},
|
|
|
|
ExecutableCommand,
|
|
|
|
},
|
2024-08-02 11:18:00 +00:00
|
|
|
Terminal,
|
2024-06-24 08:37:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pub fn init_error_hooks() -> color_eyre::Result<()> {
|
|
|
|
let (panic, error) = HookBuilder::default().into_hooks();
|
|
|
|
let panic = panic.into_panic_hook();
|
|
|
|
let error = error.into_eyre_hook();
|
|
|
|
color_eyre::eyre::set_hook(Box::new(move |e| {
|
|
|
|
let _ = restore_terminal();
|
|
|
|
error(e)
|
|
|
|
}))?;
|
|
|
|
std::panic::set_hook(Box::new(move |info| {
|
|
|
|
let _ = restore_terminal();
|
|
|
|
panic(info);
|
|
|
|
}));
|
|
|
|
Ok(())
|
2021-11-11 14:56:37 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
pub fn init_terminal() -> io::Result<Terminal<impl Backend>> {
|
|
|
|
stdout().execute(EnterAlternateScreen)?;
|
|
|
|
enable_raw_mode()?;
|
|
|
|
Terminal::new(CrosstermBackend::new(stdout()))
|
2021-11-11 14:56:37 +00:00
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
|
2024-06-24 08:37:22 +00:00
|
|
|
pub fn restore_terminal() -> io::Result<()> {
|
|
|
|
stdout().execute(LeaveAlternateScreen)?;
|
|
|
|
disable_raw_mode()
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
|
|
|
}
|