2018-09-23 18:59:51 +00:00
|
|
|
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
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
mod util;
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
use std::io;
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
use termion::event::Key;
|
|
|
|
use termion::input::MouseTerminal;
|
|
|
|
use termion::raw::IntoRawMode;
|
|
|
|
use termion::screen::AlternateScreen;
|
|
|
|
use tui::backend::TermionBackend;
|
2018-08-12 17:44:52 +00:00
|
|
|
use tui::layout::{Constraint, Direction, Layout, Rect};
|
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::{Block, Borders, Gauge, Widget};
|
2018-06-09 09:02:37 +00:00
|
|
|
use tui::Terminal;
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
use util::event::{Event, Events};
|
|
|
|
|
2016-11-07 23:35:46 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
fn update(&mut self) {
|
2016-11-07 23:35:46 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
fn main() -> Result<(), failure::Error> {
|
2016-11-07 23:35:46 +00:00
|
|
|
// Terminal initialization
|
2018-09-23 18:59:51 +00:00
|
|
|
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
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
let events = Events::new();
|
2016-11-07 23:35:46 +00:00
|
|
|
|
|
|
|
let mut app = App::new();
|
|
|
|
|
|
|
|
loop {
|
2018-09-23 18:59:51 +00:00
|
|
|
let size = terminal.size()?;
|
2017-05-08 19:34:41 +00:00
|
|
|
if size != app.size {
|
2018-09-23 18:59:51 +00:00
|
|
|
terminal.resize(size)?;
|
2017-05-08 19:34:41 +00:00
|
|
|
app.size = size;
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
terminal.draw(|mut f| {
|
|
|
|
let chunks = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.margin(2)
|
|
|
|
.constraints(
|
|
|
|
[
|
|
|
|
Constraint::Percentage(25),
|
|
|
|
Constraint::Percentage(25),
|
|
|
|
Constraint::Percentage(25),
|
|
|
|
Constraint::Percentage(25),
|
|
|
|
]
|
|
|
|
.as_ref(),
|
|
|
|
).split(app.size);
|
|
|
|
Gauge::default()
|
|
|
|
.block(Block::default().title("Gauge1").borders(Borders::ALL))
|
|
|
|
.style(Style::default().fg(Color::Yellow))
|
|
|
|
.percent(app.progress1)
|
|
|
|
.render(&mut f, chunks[0]);
|
|
|
|
Gauge::default()
|
|
|
|
.block(Block::default().title("Gauge2").borders(Borders::ALL))
|
|
|
|
.style(Style::default().fg(Color::Magenta).bg(Color::Green))
|
|
|
|
.percent(app.progress2)
|
|
|
|
.label(&format!("{}/100", app.progress2))
|
|
|
|
.render(&mut f, chunks[1]);
|
|
|
|
Gauge::default()
|
|
|
|
.block(Block::default().title("Gauge2").borders(Borders::ALL))
|
|
|
|
.style(Style::default().fg(Color::Yellow))
|
|
|
|
.percent(app.progress3)
|
|
|
|
.render(&mut f, chunks[2]);
|
|
|
|
Gauge::default()
|
|
|
|
.block(Block::default().title("Gauge3").borders(Borders::ALL))
|
|
|
|
.style(Style::default().fg(Color::Cyan).modifier(Modifier::Italic))
|
|
|
|
.percent(app.progress4)
|
|
|
|
.label(&format!("{}/100", app.progress2))
|
|
|
|
.render(&mut f, chunks[3]);
|
|
|
|
})?;
|
|
|
|
|
|
|
|
match events.next()? {
|
|
|
|
Event::Input(input) => if input == Key::Char('q') {
|
2017-10-30 21:28:37 +00:00
|
|
|
break;
|
|
|
|
},
|
2016-11-07 23:35:46 +00:00
|
|
|
Event::Tick => {
|
2018-09-23 18:59:51 +00:00
|
|
|
app.update();
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
Ok(())
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|