2018-09-23 18:59:51 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
mod util;
|
2016-11-08 09:19:28 +00:00
|
|
|
|
2019-01-06 11:57:06 +00:00
|
|
|
use crate::util::event::{Event, Events};
|
2020-03-13 01:02:51 +00:00
|
|
|
use std::{error::Error, io};
|
2020-03-12 23:30:37 +00:00
|
|
|
use termion::{event::Key, input::MouseTerminal, raw::IntoRawMode, screen::AlternateScreen};
|
|
|
|
use tui::{
|
|
|
|
backend::TermionBackend,
|
|
|
|
layout::{Constraint, Layout},
|
|
|
|
style::{Color, Modifier, Style},
|
2020-11-15 21:47:00 +00:00
|
|
|
widgets::{Block, Borders, Cell, Row, Table, TableState},
|
2020-03-12 23:30:37 +00:00
|
|
|
Terminal,
|
|
|
|
};
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2020-03-12 23:30:37 +00:00
|
|
|
pub struct StatefulTable<'a> {
|
|
|
|
state: TableState,
|
2016-11-08 09:19:28 +00:00
|
|
|
items: Vec<Vec<&'a str>>,
|
|
|
|
}
|
|
|
|
|
2020-03-12 23:30:37 +00:00
|
|
|
impl<'a> StatefulTable<'a> {
|
|
|
|
fn new() -> StatefulTable<'a> {
|
|
|
|
StatefulTable {
|
|
|
|
state: TableState::default(),
|
2017-09-10 21:02:56 +00:00
|
|
|
items: vec![
|
2019-07-09 14:05:17 +00:00
|
|
|
vec!["Row11", "Row12", "Row13"],
|
2017-09-10 21:02:56 +00:00
|
|
|
vec!["Row21", "Row22", "Row23"],
|
|
|
|
vec!["Row31", "Row32", "Row33"],
|
|
|
|
vec!["Row41", "Row42", "Row43"],
|
|
|
|
vec!["Row51", "Row52", "Row53"],
|
2020-11-15 21:47:00 +00:00
|
|
|
vec!["Row61", "Row62\nTest", "Row63"],
|
2020-03-12 23:30:37 +00:00
|
|
|
vec!["Row71", "Row72", "Row73"],
|
|
|
|
vec!["Row81", "Row82", "Row83"],
|
|
|
|
vec!["Row91", "Row92", "Row93"],
|
|
|
|
vec!["Row101", "Row102", "Row103"],
|
|
|
|
vec!["Row111", "Row112", "Row113"],
|
|
|
|
vec!["Row121", "Row122", "Row123"],
|
|
|
|
vec!["Row131", "Row132", "Row133"],
|
|
|
|
vec!["Row141", "Row142", "Row143"],
|
|
|
|
vec!["Row151", "Row152", "Row153"],
|
|
|
|
vec!["Row161", "Row162", "Row163"],
|
|
|
|
vec!["Row171", "Row172", "Row173"],
|
|
|
|
vec!["Row181", "Row182", "Row183"],
|
|
|
|
vec!["Row191", "Row192", "Row193"],
|
2017-09-10 21:02:56 +00:00
|
|
|
],
|
2016-11-08 09:19:28 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-12 23:30:37 +00:00
|
|
|
pub 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));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub 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));
|
|
|
|
}
|
2016-11-08 09:19:28 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 01:02:51 +00:00
|
|
|
fn main() -> Result<(), Box<dyn 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)?;
|
|
|
|
|
|
|
|
let events = Events::new();
|
2016-11-08 09:19:28 +00:00
|
|
|
|
2020-03-12 23:30:37 +00:00
|
|
|
let mut table = StatefulTable::new();
|
2016-11-08 09:19:28 +00:00
|
|
|
|
|
|
|
// Input
|
2018-09-23 18:59:51 +00:00
|
|
|
loop {
|
2020-06-15 20:57:23 +00:00
|
|
|
terminal.draw(|f| {
|
2018-09-23 18:59:51 +00:00
|
|
|
let rects = Layout::default()
|
|
|
|
.constraints([Constraint::Percentage(100)].as_ref())
|
|
|
|
.margin(5)
|
2018-12-07 14:43:05 +00:00
|
|
|
.split(f.size());
|
2019-12-15 20:38:18 +00:00
|
|
|
|
2020-11-15 21:47:00 +00:00
|
|
|
let selected_style = Style::default().add_modifier(Modifier::REVERSED);
|
|
|
|
let normal_style = Style::default().bg(Color::Blue);
|
|
|
|
let header_cells = ["Header1", "Header2", "Header3"]
|
2020-03-12 23:30:37 +00:00
|
|
|
.iter()
|
2020-11-15 21:47:00 +00:00
|
|
|
.map(|h| Cell::from(*h).style(Style::default().fg(Color::Red)));
|
|
|
|
let header = Row::new(header_cells)
|
|
|
|
.style(normal_style)
|
|
|
|
.height(1)
|
|
|
|
.bottom_margin(1);
|
|
|
|
let rows = table.items.iter().map(|item| {
|
|
|
|
let height = item
|
|
|
|
.iter()
|
|
|
|
.map(|content| content.chars().filter(|c| *c == '\n').count())
|
|
|
|
.max()
|
|
|
|
.unwrap_or(0)
|
|
|
|
+ 1;
|
|
|
|
let cells = item.iter().map(|c| Cell::from(*c));
|
|
|
|
Row::new(cells).height(height as u16).bottom_margin(1)
|
|
|
|
});
|
|
|
|
let t = Table::new(rows)
|
|
|
|
.header(header)
|
2018-09-23 18:59:51 +00:00
|
|
|
.block(Block::default().borders(Borders::ALL).title("Table"))
|
2020-03-12 23:30:37 +00:00
|
|
|
.highlight_style(selected_style)
|
|
|
|
.highlight_symbol(">> ")
|
2019-07-10 10:39:10 +00:00
|
|
|
.widths(&[
|
|
|
|
Constraint::Percentage(50),
|
|
|
|
Constraint::Length(30),
|
|
|
|
Constraint::Max(10),
|
2019-12-15 20:38:18 +00:00
|
|
|
]);
|
2020-03-12 23:30:37 +00:00
|
|
|
f.render_stateful_widget(t, rects[0], &mut table.state);
|
2018-09-23 18:59:51 +00:00
|
|
|
})?;
|
|
|
|
|
2020-05-16 22:51:57 +00:00
|
|
|
if let Event::Input(key) = events.next()? {
|
|
|
|
match key {
|
2018-09-23 18:59:51 +00:00
|
|
|
Key::Char('q') => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Key::Down => {
|
2020-03-12 23:30:37 +00:00
|
|
|
table.next();
|
2016-11-08 09:19:28 +00:00
|
|
|
}
|
2018-12-07 15:17:33 +00:00
|
|
|
Key::Up => {
|
2020-03-12 23:30:37 +00:00
|
|
|
table.previous();
|
2018-12-07 15:17:33 +00:00
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
_ => {}
|
2020-05-16 22:51:57 +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
|
|
|
}
|