2018-09-23 18:59:51 +00:00
|
|
|
extern crate failure;
|
2016-11-08 09:19:28 +00:00
|
|
|
extern crate termion;
|
2017-10-30 21:28:37 +00:00
|
|
|
extern crate tui;
|
2016-11-08 09:19:28 +00:00
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
mod util;
|
2016-11-08 09:19:28 +00:00
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
use std::io;
|
2016-11-08 09:19:28 +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, Layout};
|
2017-10-30 21:28:37 +00:00
|
|
|
use tui::style::{Color, Modifier, Style};
|
2018-05-06 10:59:24 +00:00
|
|
|
use tui::widgets::{Block, Borders, Row, Table, Widget};
|
2018-06-09 09:02:37 +00:00
|
|
|
use tui::Terminal;
|
2016-11-08 09:19:28 +00:00
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
use util::event::{Event, Events};
|
|
|
|
|
2016-11-08 09:19:28 +00:00
|
|
|
struct App<'a> {
|
|
|
|
items: Vec<Vec<&'a str>>,
|
|
|
|
selected: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> App<'a> {
|
|
|
|
fn new() -> App<'a> {
|
|
|
|
App {
|
2017-09-10 21:02:56 +00:00
|
|
|
items: vec![
|
|
|
|
vec!["Row12", "Row12", "Row13"],
|
|
|
|
vec!["Row21", "Row22", "Row23"],
|
|
|
|
vec!["Row31", "Row32", "Row33"],
|
|
|
|
vec!["Row41", "Row42", "Row43"],
|
|
|
|
vec!["Row51", "Row52", "Row53"],
|
|
|
|
vec!["Row61", "Row62", "Row63"],
|
|
|
|
],
|
2016-11-08 09:19:28 +00:00
|
|
|
selected: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
fn main() -> Result<(), failure::Error> {
|
2016-11-08 09:19:28 +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()?;
|
|
|
|
|
|
|
|
let events = Events::new();
|
2016-11-08 09:19:28 +00:00
|
|
|
|
|
|
|
// App
|
|
|
|
let mut app = App::new();
|
|
|
|
|
|
|
|
// Input
|
2018-09-23 18:59:51 +00:00
|
|
|
loop {
|
|
|
|
terminal.draw(|mut f| {
|
|
|
|
let selected_style = Style::default().fg(Color::Yellow).modifier(Modifier::Bold);
|
|
|
|
let normal_style = Style::default().fg(Color::White);
|
|
|
|
let header = ["Header1", "Header2", "Header3"];
|
|
|
|
let rows = app.items.iter().enumerate().map(|(i, item)| {
|
|
|
|
if i == app.selected {
|
|
|
|
Row::StyledData(item.into_iter(), selected_style)
|
|
|
|
} else {
|
|
|
|
Row::StyledData(item.into_iter(), normal_style)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let rects = Layout::default()
|
|
|
|
.constraints([Constraint::Percentage(100)].as_ref())
|
|
|
|
.margin(5)
|
2018-12-07 14:43:05 +00:00
|
|
|
.split(f.size());
|
2018-09-23 18:59:51 +00:00
|
|
|
Table::new(header.into_iter(), rows)
|
|
|
|
.block(Block::default().borders(Borders::ALL).title("Table"))
|
|
|
|
.widths(&[10, 10, 10])
|
|
|
|
.render(&mut f, rects[0]);
|
|
|
|
})?;
|
|
|
|
|
|
|
|
match events.next()? {
|
|
|
|
Event::Input(key) => match key {
|
|
|
|
Key::Char('q') => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Key::Down => {
|
|
|
|
app.selected += 1;
|
|
|
|
if app.selected > app.items.len() - 1 {
|
|
|
|
app.selected = 0;
|
|
|
|
}
|
2016-11-08 09:19:28 +00:00
|
|
|
}
|
2018-12-07 15:17:33 +00:00
|
|
|
Key::Up => {
|
|
|
|
if app.selected > 0 {
|
|
|
|
app.selected -= 1;
|
|
|
|
} else {
|
|
|
|
app.selected = app.items.len() - 1;
|
|
|
|
}
|
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
_ => {}
|
2017-10-30 21:28:37 +00:00
|
|
|
},
|
2016-11-08 09:19:28 +00:00
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
Ok(())
|
2016-11-08 09:19:28 +00:00
|
|
|
}
|