feat: add an option to run the examples without all unicode symbols

This commit is contained in:
Florian Dehau 2020-04-14 01:04:07 +02:00
parent 584e1b0500
commit c98002eb76
6 changed files with 61 additions and 24 deletions

View file

@ -30,6 +30,9 @@ struct Cli {
/// time in ms between two ticks. /// time in ms between two ticks.
#[argh(option, default = "250")] #[argh(option, default = "250")]
tick_rate: u64, tick_rate: u64,
/// whether unicode symbols are used to improve the overall look of the app
#[argh(option, default = "true")]
enhanced_graphics: bool,
} }
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
@ -48,10 +51,11 @@ fn main() -> Result<(), Box<dyn Error>> {
// Setup input handling // Setup input handling
let (tx, rx) = mpsc::channel(); let (tx, rx) = mpsc::channel();
let tick_rate = Duration::from_millis(cli.tick_rate);
thread::spawn(move || { thread::spawn(move || {
loop { loop {
// poll for tick rate duration, if no events, sent tick event. // poll for tick rate duration, if no events, sent tick event.
if event::poll(Duration::from_millis(cli.tick_rate)).unwrap() { if event::poll(tick_rate).unwrap() {
if let CEvent::Key(key) = event::read().unwrap() { if let CEvent::Key(key) = event::read().unwrap() {
tx.send(Event::Input(key)).unwrap(); tx.send(Event::Input(key)).unwrap();
} }
@ -61,7 +65,7 @@ fn main() -> Result<(), Box<dyn Error>> {
} }
}); });
let mut app = App::new("Crossterm Demo"); let mut app = App::new("Crossterm Demo", cli.enhanced_graphics);
terminal.clear()?; terminal.clear()?;

View file

@ -18,6 +18,9 @@ struct Cli {
/// time in ms between two ticks. /// time in ms between two ticks.
#[argh(option, default = "250")] #[argh(option, default = "250")]
tick_rate: u64, tick_rate: u64,
/// whether unicode symbols are used to improve the overall look of the app
#[argh(option, default = "true")]
enhanced_graphics: bool,
} }
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
@ -32,7 +35,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut terminal = Terminal::new(backend)?; let mut terminal = Terminal::new(backend)?;
terminal.hide_cursor()?; terminal.hide_cursor()?;
let mut app = App::new("Curses demo"); let mut app = App::new("Curses demo", cli.enhanced_graphics);
let mut last_tick = Instant::now(); let mut last_tick = Instant::now();
let tick_rate = Duration::from_millis(cli.tick_rate); let tick_rate = Duration::from_millis(cli.tick_rate);

View file

@ -108,17 +108,18 @@ pub struct App<'a> {
pub should_quit: bool, pub should_quit: bool,
pub tabs: TabsState<'a>, pub tabs: TabsState<'a>,
pub show_chart: bool, pub show_chart: bool,
pub progress: u16, pub progress: f64,
pub sparkline: Signal<RandomSignal>, pub sparkline: Signal<RandomSignal>,
pub tasks: StatefulList<&'a str>, pub tasks: StatefulList<&'a str>,
pub logs: StatefulList<(&'a str, &'a str)>, pub logs: StatefulList<(&'a str, &'a str)>,
pub signals: Signals, pub signals: Signals,
pub barchart: Vec<(&'a str, u64)>, pub barchart: Vec<(&'a str, u64)>,
pub servers: Vec<Server<'a>>, pub servers: Vec<Server<'a>>,
pub enhanced_graphics: bool,
} }
impl<'a> App<'a> { impl<'a> App<'a> {
pub fn new(title: &'a str) -> App<'a> { pub fn new(title: &'a str, enhanced_graphics: bool) -> App<'a> {
let mut rand_signal = RandomSignal::new(0, 100); let mut rand_signal = RandomSignal::new(0, 100);
let sparkline_points = rand_signal.by_ref().take(300).collect(); let sparkline_points = rand_signal.by_ref().take(300).collect();
let mut sin_signal = SinSignal::new(0.2, 3.0, 18.0); let mut sin_signal = SinSignal::new(0.2, 3.0, 18.0);
@ -130,7 +131,7 @@ impl<'a> App<'a> {
should_quit: false, should_quit: false,
tabs: TabsState::new(vec!["Tab0", "Tab1"]), tabs: TabsState::new(vec!["Tab0", "Tab1"]),
show_chart: true, show_chart: true,
progress: 0, progress: 0.0,
sparkline: Signal { sparkline: Signal {
source: rand_signal, source: rand_signal,
points: sparkline_points, points: sparkline_points,
@ -178,6 +179,7 @@ impl<'a> App<'a> {
status: "Up", status: "Up",
}, },
], ],
enhanced_graphics,
} }
} }
@ -211,9 +213,9 @@ impl<'a> App<'a> {
pub fn on_tick(&mut self) { pub fn on_tick(&mut self) {
// Update progress // Update progress
self.progress += 5; self.progress += 0.001;
if self.progress > 100 { if self.progress > 1.0 {
self.progress = 0; self.progress = 0.0;
} }
self.sparkline.on_tick(); self.sparkline.on_tick();

View file

@ -1,12 +1,15 @@
use tui::backend::Backend; use tui::{
use tui::layout::{Constraint, Direction, Layout, Rect}; backend::Backend,
use tui::style::{Color, Modifier, Style}; layout::{Constraint, Direction, Layout, Rect},
use tui::widgets::canvas::{Canvas, Line, Map, MapResolution, Rectangle}; style::{Color, Modifier, Style},
use tui::widgets::{ symbols,
Axis, BarChart, Block, Borders, Chart, Dataset, Gauge, List, Marker, Paragraph, Row, Sparkline, widgets::canvas::{Canvas, Line, Map, MapResolution, Rectangle},
Table, Tabs, Text, widgets::{
Axis, BarChart, Block, Borders, Chart, Dataset, Gauge, List, Paragraph, Row, Sparkline,
Table, Tabs, Text,
},
Frame,
}; };
use tui::Frame;
use crate::demo::App; use crate::demo::App;
@ -58,7 +61,7 @@ where
let block = Block::default().borders(Borders::ALL).title("Graphs"); let block = Block::default().borders(Borders::ALL).title("Graphs");
f.render_widget(block, area); f.render_widget(block, area);
let label = format!("{} / 100", app.progress); let label = format!("{:.2}%", app.progress * 100.0);
let gauge = Gauge::default() let gauge = Gauge::default()
.block(Block::default().title("Gauge:")) .block(Block::default().title("Gauge:"))
.style( .style(
@ -68,13 +71,18 @@ where
.modifier(Modifier::ITALIC | Modifier::BOLD), .modifier(Modifier::ITALIC | Modifier::BOLD),
) )
.label(&label) .label(&label)
.percent(app.progress); .ratio(app.progress);
f.render_widget(gauge, chunks[0]); f.render_widget(gauge, chunks[0]);
let sparkline = Sparkline::default() let sparkline = Sparkline::default()
.block(Block::default().title("Sparkline:")) .block(Block::default().title("Sparkline:"))
.style(Style::default().fg(Color::Green)) .style(Style::default().fg(Color::Green))
.data(&app.sparkline.points); .data(&app.sparkline.points)
.bar_set(if app.enhanced_graphics {
symbols::bar::NINE_LEVELS
} else {
symbols::bar::THREE_LEVELS
});
f.render_widget(sparkline, chunks[1]); f.render_widget(sparkline, chunks[1]);
} }
@ -134,6 +142,11 @@ where
.data(&app.barchart) .data(&app.barchart)
.bar_width(3) .bar_width(3)
.bar_gap(2) .bar_gap(2)
.bar_set(if app.enhanced_graphics {
symbols::bar::NINE_LEVELS
} else {
symbols::bar::THREE_LEVELS
})
.value_style( .value_style(
Style::default() Style::default()
.fg(Color::Black) .fg(Color::Black)
@ -153,12 +166,16 @@ where
let datasets = [ let datasets = [
Dataset::default() Dataset::default()
.name("data2") .name("data2")
.marker(Marker::Dot) .marker(symbols::Marker::Dot)
.style(Style::default().fg(Color::Cyan)) .style(Style::default().fg(Color::Cyan))
.data(&app.signals.sin1.points), .data(&app.signals.sin1.points),
Dataset::default() Dataset::default()
.name("data3") .name("data3")
.marker(Marker::Braille) .marker(if app.enhanced_graphics {
symbols::Marker::Braille
} else {
symbols::Marker::Dot
})
.style(Style::default().fg(Color::Yellow)) .style(Style::default().fg(Color::Yellow))
.data(&app.signals.sin2.points), .data(&app.signals.sin2.points),
]; ];
@ -285,6 +302,11 @@ where
ctx.print(server.coords.1, server.coords.0, "X", color); ctx.print(server.coords.1, server.coords.0, "X", color);
} }
}) })
.marker(if app.enhanced_graphics {
symbols::Marker::Braille
} else {
symbols::Marker::Dot
})
.x_bounds([-180.0, 180.0]) .x_bounds([-180.0, 180.0])
.y_bounds([-90.0, 90.0]); .y_bounds([-90.0, 90.0]);
f.render_widget(map, chunks[1]); f.render_widget(map, chunks[1]);

