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
|
|
|
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
use termion::event;
|
|
|
|
use termion::input::TermRead;
|
|
|
|
|
2016-11-28 08:52:51 +00:00
|
|
|
use tui::Terminal;
|
2017-09-03 13:34:05 +00:00
|
|
|
use tui::backend::MouseBackend;
|
2017-12-26 16:09:04 +00:00
|
|
|
use tui::widgets::{Block, Borders, Row, Table, Widget};
|
2017-10-30 21:28:37 +00:00
|
|
|
use tui::layout::{Direction, Group, Rect, Size};
|
|
|
|
use tui::style::{Color, Modifier, Style};
|
2016-11-08 09:19:28 +00:00
|
|
|
|
|
|
|
struct App<'a> {
|
2017-05-08 19:34:41 +00:00
|
|
|
size: Rect,
|
2016-11-08 09:19:28 +00:00
|
|
|
items: Vec<Vec<&'a str>>,
|
|
|
|
selected: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> App<'a> {
|
|
|
|
fn new() -> App<'a> {
|
|
|
|
App {
|
2017-05-08 19:34:41 +00:00
|
|
|
size: Rect::default(),
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// Terminal initialization
|
2017-09-03 13:34:05 +00:00
|
|
|
let backend = MouseBackend::new().unwrap();
|
2016-11-08 09:19:28 +00:00
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
|
|
|
|
// App
|
|
|
|
let mut app = App::new();
|
|
|
|
|
|
|
|
// First draw call
|
|
|
|
terminal.clear().unwrap();
|
|
|
|
terminal.hide_cursor().unwrap();
|
2017-05-08 19:34:41 +00:00
|
|
|
app.size = terminal.size().unwrap();
|
2016-11-08 09:19:28 +00:00
|
|
|
draw(&mut terminal, &app);
|
|
|
|
|
|
|
|
// Input
|
|
|
|
let stdin = io::stdin();
|
|
|
|
for c in stdin.keys() {
|
2017-05-08 19:34:41 +00:00
|
|
|
let size = terminal.size().unwrap();
|
|
|
|
if size != app.size {
|
|
|
|
terminal.resize(size).unwrap();
|
|
|
|
app.size = size;
|
|
|
|
}
|
|
|
|
|
2016-11-08 09:19:28 +00:00
|
|
|
let evt = c.unwrap();
|
|
|
|
match evt {
|
|
|
|
event::Key::Char('q') => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
event::Key::Down => {
|
|
|
|
app.selected += 1;
|
|
|
|
if app.selected > app.items.len() - 1 {
|
|
|
|
app.selected = 0;
|
|
|
|
}
|
|
|
|
}
|
2017-10-30 21:28:37 +00:00
|
|
|
event::Key::Up => if app.selected > 0 {
|
|
|
|
app.selected -= 1;
|
|
|
|
} else {
|
|
|
|
app.selected = app.items.len() - 1;
|
|
|
|
},
|
2016-11-08 09:19:28 +00:00
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
draw(&mut terminal, &app);
|
|
|
|
}
|
|
|
|
|
|
|
|
terminal.show_cursor().unwrap();
|
2017-09-10 21:02:56 +00:00
|
|
|
terminal.clear().unwrap();
|
2016-11-08 09:19:28 +00:00
|
|
|
}
|
|
|
|
|
2017-09-03 13:34:05 +00:00
|
|
|
fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
|
2018-05-06 08:16:26 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
});
|
2016-11-08 09:19:28 +00:00
|
|
|
Group::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.sizes(&[Size::Percent(100)])
|
|
|
|
.margin(5)
|
2017-05-08 19:34:41 +00:00
|
|
|
.render(t, &app.size, |t, chunks| {
|
2018-05-06 08:16:26 +00:00
|
|
|
Table::new(header.into_iter(), rows)
|
|
|
|
.block(Block::default().borders(Borders::ALL).title("Table"))
|
2016-11-08 09:19:28 +00:00
|
|
|
.widths(&[10, 10, 10])
|
|
|
|
.render(t, &chunks[0]);
|
|
|
|
});
|
|
|
|
|
|
|
|
t.draw().unwrap();
|
|
|
|
}
|