2018-09-23 18:59:51 +00:00
|
|
|
#[allow(dead_code)]
|
2016-11-07 23:35:46 +00:00
|
|
|
mod util;
|
|
|
|
|
|
|
|
use std::io;
|
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
use termion::event::Key;
|
|
|
|
use termion::input::MouseTerminal;
|
|
|
|
use termion::raw::IntoRawMode;
|
|
|
|
use termion::screen::AlternateScreen;
|
|
|
|
use tui::backend::TermionBackend;
|
2017-10-30 21:28:37 +00:00
|
|
|
use tui::style::{Color, Modifier, Style};
|
2019-12-15 20:38:18 +00:00
|
|
|
use tui::widgets::{Axis, Block, Borders, Chart, Dataset, Marker};
|
2018-06-09 09:02:37 +00:00
|
|
|
use tui::Terminal;
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2019-01-06 11:57:06 +00:00
|
|
|
use crate::util::event::{Event, Events};
|
|
|
|
use crate::util::SinSignal;
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2016-11-07 23:35:46 +00:00
|
|
|
struct App {
|
|
|
|
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 {
|
2018-08-12 22:27:56 +00:00
|
|
|
signal1,
|
|
|
|
data1,
|
|
|
|
signal2,
|
|
|
|
data2,
|
2016-11-07 23:35:46 +00:00
|
|
|
window: [0.0, 20.0],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
fn update(&mut self) {
|
2016-11-07 23:35:46 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
fn main() -> Result<(), failure::Error> {
|
2016-11-07 23:35:46 +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)?;
|
|
|
|
terminal.hide_cursor()?;
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
let events = Events::new();
|
2016-11-07 23:35:46 +00:00
|
|
|
|
|
|
|
// App
|
|
|
|
let mut app = App::new();
|
|
|
|
|
|
|
|
loop {
|
2018-09-23 18:59:51 +00:00
|
|
|
terminal.draw(|mut f| {
|
2018-12-07 14:43:05 +00:00
|
|
|
let size = f.size();
|
2019-12-15 20:38:18 +00:00
|
|
|
let x_labels = [
|
|
|
|
format!("{}", app.window[0]),
|
|
|
|
format!("{}", (app.window[0] + app.window[1]) / 2.0),
|
|
|
|
format!("{}", app.window[1]),
|
|
|
|
];
|
|
|
|
let 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),
|
|
|
|
];
|
|
|
|
let chart = Chart::default()
|
2018-09-23 18:59:51 +00:00
|
|
|
.block(
|
|
|
|
Block::default()
|
|
|
|
.title("Chart")
|
2019-02-25 14:15:00 +00:00
|
|
|
.title_style(Style::default().fg(Color::Cyan).modifier(Modifier::BOLD))
|
2018-09-23 18:59:51 +00:00
|
|
|
.borders(Borders::ALL),
|
2018-12-07 15:17:33 +00:00
|
|
|
)
|
|
|
|
.x_axis(
|
2018-09-23 18:59:51 +00:00
|
|
|
Axis::default()
|
|
|
|
.title("X Axis")
|
2019-03-03 16:50:23 +00:00
|
|
|
.style(Style::default().fg(Color::Gray))
|
2019-02-25 14:15:00 +00:00
|
|
|
.labels_style(Style::default().modifier(Modifier::ITALIC))
|
2018-09-23 18:59:51 +00:00
|
|
|
.bounds(app.window)
|
2019-12-15 20:38:18 +00:00
|
|
|
.labels(&x_labels),
|
2018-12-07 15:17:33 +00:00
|
|
|
)
|
|
|
|
.y_axis(
|
2018-09-23 18:59:51 +00:00
|
|
|
Axis::default()
|
|
|
|
.title("Y Axis")
|
2019-03-03 16:50:23 +00:00
|
|
|
.style(Style::default().fg(Color::Gray))
|
2019-02-25 14:15:00 +00:00
|
|
|
.labels_style(Style::default().modifier(Modifier::ITALIC))
|
2018-09-23 18:59:51 +00:00
|
|
|
.bounds([-20.0, 20.0])
|
|
|
|
.labels(&["-20", "0", "20"]),
|
2018-12-07 15:17:33 +00:00
|
|
|
)
|
2019-12-15 20:38:18 +00:00
|
|
|
.datasets(&datasets);
|
|
|
|
f.render_widget(chart, size);
|
2018-09-23 18:59:51 +00:00
|
|
|
})?;
|
|
|
|
|
|
|
|
match events.next()? {
|
2018-12-07 15:17:33 +00:00
|
|
|
Event::Input(input) => {
|
|
|
|
if input == Key::Char('q') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-11-07 23:35:46 +00:00
|
|
|
Event::Tick => {
|
2018-09-23 18:59:51 +00:00
|
|
|
app.update();
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
Ok(())
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|