View file

@ -17,6 +17,9 @@ struct Cli {
/// time in ms between two ticks. /// time in ms between two ticks.
#[argh(option, default = "250")] #[argh(option, default = "250")]
tick_rate: u64, tick_rate: u64,
/// whether unicode symbols are used to improve the overall look of the app
#[argh(option, default = "true")]
enhanced_graphics: bool,
} }
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
@ -26,7 +29,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut terminal = Terminal::new(backend)?; let mut terminal = Terminal::new(backend)?;
terminal.hide_cursor()?; terminal.hide_cursor()?;
let mut app = App::new("Rustbox demo"); let mut app = App::new("Rustbox demo", cli.enhanced_graphics);
let mut last_tick = Instant::now(); let mut last_tick = Instant::now();
let tick_rate = Duration::from_millis(cli.tick_rate); let tick_rate = Duration::from_millis(cli.tick_rate);

View file

@ -17,6 +17,9 @@ struct Cli {
/// time in ms between two ticks. /// time in ms between two ticks.
#[argh(option, default = "250")] #[argh(option, default = "250")]
tick_rate: u64, tick_rate: u64,
/// whether unicode symbols are used to improve the overall look of the app
#[argh(option, default = "true")]
enhanced_graphics: bool,
} }
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
@ -34,7 +37,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut terminal = Terminal::new(backend)?; let mut terminal = Terminal::new(backend)?;
terminal.hide_cursor()?; terminal.hide_cursor()?;
let mut app = App::new("Termion demo"); let mut app = App::new("Termion demo", cli.enhanced_graphics);
loop { loop {
terminal.draw(|mut f| ui::draw(&mut f, &mut app))?; terminal.draw(|mut f| ui::draw(&mut f, &mut app))?;