2016-11-07 23:35:46 +00:00
|
|
|
extern crate termion;
|
2017-10-30 21:28:37 +00:00
|
|
|
extern crate tui;
|
2016-11-07 23:35:46 +00:00
|
|
|
|
|
|
|
use std::io;
|
2018-05-06 10:59:24 +00:00
|
|
|
use std::sync::mpsc;
|
2016-11-07 23:35:46 +00:00
|
|
|
use std::thread;
|
|
|
|
use std::time;
|
|
|
|
|
|
|
|
use termion::event;
|
|
|
|
use termion::input::TermRead;
|
|
|
|
|
2017-09-03 13:34:05 +00:00
|
|
|
use tui::backend::MouseBackend;
|
2018-08-12 17:44:52 +00:00
|
|
|
use tui::layout::{Constraint, Direction, Layout, Rect};
|
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::{BarChart, Block, Borders, Widget};
|
2018-06-09 09:02:37 +00:00
|
|
|
use tui::Terminal;
|
2016-11-07 23:35:46 +00:00
|
|
|
|
|
|
|
struct App<'a> {
|
2017-05-08 19:34:41 +00:00
|
|
|
size: Rect,
|
2016-11-07 23:35:46 +00:00
|
|
|
data: Vec<(&'a str, u64)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> App<'a> {
|
|
|
|
fn new() -> App<'a> {
|
|
|
|
App {
|
2017-05-08 19:34:41 +00:00
|
|
|
size: Rect::default(),
|
2017-09-11 05:58:37 +00:00
|
|
|
data: vec![
|
|
|
|
("B1", 9),
|
|
|
|
("B2", 12),
|
|
|
|
("B3", 5),
|
|
|
|
("B4", 8),
|
|
|
|
("B5", 2),
|
|
|
|
("B6", 4),
|
|
|
|
("B7", 5),
|
|
|
|
("B8", 9),
|
|
|
|
("B9", 14),
|
|
|
|
("B10", 15),
|
|
|
|
("B11", 1),
|
|
|
|
("B12", 0),
|
|
|
|
("B13", 4),
|
|
|
|
("B14", 6),
|
|
|
|
("B15", 4),
|
|
|
|
("B16", 6),
|
|
|
|
("B17", 4),
|
|
|
|
("B18", 7),
|
|
|
|
("B19", 13),
|
|
|
|
("B20", 8),
|
|
|
|
("B21", 11),
|
|
|
|
("B22", 9),
|
|
|
|
("B23", 3),
|
|
|
|
("B24", 5),
|
|
|
|
],
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn advance(&mut self) {
|
|
|
|
let value = self.data.pop().unwrap();
|
|
|
|
self.data.insert(0, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Event {
|
|
|
|
Input(event::Key),
|
|
|
|
Tick,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// Terminal initialization
|
2017-09-03 13:34:05 +00:00
|
|
|
let backend = MouseBackend::new().unwrap();
|
2016-11-07 23:35:46 +00:00
|
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
|
|
|
|
|
|
// Channels
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
let input_tx = tx.clone();
|
|
|
|
let clock_tx = tx.clone();
|
|
|
|
|
|
|
|
// Input
|
|
|
|
thread::spawn(move || {
|
|
|
|
let stdin = io::stdin();
|
|
|
|
for c in stdin.keys() {
|
|
|
|
let evt = c.unwrap();
|
|
|
|
input_tx.send(Event::Input(evt)).unwrap();
|
|
|
|
if evt == event::Key::Char('q') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Tick
|
2017-05-21 09:13:24 +00:00
|
|
|
thread::spawn(move || loop {
|
2017-09-11 05:58:37 +00:00
|
|
|
clock_tx.send(Event::Tick).unwrap();
|
|
|
|
thread::sleep(time::Duration::from_millis(500));
|
|
|
|
});
|
2016-11-07 23:35:46 +00:00
|
|
|
|
|
|
|
// 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();
|
2018-09-01 12:05:33 +00:00
|
|
|
draw(&mut terminal, &app).unwrap();
|
2016-11-07 23:35:46 +00:00
|
|
|
|
|
|
|
loop {
|
2017-05-08 19:34:41 +00:00
|
|
|
let size = terminal.size().unwrap();
|
|
|
|
if app.size != size {
|
|
|
|
terminal.resize(size).unwrap();
|
|
|
|
app.size = size;
|
|
|
|
}
|
|
|
|
|
2016-11-07 23:35:46 +00:00
|
|
|
let evt = rx.recv().unwrap();
|
|
|
|
match evt {
|
2017-10-30 21:28:37 +00:00
|
|
|
Event::Input(input) => if input == event::Key::Char('q') {
|
|
|
|
break;
|
|
|
|
},
|
2016-11-07 23:35:46 +00:00
|
|
|
Event::Tick => {
|
|
|
|
app.advance();
|
|
|
|
}
|
|
|
|
}
|
2018-09-01 12:05:33 +00:00
|
|
|
draw(&mut terminal, &app).unwrap();
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
terminal.show_cursor().unwrap();
|
|
|
|
}
|
|
|
|
|
2018-09-01 12:05:33 +00:00
|
|
|
fn draw(t: &mut Terminal<MouseBackend>, app: &App) -> Result<(), io::Error> {
|
|
|
|
t.draw(|mut f| {
|
2018-08-12 17:44:52 +00:00
|
|
|
let chunks = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.margin(2)
|
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
2018-08-12 22:27:56 +00:00
|
|
|
.split(app.size);
|
2018-08-12 17:44:52 +00:00
|
|
|
BarChart::default()
|
|
|
|
.block(Block::default().title("Data1").borders(Borders::ALL))
|
|
|
|
.data(&app.data)
|
|
|
|
.bar_width(9)
|
|
|
|
.style(Style::default().fg(Color::Yellow))
|
|
|
|
.value_style(Style::default().fg(Color::Black).bg(Color::Yellow))
|
2018-08-12 22:27:56 +00:00
|
|
|
.render(&mut f, chunks[0]);
|
2018-08-12 17:44:52 +00:00
|
|
|
{
|
|
|
|
let chunks = Layout::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
2018-08-12 22:27:56 +00:00
|
|
|
.split(chunks[1]);
|
2016-11-07 23:35:46 +00:00
|
|
|
BarChart::default()
|
2018-08-12 17:44:52 +00:00
|
|
|
.block(Block::default().title("Data2").borders(Borders::ALL))
|
2016-11-07 23:35:46 +00:00
|
|
|
.data(&app.data)
|
2018-08-12 17:44:52 +00:00
|
|
|
.bar_width(5)
|
|
|
|
.bar_gap(3)
|
|
|
|
.style(Style::default().fg(Color::Green))
|
|
|
|
.value_style(Style::default().bg(Color::Green).modifier(Modifier::Bold))
|
2018-08-12 22:27:56 +00:00
|
|
|
.render(&mut f, chunks[0]);
|
2018-08-12 17:44:52 +00:00
|
|
|
BarChart::default()
|
|
|
|
.block(Block::default().title("Data3").borders(Borders::ALL))
|
|
|
|
.data(&app.data)
|
|
|
|
.style(Style::default().fg(Color::Red))
|
|
|
|
.bar_width(7)
|
|
|
|
.bar_gap(0)
|
|
|
|
.value_style(Style::default().bg(Color::Red))
|
|
|
|
.label_style(Style::default().fg(Color::Cyan).modifier(Modifier::Italic))
|
2018-08-12 22:27:56 +00:00
|
|
|
.render(&mut f, chunks[1]);
|
2018-08-12 17:44:52 +00:00
|
|
|
}
|
2018-09-01 12:05:33 +00:00
|
|
|
})
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|