tui-rs/examples/gauge.rs

122 lines
3.4 KiB
Rust
Raw Normal View History

#[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::{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};
2016-11-07 23:35:46 +00:00
struct App {
progress1: u16,
progress2: u16,
progress3: f64,
2016-11-07 23:35:46 +00:00
progress4: u16,
}
impl App {
fn new() -> App {
App {
progress1: 0,
progress2: 0,
progress3: 0.0,
2016-11-07 23:35:46 +00:00
progress4: 0,
}
}
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 += 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;
}
}
}
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
let events = Events::new();
2016-11-07 23:35:46 +00:00
let mut app = App::new();
loop {
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(f.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("Gauge3").borders(Borders::ALL))
.style(Style::default().fg(Color::Yellow))
.ratio(app.progress3)
.render(&mut f, chunks[2]);
Gauge::default()
.block(Block::default().title("Gauge4").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') {
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
}