ratatui/examples/barchart.rs

133 lines
3.9 KiB
Rust
Raw Normal View History

extern crate failure;
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
#[allow(dead_code)]
mod util;
2016-11-07 23:35:46 +00:00
use std::io;
2016-11-07 23:35:46 +00:00
use termion::event::Key;
use termion::input::MouseTerminal;
use termion::raw::IntoRawMode;
use termion::screen::AlternateScreen;
use tui::backend::TermionBackend;
use tui::layout::{Constraint, Direction, Layout};
2017-10-30 21:28:37 +00:00
use tui::style::{Color, Modifier, Style};
2018-05-06 10:59:24 +00:00
use tui::widgets::{BarChart, Block, Borders, Widget};
2018-06-09 09:02:37 +00:00
use tui::Terminal;
2016-11-07 23:35:46 +00:00
use util::event::{Event, Events};
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
}
}
fn update(&mut self) {
2016-11-07 23:35:46 +00:00
let value = self.data.pop().unwrap();
self.data.insert(0, value);
}
}
fn main() -> Result<(), failure::Error> {
2016-11-07 23:35:46 +00:00
// Terminal initialization
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
// Setup event handlers
let events = Events::new();
2016-11-07 23:35:46 +00:00
// App
let mut app = App::new();
loop {
let size = terminal.size()?;
terminal.draw(|mut f| {
let chunks = Layout::default()
.direction(Direction::Vertical)
.margin(2)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.split(size);
BarChart::default()
.block(Block::default().title("Data1").borders(Borders::ALL))
.data(&app.data)
.bar_width(9)
.style(Style::default().fg(Color::Yellow))
.value_style(Style::default().fg(Color::Black).bg(Color::Yellow))
.render(&mut f, chunks[0]);
{
let chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.split(chunks[1]);
BarChart::default()
.block(Block::default().title("Data2").borders(Borders::ALL))
.data(&app.data)
.bar_width(5)
.bar_gap(3)
.style(Style::default().fg(Color::Green))
.value_style(Style::default().bg(Color::Green).modifier(Modifier::Bold))
.render(&mut f, chunks[0]);
BarChart::default()
.block(Block::default().title("Data3").borders(Borders::ALL))
.data(&app.data)
.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).modifier(Modifier::Italic))
.render(&mut f, chunks[1]);
}
})?;
2017-05-08 19:34:41 +00:00
match events.next()? {
Event::Input(input) => {
if input == Key::Char('q') {
break;
}
}
2016-11-07 23:35:46 +00:00
Event::Tick => {
app.update();
2016-11-07 23:35:46 +00:00
}
}
}
Ok(())
2016-11-07 23:35:46 +00:00
}