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},
|
|
|
|
};
|
2023-03-17 16:03:49 +00:00
|
|
|
use ratatui::{
|
2021-11-01 21:27:46 +00:00
|
|
|
backend::{Backend, CrosstermBackend},
|
2020-03-13 01:02:51 +00:00
|
|
|
layout::{Constraint, Direction, Layout},
|
|
|
|
style::{Color, Modifier, Style},
|
|
|
|
widgets::{BarChart, Block, Borders},
|
2021-11-01 21:27:46 +00:00
|
|
|
Frame, Terminal,
|
2020-03-13 01:02:51 +00:00
|
|
|
};
|
2023-03-17 16:03:49 +00:00
|
|
|
use std::{
|
|
|
|
error::Error,
|
|
|
|
io,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2016-11-07 23:35:46 +00:00
|
|
|
struct App<'a> {
|
|
|
|
data: Vec<(&'a str, u64)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> App<'a> {
|
|
|
|
fn new() -> App<'a> {
|
|
|
|
App {
|
2017-09-11 05:58:37 +00:00
|
|
|
data: vec![
|
|
|
|
("B1", 9),
|
|
|
|
("B2", 12),
|
|
|
|
("B3", 5),
|
|
|
|
("B4", 8),
|
|
|
|
("B5", 2),
|
|
|
|
("B6", 4),
|
|
|
|
("B7", 5),
|
|
|
|
("B8", 9),
|
|
|
|
("B9", 14),
|
|
|
|
("B10", 15),
|
|
|
|
("B11", 1),
|
|
|
|
("B12", 0),
|
|
|
|
("B13", 4),
|
|
|
|
("B14", 6),
|
|
|
|
("B15", 4),
|
|
|
|
("B16", 6),
|
|
|
|
("B17", 4),
|
|
|
|
("B18", 7),
|
|
|
|
("B19", 13),
|
|
|
|
("B20", 8),
|
|
|
|
("B21", 11),
|
|
|
|
("B22", 9),
|
|
|
|
("B23", 3),
|
|
|
|
("B24", 5),
|
|
|
|
],
|
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.data.pop().unwrap();
|
|
|
|
self.data.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);
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
// restore terminal
|
|
|
|
disable_raw_mode()?;
|
|
|
|
execute!(
|
|
|
|
terminal.backend_mut(),
|
|
|
|
LeaveAlternateScreen,
|
|
|
|
DisableMouseCapture
|
|
|
|
)?;
|
|
|
|
terminal.show_cursor()?;
|
2019-12-15 20:38:18 +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
|
|
|
}
|
2019-12-15 20:38:18 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2019-12-15 20:38:18 +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();
|
|
|
|
loop {
|
2021-12-23 17:46:25 +00:00
|
|
|
terminal.draw(|f| ui(f, &app))?;
|
2017-05-08 19:34:41 +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::Percentage(50), Constraint::Percentage(50)].as_ref())
|
|
|
|
.split(f.size());
|
|
|
|
let barchart = BarChart::default()
|
|
|
|
.block(Block::default().title("Data1").borders(Borders::ALL))
|
|
|
|
.data(&app.data)
|
|
|
|
.bar_width(9)
|
|
|
|
.bar_style(Style::default().fg(Color::Yellow))
|
|
|
|
.value_style(Style::default().fg(Color::Black).bg(Color::Yellow));
|
|
|
|
f.render_widget(barchart, chunks[0]);
|
|
|
|
|
|
|
|
let chunks = Layout::default()
|
|
|
|
.direction(Direction::Horizontal)
|
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
|
|
|
.split(chunks[1]);
|
|
|
|
|
|
|
|
let barchart = BarChart::default()
|
|
|
|
.block(Block::default().title("Data2").borders(Borders::ALL))
|
|
|
|
.data(&app.data)
|
|
|
|
.bar_width(5)
|
|
|
|
.bar_gap(3)
|
|
|
|
.bar_style(Style::default().fg(Color::Green))
|
|
|
|
.value_style(
|
|
|
|
Style::default()
|
|
|
|
.bg(Color::Green)
|
|
|
|
.add_modifier(Modifier::BOLD),
|
|
|
|
);
|
|
|
|
f.render_widget(barchart, chunks[0]);
|
|
|
|
|
|
|
|
let barchart = BarChart::default()
|
|
|
|
.block(Block::default().title("Data3").borders(Borders::ALL))
|
|
|
|
.data(&app.data)
|
|
|
|
.bar_style(Style::default().fg(Color::Red))
|
|
|
|
.bar_width(7)
|
|
|
|
.bar_gap(0)
|
|
|
|
.value_style(Style::default().bg(Color::Red))
|
|
|
|
.label_style(
|
|
|
|
Style::default()
|
|
|
|
.fg(Color::Cyan)
|
|
|
|
.add_modifier(Modifier::ITALIC),
|
|
|
|
);
|
|
|
|
f.render_widget(barchart, chunks[1]);
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|