2023-06-12 05:07:15 +00:00
|
|
|
use std::{
|
|
|
|
error::Error,
|
|
|
|
io,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
use crossterm::{
|
|
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
|
|
|
|
execute,
|
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
|
|
};
|
2021-11-11 14:56:37 +00:00
|
|
|
use rand::{
|
|
|
|
distributions::{Distribution, Uniform},
|
|
|
|
rngs::ThreadRng,
|
|
|
|
};
|
2023-07-16 09:11:59 +00:00
|
|
|
use ratatui::{prelude::*, widgets::*};
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2021-11-11 14:56:37 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct RandomSignal {
|
|
|
|
distribution: Uniform<u64>,
|
|
|
|
rng: ThreadRng,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RandomSignal {
|
|
|
|
pub fn new(lower: u64, upper: u64) -> RandomSignal {
|
|
|
|
RandomSignal {
|
|
|
|
distribution: Uniform::new(lower, upper),
|
|
|
|
rng: rand::thread_rng(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for RandomSignal {
|
|
|
|
type Item = u64;
|
|
|
|
fn next(&mut self) -> Option<u64> {
|
|
|
|
Some(self.distribution.sample(&mut self.rng))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-07 23:35:46 +00:00
|
|
|
struct App {
|
|
|
|
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 {
|
2018-08-12 22:27:56 +00:00
|
|
|
signal,
|
|
|
|
data1,
|
|
|
|
data2,
|
|
|
|
data3,
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
fn on_tick(&mut self) {
|
2016-11-07 23:35:46 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 01:02:51 +00:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
2021-11-01 21:27:46 +00:00
|
|
|
// setup terminal
|
|
|
|
enable_raw_mode()?;
|
|
|
|
let mut stdout = io::stdout();
|
|
|
|
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
|
|
|
let backend = CrosstermBackend::new(stdout);
|
2018-09-23 18:59:51 +00:00
|
|
|
let mut terminal = Terminal::new(backend)?;
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
// create app and run it
|
|
|
|
let tick_rate = Duration::from_millis(250);
|
|
|
|
let app = App::new();
|
|
|
|
let res = run_app(&mut terminal, app, tick_rate);
|
|
|
|
|
|
|
|
// restore terminal
|
|
|
|
disable_raw_mode()?;
|
|
|
|
execute!(
|
|
|
|
terminal.backend_mut(),
|
|
|
|
LeaveAlternateScreen,
|
|
|
|
DisableMouseCapture
|
|
|
|
)?;
|
|
|
|
terminal.show_cursor()?;
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
if let Err(err) = res {
|
2023-05-22 03:46:02 +00:00
|
|
|
println!("{err:?}");
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
fn run_app<B: Backend>(
|
|
|
|
terminal: &mut Terminal<B>,
|
|
|
|
mut app: App,
|
|
|
|
tick_rate: Duration,
|
|
|
|
) -> io::Result<()> {
|
|
|
|
let mut last_tick = Instant::now();
|
2016-11-07 23:35:46 +00:00
|
|
|
loop {
|
2021-12-23 17:46:25 +00:00
|
|
|
terminal.draw(|f| ui(f, &app))?;
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
let timeout = tick_rate
|
|
|
|
.checked_sub(last_tick.elapsed())
|
|
|
|
.unwrap_or_else(|| Duration::from_secs(0));
|
|
|
|
if crossterm::event::poll(timeout)? {
|
|
|
|
if let Event::Key(key) = event::read()? {
|
2021-12-23 17:46:25 +00:00
|
|
|
if let KeyCode::Char('q') = key.code {
|
|
|
|
return Ok(());
|
2018-12-07 15:17:33 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
|
|
|
if last_tick.elapsed() >= tick_rate {
|
|
|
|
app.on_tick();
|
|
|
|
last_tick = Instant::now();
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
fn ui<B: Backend>(f: &mut Frame<B>, app: &App) {
|
|
|
|
let chunks = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.margin(2)
|
|
|
|
.constraints(
|
|
|
|
[
|
|
|
|
Constraint::Length(3),
|
|
|
|
Constraint::Length(3),
|
|
|
|
Constraint::Length(7),
|
|
|
|
Constraint::Min(0),
|
|
|
|
]
|
|
|
|
.as_ref(),
|
|
|
|
)
|
|
|
|
.split(f.size());
|
|
|
|
let sparkline = Sparkline::default()
|
|
|
|
.block(
|
|
|
|
Block::default()
|
|
|
|
.title("Data1")
|
|
|
|
.borders(Borders::LEFT | Borders::RIGHT),
|
|
|
|
)
|
|
|
|
.data(&app.data1)
|
|
|
|
.style(Style::default().fg(Color::Yellow));
|
|
|
|
f.render_widget(sparkline, chunks[0]);
|
|
|
|
let sparkline = Sparkline::default()
|
|
|
|
.block(
|
|
|
|
Block::default()
|
|
|
|
.title("Data2")
|
|
|
|
.borders(Borders::LEFT | Borders::RIGHT),
|
|
|
|
)
|
|
|
|
.data(&app.data2)
|
|
|
|
.style(Style::default().bg(Color::Green));
|
|
|
|
f.render_widget(sparkline, chunks[1]);
|
|
|
|
// Multiline
|
|
|
|
let sparkline = Sparkline::default()
|
|
|
|
.block(
|
|
|
|
Block::default()
|
|
|
|
.title("Data3")
|
|
|
|
.borders(Borders::LEFT | Borders::RIGHT),
|
|
|
|
)
|
|
|
|
.data(&app.data3)
|
|
|
|
.style(Style::default().fg(Color::Red));
|
|
|
|
f.render_widget(sparkline, chunks[2]);
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|