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;
|
|
|
|
use std::thread;
|
|
|
|
use std::time;
|
|
|
|
use std::sync::mpsc;
|
|
|
|
|
|
|
|
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::{Axis, Block, Borders, Chart, Dataset, Marker, Widget};
|
2017-05-08 19:34:41 +00:00
|
|
|
use tui::layout::Rect;
|
2017-10-30 21:28:37 +00:00
|
|
|
use tui::style::{Color, Modifier, Style};
|
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
|
|
|
signal1: SinSignal,
|
|
|
|
data1: Vec<(f64, f64)>,
|
|
|
|
signal2: SinSignal,
|
|
|
|
data2: Vec<(f64, f64)>,
|
|
|
|
window: [f64; 2],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl App {
|
|
|
|
fn new() -> App {
|
|
|
|
let mut signal1 = SinSignal::new(0.2, 3.0, 18.0);
|
|
|
|
let mut signal2 = SinSignal::new(0.1, 2.0, 10.0);
|
|
|
|
let data1 = signal1.by_ref().take(200).collect::<Vec<(f64, f64)>>();
|
|
|
|
let data2 = signal2.by_ref().take(200).collect::<Vec<(f64, f64)>>();
|
|
|
|
App {
|
2017-05-08 19:34:41 +00:00
|
|
|
size: Rect::default(),
|
2016-11-07 23:35:46 +00:00
|
|
|
signal1: signal1,
|
|
|
|
data1: data1,
|
|
|
|
signal2: signal2,
|
|
|
|
data2: data2,
|
|
|
|
window: [0.0, 20.0],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn advance(&mut self) {
|
|
|
|
for _ in 0..5 {
|
|
|
|
self.data1.remove(0);
|
|
|
|
}
|
|
|
|
self.data1.extend(self.signal1.by_ref().take(5));
|
|
|
|
for _ in 0..10 {
|
|
|
|
self.data2.remove(0);
|
|
|
|
}
|
|
|
|
self.data2.extend(self.signal2.by_ref().take(10));
|
|
|
|
self.window[0] += 1.0;
|
|
|
|
self.window[1] += 1.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
Chart::default()
|
2017-09-11 05:58:37 +00:00
|
|
|
.block(
|
|
|
|
Block::default()
|
|
|
|
.title("Chart")
|
|
|
|
.title_style(Style::default().fg(Color::Cyan).modifier(Modifier::Bold))
|
2017-12-26 15:55:13 +00:00
|
|
|
.borders(Borders::ALL),
|
2017-09-11 05:58:37 +00:00
|
|
|
)
|
|
|
|
.x_axis(
|
|
|
|
Axis::default()
|
|
|
|
.title("X Axis")
|
|
|
|
.style(Style::default().fg(Color::Gray))
|
|
|
|
.labels_style(Style::default().modifier(Modifier::Italic))
|
|
|
|
.bounds(app.window)
|
2017-10-30 21:28:37 +00:00
|
|
|
.labels(&[
|
|
|
|
&format!("{}", app.window[0]),
|
|
|
|
&format!("{}", (app.window[0] + app.window[1]) / 2.0),
|
|
|
|
&format!("{}", app.window[1]),
|
|
|
|
]),
|
2017-09-11 05:58:37 +00:00
|
|
|
)
|
|
|
|
.y_axis(
|
|
|
|
Axis::default()
|
|
|
|
.title("Y Axis")
|
|
|
|
.style(Style::default().fg(Color::Gray))
|
|
|
|
.labels_style(Style::default().modifier(Modifier::Italic))
|
|
|
|
.bounds([-20.0, 20.0])
|
|
|
|
.labels(&["-20", "0", "20"]),
|
|
|
|
)
|
2017-10-30 21:28:37 +00:00
|
|
|
.datasets(&[
|
|
|
|
Dataset::default()
|
|
|
|
.name("data2")
|
|
|
|
.marker(Marker::Dot)
|
|
|
|
.style(Style::default().fg(Color::Cyan))
|
|
|
|
.data(&app.data1),
|
|
|
|
Dataset::default()
|
|
|
|
.name("data3")
|
|
|
|
.marker(Marker::Braille)
|
|
|
|
.style(Style::default().fg(Color::Yellow))
|
|
|
|
.data(&app.data2),
|
|
|
|
])
|
2017-05-08 19:34:41 +00:00
|
|
|
.render(t, &app.size);
|
2016-11-07 23:35:46 +00:00
|
|
|
|
|
|
|
t.draw().unwrap();
|
|
|
|
}
|