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
|
|
|
|
|
|
|
mod util;
|
|
|
|
use util::*;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-05-25 05:55:49 +00:00
|
|
|
use tui::Terminal;
|
2017-09-03 13:34:05 +00:00
|
|
|
use tui::backend::MouseBackend;
|
2017-10-30 21:28:37 +00:00
|
|
|
use tui::layout::{Direction, Group, Rect, Size};
|
|
|
|
use tui::style::{Color, Style};
|
2018-05-06 10:59:24 +00:00
|
|
|
use tui::widgets::{Block, Borders, Sparkline, Widget};
|
2016-11-07 23:35:46 +00:00
|
|
|
|
|
|
|
struct App {
|
2017-05-08 19:34:41 +00:00
|
|
|
size: Rect,
|
2016-11-07 23:35:46 +00:00
|
|
|
signal: RandomSignal,
|
|
|
|
data1: Vec<u64>,
|
|
|
|
data2: Vec<u64>,
|
|
|
|
data3: Vec<u64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl App {
|
|
|
|
fn new() -> App {
|
|
|
|
let mut signal = RandomSignal::new(0, 100);
|
|
|
|
let data1 = signal.by_ref().take(200).collect::<Vec<u64>>();
|
|
|
|
let data2 = signal.by_ref().take(200).collect::<Vec<u64>>();
|
|
|
|
let data3 = signal.by_ref().take(200).collect::<Vec<u64>>();
|
|
|
|
App {
|
2017-05-08 19:34:41 +00:00
|
|
|
size: Rect::default(),
|
2016-11-07 23:35:46 +00:00
|
|
|
signal: signal,
|
|
|
|
data1: data1,
|
|
|
|
data2: data2,
|
|
|
|
data3: data3,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn advance(&mut self) {
|
|
|
|
let value = self.signal.next().unwrap();
|
|
|
|
self.data1.pop();
|
|
|
|
self.data1.insert(0, value);
|
|
|
|
let value = self.signal.next().unwrap();
|
|
|
|
self.data2.pop();
|
|
|
|
self.data2.insert(0, value);
|
|
|
|
let value = self.signal.next().unwrap();
|
|
|
|
self.data3.pop();
|
|
|
|
self.data3.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();
|
2016-11-07 23:35:46 +00:00
|
|
|
draw(&mut terminal, &app);
|
|
|
|
|
|
|
|
loop {
|
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-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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
draw(&mut terminal, &app);
|
|
|
|
}
|
|
|
|
|
|
|
|
terminal.show_cursor().unwrap();
|
|
|
|
}
|
|
|
|
|
2017-09-03 13:34:05 +00:00
|
|
|
fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
|
2016-11-07 23:35:46 +00:00
|
|
|
Group::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.margin(2)
|
2017-12-26 11:05:47 +00:00
|
|
|
.sizes(&[Size::Fixed(3), Size::Fixed(3), Size::Fixed(7), Size::Min(0)])
|
2017-05-08 19:34:41 +00:00
|
|
|
.render(t, &app.size, |t, chunks| {
|
2016-11-07 23:35:46 +00:00
|
|
|
Sparkline::default()
|
2017-10-30 21:28:37 +00:00
|
|
|
.block(
|
|
|
|
Block::default()
|
|
|
|
.title("Data1")
|
2017-12-26 15:55:13 +00:00
|
|
|
.borders(Borders::LEFT | Borders::RIGHT),
|
2017-10-30 21:28:37 +00:00
|
|
|
)
|
2016-11-07 23:35:46 +00:00
|
|
|
.data(&app.data1)
|
|
|
|
.style(Style::default().fg(Color::Yellow))
|
|
|
|
.render(t, &chunks[0]);
|
|
|
|
Sparkline::default()
|
2017-10-30 21:28:37 +00:00
|
|
|
.block(
|
|
|
|
Block::default()
|
|
|
|
.title("Data2")
|
2017-12-26 15:55:13 +00:00
|
|
|
.borders(Borders::LEFT | Borders::RIGHT),
|
2017-10-30 21:28:37 +00:00
|
|
|
)
|
2016-11-07 23:35:46 +00:00
|
|
|
.data(&app.data2)
|
|
|
|
.style(Style::default().bg(Color::Green))
|
|
|
|
.render(t, &chunks[1]);
|
|
|
|
// Multiline
|
|
|
|
Sparkline::default()
|
2017-10-30 21:28:37 +00:00
|
|
|
.block(
|
|
|
|
Block::default()
|
|
|
|
.title("Data3")
|
2017-12-26 15:55:13 +00:00
|
|
|
.borders(Borders::LEFT | Borders::RIGHT),
|
2017-10-30 21:28:37 +00:00
|
|
|
)
|
2016-11-07 23:35:46 +00:00
|
|
|
.data(&app.data3)
|
|
|
|
.style(Style::default().fg(Color::Red))
|
|
|
|
.render(t, &chunks[2]);
|
|
|
|
});
|
|
|
|
|
|
|
|
t.draw().unwrap();
|
|
|
|
}
|