2018-09-23 18:59:51 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
mod util;
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
use std::io;
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
use termion::event::Key;
|
|
|
|
use termion::input::MouseTerminal;
|
|
|
|
use termion::raw::IntoRawMode;
|
|
|
|
use termion::screen::AlternateScreen;
|
|
|
|
use tui::backend::TermionBackend;
|
2018-11-26 02:06:34 +00:00
|
|
|
use tui::layout::{Constraint, Corner, Direction, Layout};
|
2017-10-30 21:28:37 +00:00
|
|
|
use tui::style::{Color, Modifier, Style};
|
2018-09-08 06:03:30 +00:00
|
|
|
use tui::widgets::{Block, Borders, List, SelectableList, Text, Widget};
|
2018-06-09 09:02:37 +00:00
|
|
|
use tui::Terminal;
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2019-01-06 11:57:06 +00:00
|
|
|
use crate::util::event::{Event, Events};
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2016-11-07 23:35:46 +00:00
|
|
|
struct App<'a> {
|
|
|
|
items: Vec<&'a str>,
|
2018-09-04 19:45:03 +00:00
|
|
|
selected: Option<usize>,
|
2016-11-07 23:35:46 +00:00
|
|
|
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 {
|
2017-09-11 05:58:37 +00:00
|
|
|
items: vec![
|
2017-12-26 11:05:47 +00:00
|
|
|
"Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item8", "Item9",
|
|
|
|
"Item10", "Item11", "Item12", "Item13", "Item14", "Item15", "Item16", "Item17",
|
|
|
|
"Item18", "Item19", "Item20", "Item21", "Item22", "Item23", "Item24",
|
2017-09-11 05:58:37 +00:00
|
|
|
],
|
2018-09-04 19:45:03 +00:00
|
|
|
selected: None,
|
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
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
fn main() -> Result<(), failure::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)?;
|
|
|
|
terminal.hide_cursor()?;
|
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
|
|
|
|
|
|
|
// App
|
|
|
|
let mut app = App::new();
|
|
|
|
|
|
|
|
loop {
|
2018-09-23 18:59:51 +00:00
|
|
|
terminal.draw(|mut f| {
|
|
|
|
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
|
|
|
|
|
|
|
let style = Style::default().fg(Color::Black).bg(Color::White);
|
|
|
|
SelectableList::default()
|
|
|
|
.block(Block::default().borders(Borders::ALL).title("List"))
|
|
|
|
.items(&app.items)
|
|
|
|
.select(app.selected)
|
|
|
|
.style(style)
|
|
|
|
.highlight_style(style.fg(Color::LightGreen).modifier(Modifier::Bold))
|
|
|
|
.highlight_symbol(">")
|
|
|
|
.render(&mut f, chunks[0]);
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
});
|
|
|
|
List::new(events)
|
|
|
|
.block(Block::default().borders(Borders::ALL).title("List"))
|
|
|
|
.start_corner(Corner::BottomLeft)
|
|
|
|
.render(&mut f, chunks[1]);
|
|
|
|
}
|
|
|
|
})?;
|
|
|
|
|
|
|
|
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 => {
|
2018-09-04 19:45:03 +00:00
|
|
|
app.selected = None;
|
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
Key::Down => {
|
2018-09-04 19:45:03 +00:00
|
|
|
app.selected = if let Some(selected) = app.selected {
|
|
|
|
if selected >= app.items.len() - 1 {
|
|
|
|
Some(0)
|
|
|
|
} else {
|
|
|
|
Some(selected + 1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Some(0)
|
|
|
|
}
|
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
Key::Up => {
|
2018-09-04 19:45:03 +00:00
|
|
|
app.selected = if let Some(selected) = app.selected {
|
|
|
|
if selected > 0 {
|
|
|
|
Some(selected - 1)
|
|
|
|
} else {
|
|
|
|
Some(app.items.len() - 1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Some(0)
|
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
|
|
|
}
|