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-11-26 02:06:34 +00:00
|
|
|
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::{Block, Borders, Gauge, Widget};
|
2018-06-09 09:02:37 +00:00
|
|
|
use tui::Terminal;
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2019-01-06 11:57:06 +00:00
|
|
|
use crate::util::event::{Event, Events};
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2016-11-07 23:35:46 +00:00
|
|
|
struct App {
|
|
|
|
progress1: u16,
|
|
|
|
progress2: u16,
|
2018-11-27 01:02:37 +00:00
|
|
|
progress3: f64,
|
2016-11-07 23:35:46 +00:00
|
|
|
progress4: u16,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl App {
|
|
|
|
fn new() -> App {
|
|
|
|
App {
|
|
|
|
progress1: 0,
|
|
|
|
progress2: 0,
|
2018-11-27 01:02:37 +00:00
|
|
|
progress3: 0.0,
|
2016-11-07 23:35:46 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-11-27 01:02:37 +00:00
|
|
|
self.progress3 += 0.001;
|
|
|
|
if self.progress3 > 1.0 {
|
|
|
|
self.progress3 = 0.0;
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
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),
|
|
|
|
]
|
2018-12-07 15:17:33 +00:00
|
|
|
.as_ref(),
|
|
|
|
)
|
2018-12-07 14:43:05 +00:00
|
|
|
.split(f.size());
|
2018-11-26 02:06:34 +00:00
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
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()
|
2018-11-27 01:02:37 +00:00
|
|
|
.block(Block::default().title("Gauge3").borders(Borders::ALL))
|
2018-09-23 18:59:51 +00:00
|
|
|
.style(Style::default().fg(Color::Yellow))
|
2018-11-27 01:02:37 +00:00
|
|
|
.ratio(app.progress3)
|
2018-09-23 18:59:51 +00:00
|
|
|
.render(&mut f, chunks[2]);
|
|
|
|
Gauge::default()
|
2018-11-27 01:02:37 +00:00
|
|
|
.block(Block::default().title("Gauge4").borders(Borders::ALL))
|
2019-02-25 14:15:00 +00:00
|
|
|
.style(Style::default().fg(Color::Cyan).modifier(Modifier::ITALIC))
|
2018-09-23 18:59:51 +00:00
|
|
|
.percent(app.progress4)
|
|
|
|
.label(&format!("{}/100", app.progress2))
|
|
|
|
.render(&mut f, chunks[3]);
|
|
|
|
})?;
|
|
|
|
|
|
|
|
match events.next()? {
|
2018-12-07 15:17:33 +00:00
|
|
|
Event::Input(input) => {
|
|
|
|
if input == Key::Char('q') {
|
|
|
|
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
|
|
|
}
|