Created
March 26, 2018 21:08
-
-
Save snobu/edfc148feb7f70b9733c207289df3df4 to your computer and use it in GitHub Desktop.
tui-rs sparkline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extern crate termion; | |
| extern crate tui; | |
| mod util; | |
| use util::*; | |
| use std::io; | |
| use std::thread; | |
| use std::time; | |
| use std::sync::mpsc; | |
| use termion::event; | |
| use termion::input::TermRead; | |
| use tui::Terminal; | |
| use tui::backend::MouseBackend; | |
| use tui::widgets::{Block, Borders, Sparkline, Widget}; | |
| use tui::layout::{Direction, Group, Rect, Size}; | |
| use tui::style::{Color, Style}; | |
| struct App { | |
| size: Rect, | |
| signal: RandomSignal, | |
| data3: Vec<u64>, | |
| } | |
| impl App { | |
| fn new() -> App { | |
| let mut signal = RandomSignal::new(17, 100); | |
| let data3 = signal.by_ref().take(200).collect::<Vec<u64>>(); | |
| App { | |
| size: Rect::default(), | |
| signal: signal, | |
| data3: data3, | |
| } | |
| } | |
| fn advance(&mut self) { | |
| let value = self.signal.next().unwrap(); | |
| self.data3.pop(); | |
| self.data3.insert(0, value); | |
| } | |
| } | |
| enum Event { | |
| Input(event::Key), | |
| Tick, | |
| } | |
| fn main() { | |
| // Terminal initialization | |
| let backend = MouseBackend::new().unwrap(); | |
| let mut terminal = Terminal::new(backend).unwrap(); | |
| // Channels | |
| let (tx, rx) = mpsc::channel(); | |
| let input_tx = tx.clone(); | |
| let clock_tx = tx.clone(); | |
| // Input | |
| thread::spawn(move || { | |
| let stdin = io::stdin(); | |
| for c in stdin.keys() { | |
| let evt = c.unwrap(); | |
| input_tx.send(Event::Input(evt)).unwrap(); | |
| if evt == event::Key::Char('q') { | |
| break; | |
| } | |
| } | |
| }); | |
| // Tick | |
| thread::spawn(move || loop { | |
| clock_tx.send(Event::Tick).unwrap(); | |
| thread::sleep(time::Duration::from_millis(100)); | |
| }); | |
| // App | |
| let mut app = App::new(); | |
| // First draw call | |
| terminal.clear().unwrap(); | |
| terminal.hide_cursor().unwrap(); | |
| app.size = terminal.size().unwrap(); | |
| draw(&mut terminal, &app); | |
| loop { | |
| let size = terminal.size().unwrap(); | |
| if size != app.size { | |
| terminal.resize(size).unwrap(); | |
| app.size = size; | |
| } | |
| let evt = rx.recv().unwrap(); | |
| match evt { | |
| Event::Input(input) => if input == event::Key::Char('q') { | |
| break; | |
| }, | |
| Event::Tick => { | |
| app.advance(); | |
| } | |
| } | |
| draw(&mut terminal, &app); | |
| } | |
| terminal.show_cursor().unwrap(); | |
| } | |
| fn draw(t: &mut Terminal<MouseBackend>, app: &App) { | |
| Group::default() | |
| .direction(Direction::Vertical) | |
| .margin(1) | |
| .sizes(&[Size::Fixed(1), Size::Fixed(1), Size::Fixed(20), Size::Min(0)]) | |
| .render(t, &app.size, |t, chunks| { | |
| // Multiline | |
| Sparkline::default() | |
| .block( | |
| Block::default() | |
| .title("Data3") | |
| .borders(Borders::LEFT | Borders::RIGHT), | |
| ) | |
| .data(&app.data3) | |
| .style(Style::default().fg(Color::Red)) | |
| .render(t, &chunks[2]); | |
| }); | |
| t.draw().unwrap(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment