2023-06-12 05:07:15 +00:00
|
|
|
use std::{
|
|
|
|
error::Error,
|
|
|
|
io,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
use crossterm::{
|
2023-04-13 20:21:35 +00:00
|
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind},
|
2021-11-01 21:27:46 +00:00
|
|
|
execute,
|
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
|
|
};
|
2023-07-16 09:11:59 +00:00
|
|
|
use ratatui::{prelude::*, widgets::*};
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2021-11-11 14:56:37 +00:00
|
|
|
struct StatefulList<T> {
|
|
|
|
state: ListState,
|
|
|
|
items: Vec<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> StatefulList<T> {
|
|
|
|
fn with_items(items: Vec<T>) -> StatefulList<T> {
|
|
|
|
StatefulList {
|
|
|
|
state: ListState::default(),
|
|
|
|
items,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next(&mut self) {
|
|
|
|
let i = match self.state.selected() {
|
|
|
|
Some(i) => {
|
|
|
|
if i >= self.items.len() - 1 {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
i + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => 0,
|
|
|
|
};
|
|
|
|
self.state.select(Some(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn previous(&mut self) {
|
|
|
|
let i = match self.state.selected() {
|
|
|
|
Some(i) => {
|
|
|
|
if i == 0 {
|
|
|
|
self.items.len() - 1
|
|
|
|
} else {
|
|
|
|
i - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => 0,
|
|
|
|
};
|
|
|
|
self.state.select(Some(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unselect(&mut self) {
|
|
|
|
self.state.select(None);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-04 10:34:05 +00:00
|
|
|
/// This struct holds the current state of the app. In particular, it has the `items` field which is
|
|
|
|
/// a wrapper around `ListState`. Keeping track of the items state let us render the associated
|
|
|
|
/// widget with its state and have access to features such as natural scrolling.
|
2020-12-06 15:33:31 +00:00
|
|
|
///
|
|
|
|
/// 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.
|
2016-11-07 23:35:46 +00:00
|
|
|
struct App<'a> {
|
2020-05-10 13:44:30 +00:00
|
|
|
items: StatefulList<(&'a str, usize)>,
|
2016-11-07 23:35:46 +00:00
|
|
|
events: Vec<(&'a str, &'a str)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> App<'a> {
|
|
|
|
fn new() -> App<'a> {
|
|
|
|
App {
|
2019-12-15 20:38:18 +00:00
|
|
|
items: StatefulList::with_items(vec![
|
2020-05-10 13:44:30 +00:00
|
|
|
("Item0", 1),
|
|
|
|
("Item1", 2),
|
|
|
|
("Item2", 1),
|
|
|
|
("Item3", 3),
|
|
|
|
("Item4", 1),
|
|
|
|
("Item5", 4),
|
|
|
|
("Item6", 1),
|
|
|
|
("Item7", 3),
|
|
|
|
("Item8", 1),
|
|
|
|
("Item9", 6),
|
|
|
|
("Item10", 1),
|
|
|
|
("Item11", 3),
|
|
|
|
("Item12", 1),
|
|
|
|
("Item13", 2),
|
|
|
|
("Item14", 1),
|
|
|
|
("Item15", 1),
|
|
|
|
("Item16", 4),
|
|
|
|
("Item17", 1),
|
|
|
|
("Item18", 5),
|
|
|
|
("Item19", 4),
|
|
|
|
("Item20", 1),
|
|
|
|
("Item21", 2),
|
|
|
|
("Item22", 1),
|
|
|
|
("Item23", 3),
|
|
|
|
("Item24", 1),
|
2019-12-15 20:38:18 +00:00
|
|
|
]),
|
2017-09-11 05:58:37 +00:00
|
|
|
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"),
|
|
|
|
],
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-06 15:33:31 +00:00
|
|
|
/// Rotate through the event list.
|
|
|
|
/// This only exists to simulate some kind of "progress"
|
2021-11-01 21:27:46 +00:00
|
|
|
fn on_tick(&mut self) {
|
2020-12-06 15:33:31 +00:00
|
|
|
let event = self.events.remove(0);
|
|
|
|
self.events.push(event);
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 01:02:51 +00:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
2021-11-01 21:27:46 +00:00
|
|
|
// setup terminal
|
|
|
|
enable_raw_mode()?;
|
|
|
|
let mut stdout = io::stdout();
|
|
|
|
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
|
|
|
let backend = CrosstermBackend::new(stdout);
|
2018-09-23 18:59:51 +00:00
|
|
|
let mut terminal = Terminal::new(backend)?;
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
// create app and run it
|
|
|
|
let tick_rate = Duration::from_millis(250);
|
|
|
|
let app = App::new();
|
|
|
|
let res = run_app(&mut terminal, app, tick_rate);
|
|
|
|
|
|
|
|
// restore terminal
|
|
|
|
disable_raw_mode()?;
|
|
|
|
execute!(
|
|
|
|
terminal.backend_mut(),
|
|
|
|
LeaveAlternateScreen,
|
|
|
|
DisableMouseCapture
|
|
|
|
)?;
|
|
|
|
terminal.show_cursor()?;
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
if let Err(err) = res {
|
2023-05-22 03:46:02 +00:00
|
|
|
println!("{err:?}");
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
fn run_app<B: Backend>(
|
|
|
|
terminal: &mut Terminal<B>,
|
|
|
|
mut app: App,
|
|
|
|
tick_rate: Duration,
|
|
|
|
) -> io::Result<()> {
|
|
|
|
let mut last_tick = Instant::now();
|
2016-11-07 23:35:46 +00:00
|
|
|
loop {
|
2021-11-01 21:27:46 +00:00
|
|
|
terminal.draw(|f| ui(f, &mut app))?;
|
|
|
|
|
|
|
|
let timeout = tick_rate
|
|
|
|
.checked_sub(last_tick.elapsed())
|
|
|
|
.unwrap_or_else(|| Duration::from_secs(0));
|
|
|
|
if crossterm::event::poll(timeout)? {
|
|
|
|
if let Event::Key(key) = event::read()? {
|
2023-04-13 20:21:35 +00:00
|
|
|
if key.kind == KeyEventKind::Press {
|
|
|
|
match key.code {
|
|
|
|
KeyCode::Char('q') => return Ok(()),
|
|
|
|
KeyCode::Left => app.items.unselect(),
|
|
|
|
KeyCode::Down => app.items.next(),
|
|
|
|
KeyCode::Up => app.items.previous(),
|
|
|
|
_ => {}
|
|
|
|
}
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
if last_tick.elapsed() >= tick_rate {
|
|
|
|
app.on_tick();
|
|
|
|
last_tick = Instant::now();
|
|
|
|
}
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
|
|
|
|
// Create two chunks with equal horizontal screen space
|
|
|
|
let chunks = Layout::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
|
|
|
.split(f.size());
|
|
|
|
|
|
|
|
// Iterate through all elements in the `items` app and append some debug text to it.
|
|
|
|
let items: Vec<ListItem> = app
|
|
|
|
.items
|
|
|
|
.items
|
|
|
|
.iter()
|
|
|
|
.map(|i| {
|
2023-05-18 18:21:43 +00:00
|
|
|
let mut lines = vec![Line::from(i.0)];
|
2021-11-01 21:27:46 +00:00
|
|
|
for _ in 0..i.1 {
|
2023-07-01 09:14:16 +00:00
|
|
|
lines.push(
|
|
|
|
"Lorem ipsum dolor sit amet, consectetur adipiscing elit."
|
|
|
|
.italic()
|
|
|
|
.into(),
|
|
|
|
);
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
|
|
|
ListItem::new(lines).style(Style::default().fg(Color::Black).bg(Color::White))
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Create a List from all list items and highlight the currently selected one
|
|
|
|
let items = List::new(items)
|
|
|
|
.block(Block::default().borders(Borders::ALL).title("List"))
|
|
|
|
.highlight_style(
|
|
|
|
Style::default()
|
|
|
|
.bg(Color::LightGreen)
|
|
|
|
.add_modifier(Modifier::BOLD),
|
|
|
|
)
|
|
|
|
.highlight_symbol(">> ");
|
|
|
|
|
|
|
|
// We can now render the item list
|
|
|
|
f.render_stateful_widget(items, chunks[0], &mut app.items.state);
|
|
|
|
|
|
|
|
// Let's do the same for the events.
|
|
|
|
// The event list doesn't have any state and only displays the current state of the list.
|
|
|
|
let events: Vec<ListItem> = app
|
|
|
|
.events
|
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
.map(|&(event, level)| {
|
|
|
|
// Colorcode the level depending on its type
|
|
|
|
let s = match level {
|
|
|
|
"CRITICAL" => Style::default().fg(Color::Red),
|
|
|
|
"ERROR" => Style::default().fg(Color::Magenta),
|
|
|
|
"WARNING" => Style::default().fg(Color::Yellow),
|
|
|
|
"INFO" => Style::default().fg(Color::Blue),
|
|
|
|
_ => Style::default(),
|
|
|
|
};
|
|
|
|
// Add a example datetime and apply proper spacing between them
|
2023-05-18 18:21:43 +00:00
|
|
|
let header = Line::from(vec![
|
2023-05-22 03:46:02 +00:00
|
|
|
Span::styled(format!("{level:<9}"), s),
|
2023-07-01 09:14:16 +00:00
|
|
|
" ".into(),
|
|
|
|
"2020-01-01 10:00:00".italic(),
|
2021-11-01 21:27:46 +00:00
|
|
|
]);
|
|
|
|
// The event gets its own line
|
2023-07-01 09:14:16 +00:00
|
|
|
let log = Line::from(vec![event.into()]);
|
2021-11-01 21:27:46 +00:00
|
|
|
|
|
|
|
// Here several things happen:
|
|
|
|
// 1. Add a `---` spacing line above the final list entry
|
|
|
|
// 2. Add the Level + datetime
|
|
|
|
// 3. Add a spacer line
|
|
|
|
// 4. Add the actual event
|
|
|
|
ListItem::new(vec![
|
2023-05-18 18:21:43 +00:00
|
|
|
Line::from("-".repeat(chunks[1].width as usize)),
|
2021-11-01 21:27:46 +00:00
|
|
|
header,
|
2023-05-18 18:21:43 +00:00
|
|
|
Line::from(""),
|
2021-11-01 21:27:46 +00:00
|
|
|
log,
|
|
|
|
])
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
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]);
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|