2018-09-23 18:59:51 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
mod util;
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2019-12-15 20:38:18 +00:00
|
|
|
use crate::util::{
|
|
|
|
event::{Event, Events},
|
|
|
|
StatefulList,
|
|
|
|
};
|
2020-03-13 01:02:51 +00:00
|
|
|
use std::{error::Error, io};
|
|
|
|
use termion::{event::Key, input::MouseTerminal, raw::IntoRawMode, screen::AlternateScreen};
|
|
|
|
use tui::{
|
|
|
|
backend::TermionBackend,
|
|
|
|
layout::{Constraint, Corner, Direction, Layout},
|
2020-07-11 17:08:28 +00:00
|
|
|
style::{Color, Modifier, Style},
|
2020-05-10 13:44:30 +00:00
|
|
|
text::{Span, Spans},
|
|
|
|
widgets::{Block, Borders, List, ListItem},
|
2020-03-13 01:02:51 +00:00
|
|
|
Terminal,
|
|
|
|
};
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2020-12-06 15:33:31 +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.
|
|
|
|
///
|
|
|
|
/// 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"
|
2016-11-07 23:35:46 +00:00
|
|
|
fn advance(&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>> {
|
2016-11-07 23:35:46 +00:00
|
|
|
// Terminal initialization
|
2018-09-23 18:59:51 +00:00
|
|
|
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)?;
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
let events = Events::new();
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2021-06-16 15:14:16 +00:00
|
|
|
// Create a new app with some example state
|
2016-11-07 23:35:46 +00:00
|
|
|
let mut app = App::new();
|
|
|
|
|
|
|
|
loop {
|
2020-06-15 20:57:23 +00:00
|
|
|
terminal.draw(|f| {
|
2020-12-06 15:33:31 +00:00
|
|
|
// Create two chunks with equal horizontal screen space
|
2018-09-23 18:59:51 +00:00
|
|
|
let chunks = Layout::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
2018-12-07 14:43:05 +00:00
|
|
|
.split(f.size());
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2020-12-06 15:33:31 +00:00
|
|
|
// Iterate through all elements in the `items` app and append some debug text to it.
|
2020-05-10 13:44:30 +00:00
|
|
|
let items: Vec<ListItem> = app
|
|
|
|
.items
|
|
|
|
.items
|
|
|
|
.iter()
|
|
|
|
.map(|i| {
|
|
|
|
let mut lines = vec![Spans::from(i.0)];
|
|
|
|
for _ in 0..i.1 {
|
|
|
|
lines.push(Spans::from(Span::styled(
|
|
|
|
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
|
2020-07-11 17:08:28 +00:00
|
|
|
Style::default().add_modifier(Modifier::ITALIC),
|
2020-05-10 13:44:30 +00:00
|
|
|
)));
|
|
|
|
}
|
2020-07-11 17:08:28 +00:00
|
|
|
ListItem::new(lines).style(Style::default().fg(Color::Black).bg(Color::White))
|
2020-05-10 13:44:30 +00:00
|
|
|
})
|
|
|
|
.collect();
|
2020-12-06 15:33:31 +00:00
|
|
|
|
|
|
|
// Create a List from all list items and highlight the currently selected one
|
2019-12-15 20:38:18 +00:00
|
|
|
let items = List::new(items)
|
2018-09-23 18:59:51 +00:00
|
|
|
.block(Block::default().borders(Borders::ALL).title("List"))
|
2020-07-11 17:08:28 +00:00
|
|
|
.highlight_style(
|
|
|
|
Style::default()
|
|
|
|
.bg(Color::LightGreen)
|
2020-05-10 13:44:30 +00:00
|
|
|
.add_modifier(Modifier::BOLD),
|
|
|
|
)
|
|
|
|
.highlight_symbol(">> ");
|
2020-12-06 15:33:31 +00:00
|
|
|
|
|
|
|
// We can now render the item list
|
2019-12-15 20:38:18 +00:00
|
|
|
f.render_stateful_widget(items, chunks[0], &mut app.items.state);
|
|
|
|
|
2020-12-06 15:33:31 +00:00
|
|
|
// 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.
|
2020-05-10 13:44:30 +00:00
|
|
|
let events: Vec<ListItem> = app
|
|
|
|
.events
|
|
|
|
.iter()
|
2020-12-06 15:33:31 +00:00
|
|
|
.rev()
|
|
|
|
.map(|&(event, level)| {
|
|
|
|
// Colorcode the level depending on its type
|
2020-05-10 13:44:30 +00:00
|
|
|
let s = match level {
|
2020-07-11 17:08:28 +00:00
|
|
|
"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(),
|
2020-05-10 13:44:30 +00:00
|
|
|
};
|
2020-12-06 15:33:31 +00:00
|
|
|
// Add a example datetime and apply proper spacing between them
|
2020-05-10 13:44:30 +00:00
|
|
|
let header = Spans::from(vec![
|
|
|
|
Span::styled(format!("{:<9}", level), s),
|
|
|
|
Span::raw(" "),
|
|
|
|
Span::styled(
|
|
|
|
"2020-01-01 10:00:00",
|
2020-07-11 17:08:28 +00:00
|
|
|
Style::default().add_modifier(Modifier::ITALIC),
|
2020-05-10 13:44:30 +00:00
|
|
|
),
|
|
|
|
]);
|
2021-06-16 15:14:16 +00:00
|
|
|
// The event gets its own line
|
2020-12-06 15:33:31 +00:00
|
|
|
let log = Spans::from(vec![Span::raw(event)]);
|
|
|
|
|
|
|
|
// 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
|
2020-05-10 13:44:30 +00:00
|
|
|
ListItem::new(vec![
|
|
|
|
Spans::from("-".repeat(chunks[1].width as usize)),
|
|
|
|
header,
|
|
|
|
Spans::from(""),
|
|
|
|
log,
|
|
|
|
])
|
|
|
|
})
|
|
|
|
.collect();
|
2019-12-15 20:38:18 +00:00
|
|
|
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]);
|
2018-09-23 18:59:51 +00:00
|
|
|
})?;
|
|
|
|
|
2020-12-06 15:33:31 +00:00
|
|
|
// This is a simple example on how to handle events
|
|
|
|
// 1. This breaks the loop and exits the program on `q` button press.
|
|
|
|
// 2. The `up`/`down` keys change the currently selected item in the App's `items` list.
|
|
|
|
// 3. `left` unselects the current item.
|
2018-09-23 18:59:51 +00:00
|
|
|
match events.next()? {
|
2017-10-30 21:28:37 +00:00
|
|
|
Event::Input(input) => match input {
|
2018-09-23 18:59:51 +00:00
|
|
|
Key::Char('q') => {
|
2017-10-30 21:28:37 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
Key::Left => {
|
2019-12-15 20:38:18 +00:00
|
|
|
app.items.unselect();
|
2018-09-04 19:45:03 +00:00
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
Key::Down => {
|
2019-12-15 20:38:18 +00:00
|
|
|
app.items.next();
|
2018-09-04 19:45:03 +00:00
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
Key::Up => {
|
2019-12-15 20:38:18 +00:00
|
|
|
app.items.previous();
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
2017-10-30 21:28:37 +00:00
|
|
|
_ => {}
|
|
|
|
},
|
2016-11-07 23:35:46 +00:00
|
|
|
Event::Tick => {
|
|
|
|
app.advance();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
Ok(())
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|