2024-01-24 19:50:18 +00:00
|
|
|
//! # [Ratatui] Sparkline example
|
|
|
|
//!
|
|
|
|
//! The latest version of this example is available in the [examples] folder in the repository.
|
|
|
|
//!
|
|
|
|
//! Please note that the examples are designed to be run against the `main` branch of the Github
|
|
|
|
//! repository. This means that you may not be able to compile with the latest release version on
|
|
|
|
//! crates.io, or the one that you have installed locally.
|
|
|
|
//!
|
|
|
|
//! See the [examples readme] for more information on finding examples that match the version of the
|
|
|
|
//! library you are using.
|
|
|
|
//!
|
|
|
|
//! [Ratatui]: https://github.com/ratatui-org/ratatui
|
|
|
|
//! [examples]: https://github.com/ratatui-org/ratatui/blob/main/examples
|
|
|
|
//! [examples readme]: https://github.com/ratatui-org/ratatui/blob/main/examples/README.md
|
|
|
|
|
2023-06-12 05:07:15 +00:00
|
|
|
use std::{
|
|
|
|
error::Error,
|
|
|
|
io,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
|
|
|
|
2021-11-11 14:56:37 +00:00
|
|
|
use rand::{
|
|
|
|
distributions::{Distribution, Uniform},
|
|
|
|
rngs::ThreadRng,
|
|
|
|
};
|
2024-03-02 09:06:53 +00:00
|
|
|
use ratatui::{
|
2024-05-29 11:42:29 +00:00
|
|
|
backend::{Backend, CrosstermBackend},
|
2024-05-28 20:23:39 +00:00
|
|
|
crossterm::{
|
|
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
|
|
|
|
execute,
|
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
|
|
},
|
2024-05-29 11:42:29 +00:00
|
|
|
layout::{Constraint, Layout},
|
|
|
|
style::{Color, Style},
|
2024-03-02 09:06:53 +00:00
|
|
|
widgets::{Block, Borders, Sparkline},
|
2024-08-02 11:18:00 +00:00
|
|
|
Frame, Terminal,
|
2024-03-02 09:06:53 +00:00
|
|
|
};
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2021-11-11 14:56:37 +00:00
|
|
|
#[derive(Clone)]
|
2024-03-02 09:06:53 +00:00
|
|
|
struct RandomSignal {
|
2021-11-11 14:56:37 +00:00
|
|
|
distribution: Uniform<u64>,
|
|
|
|
rng: ThreadRng,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RandomSignal {
|
2024-03-02 09:06:53 +00:00
|
|
|
fn new(lower: u64, upper: u64) -> Self {
|
|
|
|
Self {
|
2021-11-11 14:56:37 +00:00
|
|
|
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 {
|
2024-03-02 09:06:53 +00:00
|
|
|
fn new() -> Self {
|
2016-11-07 23:35:46 +00:00
|
|
|
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>>();
|
2024-03-02 09:06:53 +00:00
|
|
|
Self {
|
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
|
|
|
|
2023-10-20 19:31:52 +00:00
|
|
|
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
|
2024-08-11 00:43:13 +00:00
|
|
|
if event::poll(timeout)? {
|
2021-11-01 21:27:46 +00:00
|
|
|
if let Event::Key(key) = event::read()? {
|
2024-03-02 09:06:53 +00:00
|
|
|
if key.code == KeyCode::Char('q') {
|
2021-12-23 17:46:25 +00:00
|
|
|
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
|
|
|
|
2023-09-26 05:30:36 +00:00
|
|
|
fn ui(f: &mut Frame, app: &App) {
|
2024-01-05 15:45:14 +00:00
|
|
|
let chunks = Layout::vertical([
|
|
|
|
Constraint::Length(3),
|
|
|
|
Constraint::Length(3),
|
|
|
|
Constraint::Min(0),
|
|
|
|
])
|
2024-08-06 03:15:14 +00:00
|
|
|
.split(f.area());
|
2021-11-01 21:27:46 +00:00
|
|
|
let sparkline = Sparkline::default()
|
|
|
|
.block(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::new()
|
|
|
|
.borders(Borders::LEFT | Borders::RIGHT)
|
|
|
|
.title("Data1"),
|
2021-11-01 21:27:46 +00:00
|
|
|
)
|
|
|
|
.data(&app.data1)
|
|
|
|
.style(Style::default().fg(Color::Yellow));
|
|
|
|
f.render_widget(sparkline, chunks[0]);
|
|
|
|
let sparkline = Sparkline::default()
|
|
|
|
.block(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::new()
|
|
|
|
.borders(Borders::LEFT | Borders::RIGHT)
|
|
|
|
.title("Data2"),
|
2021-11-01 21:27:46 +00:00
|
|
|
)
|
|
|
|
.data(&app.data2)
|
|
|
|
.style(Style::default().bg(Color::Green));
|
|
|
|
f.render_widget(sparkline, chunks[1]);
|
|
|
|
// Multiline
|
|
|
|
let sparkline = Sparkline::default()
|
|
|
|
.block(
|
2024-05-02 10:09:48 +00:00
|
|
|
Block::new()
|
|
|
|
.borders(Borders::LEFT | Borders::RIGHT)
|
|
|
|
.title("Data3"),
|
2021-11-01 21:27:46 +00:00
|
|
|
)
|
|
|
|
.data(&app.data3)
|
|
|
|
.style(Style::default().fg(Color::Red));
|
|
|
|
f.render_widget(sparkline, chunks[2]);
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|