2016-11-07 23:35:46 +00:00
|
|
|
extern crate tui;
|
|
|
|
extern crate termion;
|
|
|
|
|
|
|
|
use std::io;
|
|
|
|
use std::thread;
|
|
|
|
use std::time;
|
|
|
|
use std::sync::mpsc;
|
|
|
|
|
|
|
|
use termion::event;
|
|
|
|
use termion::input::TermRead;
|
|
|
|
|
2016-11-28 08:52:51 +00:00
|
|
|
use tui::Terminal;
|
2017-09-03 13:34:05 +00:00
|
|
|
use tui::backend::MouseBackend;
|
2016-11-07 23:35:46 +00:00
|
|
|
use tui::widgets::{Widget, Block, border, Gauge};
|
2017-05-08 19:34:41 +00:00
|
|
|
use tui::layout::{Group, Direction, Size, Rect};
|
2016-11-07 23:35:46 +00:00
|
|
|
use tui::style::{Style, Color, Modifier};
|
|
|
|
|
|
|
|
struct App {
|
2017-05-08 19:34:41 +00:00
|
|
|
size: Rect,
|
2016-11-07 23:35:46 +00:00
|
|
|
progress1: u16,
|
|
|
|
progress2: u16,
|
|
|
|
progress3: u16,
|
|
|
|
progress4: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl App {
|
|
|
|
fn new() -> App {
|
|
|
|
App {
|
2017-05-08 19:34:41 +00:00
|
|
|
size: Rect::default(),
|
2016-11-07 23:35:46 +00:00
|
|
|
progress1: 0,
|
|
|
|
progress2: 0,
|
|
|
|
progress3: 0,
|
|
|
|
progress4: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn advance(&mut self) {
|
|
|
|
self.progress1 += 5;
|
|
|
|
if self.progress1 > 100 {
|
|
|
|
self.progress1 = 0;
|
|
|
|
}
|
|
|
|
self.progress2 += 10;
|
|
|
|
if self.progress2 > 100 {
|
|
|
|
self.progress2 = 0;
|
|
|
|
}
|
|
|
|
self.progress3 += 1;
|
|
|
|
if self.progress3 > 100 {
|
|
|
|
self.progress3 = 0;
|
|
|
|
}
|
|
|
|
self.progress4 += 3;
|
|
|
|
if self.progress4 > 100 {
|
|
|
|
self.progress4 = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Event {
|
|
|
|
Input(event::Key),
|
|
|
|
Tick,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// Terminal initialization
|
2017-09-03 13:34:05 +00:00
|
|
|
let backend = MouseBackend::new().unwrap();
|
2016-11-07 23:35:46 +00:00
|
|
|
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
|
2017-05-21 09:13:24 +00:00
|
|
|
thread::spawn(move || loop {
|
|
|
|
clock_tx.send(Event::Tick).unwrap();
|
|
|
|
thread::sleep(time::Duration::from_millis(500));
|
|
|
|
});
|
2016-11-07 23:35:46 +00:00
|
|
|
|
|
|
|
// App
|
|
|
|
let mut app = App::new();
|
|
|
|
|
|
|
|
// First draw call
|
|
|
|
terminal.clear().unwrap();
|
|
|
|
terminal.hide_cursor().unwrap();
|
2017-05-08 19:34:41 +00:00
|
|
|
app.size = terminal.size().unwrap();
|
2016-11-07 23:35:46 +00:00
|
|
|
draw(&mut terminal, &app);
|
|
|
|
|
|
|
|
loop {
|
2017-05-08 19:34:41 +00:00
|
|
|
let size = terminal.size().unwrap();
|
|
|
|
if size != app.size {
|
|
|
|
terminal.resize(size).unwrap();
|
|
|
|
app.size = size;
|
|
|
|
}
|
|
|
|
|
2016-11-07 23:35:46 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-09-03 13:34:05 +00:00
|
|
|
fn draw(t: &mut Terminal<MouseBackend>, app: &App) {
|
2016-11-07 23:35:46 +00:00
|
|
|
|
|
|
|
Group::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.margin(2)
|
2017-05-21 09:13:24 +00:00
|
|
|
.sizes(&[Size::Percent(25),
|
|
|
|
Size::Percent(25),
|
|
|
|
Size::Percent(25),
|
|
|
|
Size::Percent(25)])
|
2017-05-08 19:34:41 +00:00
|
|
|
.render(t, &app.size, |t, chunks| {
|
2016-11-07 23:35:46 +00:00
|
|
|
Gauge::default()
|
|
|
|
.block(Block::default().title("Gauge1").borders(border::ALL))
|
|
|
|
.style(Style::default().fg(Color::Yellow))
|
|
|
|
.percent(app.progress1)
|
|
|
|
.render(t, &chunks[0]);
|
|
|
|
Gauge::default()
|
|
|
|
.block(Block::default().title("Gauge2").borders(border::ALL))
|
|
|
|
.style(Style::default().fg(Color::Magenta).bg(Color::Green))
|
|
|
|
.percent(app.progress2)
|
|
|
|
.label(&format!("{}/100", app.progress2))
|
|
|
|
.render(t, &chunks[1]);
|
|
|
|
Gauge::default()
|
|
|
|
.block(Block::default().title("Gauge2").borders(border::ALL))
|
|
|
|
.style(Style::default().fg(Color::Yellow))
|
|
|
|
.percent(app.progress3)
|
|
|
|
.render(t, &chunks[2]);
|
|
|
|
Gauge::default()
|
|
|
|
.block(Block::default().title("Gauge3").borders(border::ALL))
|
|
|
|
.style(Style::default().fg(Color::Cyan).modifier(Modifier::Italic))
|
|
|
|
.percent(app.progress4)
|
|
|
|
.label(&format!("{}/100", app.progress2))
|
|
|
|
.render(t, &chunks[3]);
|
|
|
|
});
|
|
|
|
|
|
|
|
t.draw().unwrap();
|
|
|
|
}
|