ratatui/examples/demo.rs

487 lines
19 KiB
Rust
Raw Normal View History

2016-10-11 17:54:35 +00:00
#[macro_use]
extern crate log;
2016-11-07 23:35:46 +00:00
extern crate tui;
#[macro_use]
2016-10-09 17:46:53 +00:00
extern crate termion;
2016-11-07 23:35:46 +00:00
mod util;
2016-10-09 17:46:53 +00:00
2016-11-03 22:59:04 +00:00
use std::io;
2016-10-09 17:46:53 +00:00
use std::thread;
use std::env;
use std::time;
2016-10-09 17:46:53 +00:00
use std::sync::mpsc;
2016-10-14 17:44:52 +00:00
2016-10-09 17:46:53 +00:00
use termion::event;
use termion::input::TermRead;
2016-11-05 18:20:04 +00:00
use tui::{Terminal, TermionBackend};
use tui::widgets::{Widget, Block, SelectableList, List, Gauge, Sparkline, Paragraph, border,
Chart, Axis, Dataset, BarChart, Marker, Tabs, Table};
2016-11-04 16:54:12 +00:00
use tui::widgets::canvas::{Canvas, Map, MapResolution, Line};
use tui::layout::{Group, Direction, Size, Rect};
2016-11-06 17:49:57 +00:00
use tui::style::{Style, Color, Modifier};
2016-10-09 17:46:53 +00:00
2016-11-07 23:35:46 +00:00
use util::*;
2016-10-14 17:44:52 +00:00
struct Server<'a> {
name: &'a str,
location: &'a str,
coords: (f64, f64),
status: &'a str,
}
2016-10-27 17:37:06 +00:00
2016-10-22 09:26:46 +00:00
struct App<'a> {
size: Rect,
2016-10-22 09:26:46 +00:00
items: Vec<&'a str>,
events: Vec<(&'a str, &'a str)>,
2016-10-11 17:54:35 +00:00
selected: usize,
2016-11-07 23:35:46 +00:00
tabs: MyTabs<'a>,
2016-10-14 17:44:52 +00:00
show_chart: bool,
progress: u16,
2016-10-12 17:43:39 +00:00
data: Vec<u64>,
data2: Vec<(f64, f64)>,
2016-10-20 14:26:34 +00:00
data3: Vec<(f64, f64)>,
2016-10-22 10:51:41 +00:00
data4: Vec<(&'a str, u64)>,
window: [f64; 2],
2016-10-14 17:44:52 +00:00
colors: [Color; 2],
color_index: usize,
servers: Vec<Server<'a>>,
2016-10-09 17:46:53 +00:00
}
enum Event {
2016-10-11 17:54:35 +00:00
Input(event::Key),
Tick,
2016-10-09 17:46:53 +00:00
}
fn main() {
2016-10-11 17:54:35 +00:00
for argument in env::args() {
if argument == "--log" {
2016-11-07 23:35:46 +00:00
setup_log("demo.log");
}
}
2016-10-11 17:54:35 +00:00
info!("Start");
2016-11-07 23:35:46 +00:00
let mut rand_signal = RandomSignal::new(0, 100);
2016-11-05 18:20:04 +00:00
let mut sin_signal = SinSignal::new(0.2, 3.0, 18.0);
2016-10-27 10:35:56 +00:00
let mut sin_signal2 = SinSignal::new(0.1, 2.0, 10.0);
2016-10-14 17:44:52 +00:00
2016-10-09 17:46:53 +00:00
let mut app = App {
size: Rect::default(),
2016-10-22 09:26:46 +00:00
items: vec!["Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item8",
2016-11-05 18:20:04 +00:00
"Item9", "Item10", "Item11", "Item12", "Item13", "Item14", "Item15", "Item16",
"Item17", "Item18", "Item19", "Item20", "Item21", "Item22", "Item23", "Item24"],
events: vec![("Event1", "INFO"),
("Event2", "INFO"),
("Event3", "CRITICAL"),
("Event4", "ERROR"),
("Event5", "INFO"),
("Event6", "INFO"),
("Event7", "WARNING"),
("Event8", "INFO"),
("Event9", "INFO"),
("Event10", "INFO"),
("Event11", "CRITICAL"),
("Event12", "INFO"),
("Event13", "INFO"),
("Event14", "INFO"),
("Event15", "INFO"),
("Event16", "INFO"),
("Event17", "ERROR"),
("Event18", "ERROR"),
("Event19", "INFO"),
("Event20", "INFO"),
("Event21", "WARNING"),
("Event22", "INFO"),
("Event23", "INFO"),
("Event24", "WARNING"),
("Event25", "INFO"),
("Event26", "INFO")],
2016-10-11 17:54:35 +00:00
selected: 0,
2016-10-27 17:37:06 +00:00
tabs: MyTabs {
2016-11-07 23:35:46 +00:00
titles: vec!["Tab0", "Tab1"],
2016-10-27 17:37:06 +00:00
selection: 0,
},
2016-10-14 17:44:52 +00:00
show_chart: true,
progress: 0,
2016-11-05 18:20:04 +00:00
data: rand_signal.clone().take(300).collect(),
data2: sin_signal.clone().take(100).collect(),
2016-10-27 10:35:56 +00:00
data3: sin_signal2.clone().take(200).collect(),
2016-10-22 10:51:41 +00:00
data4: vec![("B1", 9),
("B2", 12),
("B3", 5),
("B4", 8),
("B5", 2),
("B6", 4),
("B7", 5),
("B8", 9),
("B9", 14),
("B10", 15),
("B11", 1),
("B12", 0),
("B13", 4),
("B14", 6),
2016-11-05 18:20:04 +00:00
("B15", 4),
("B16", 6),
("B17", 4),
("B18", 7),
("B19", 13),
("B20", 8),
("B21", 11),
("B22", 9),
("B23", 3),
("B24", 5)],
window: [0.0, 20.0],
2016-10-14 17:44:52 +00:00
colors: [Color::Magenta, Color::Red],
color_index: 0,
servers: vec![Server {
2016-11-05 18:20:04 +00:00
name: "NorthAmerica-1",
location: "New York City",
coords: (40.71, -74.00),
status: "Up",
},
Server {
name: "Europe-1",
location: "Paris",
coords: (48.85, 2.35),
status: "Failure",
2016-11-05 18:20:04 +00:00
},
Server {
name: "SouthAmerica-1",
location: "São Paulo",
coords: (-23.54, -46.62),
status: "Up",
},
Server {
name: "Asia-1",
location: "Singapore",
coords: (1.35, 103.86),
status: "Up",
}],
2016-10-09 17:46:53 +00:00
};
let (tx, rx) = mpsc::channel();
let input_tx = tx.clone();
2016-10-09 17:46:53 +00:00
for _ in 0..100 {
sin_signal.next();
2016-10-27 10:35:56 +00:00
}
for _ in 0..200 {
sin_signal2.next();
}
2016-10-09 17:46:53 +00:00
thread::spawn(move || {
2016-11-03 22:59:04 +00:00
let stdin = io::stdin();
2016-10-09 17:46:53 +00:00
for c in stdin.keys() {
let evt = c.unwrap();
input_tx.send(Event::Input(evt)).unwrap();
2016-10-11 17:54:35 +00:00
if evt == event::Key::Char('q') {
break;
2016-10-09 17:46:53 +00:00
}
}
});
2016-10-11 17:54:35 +00:00
thread::spawn(move || {
let tx = tx.clone();
loop {
tx.send(Event::Tick).unwrap();
2016-11-07 00:07:53 +00:00
thread::sleep(time::Duration::from_millis(200));
}
});
2016-11-07 23:35:46 +00:00
let backend = TermionBackend::new().unwrap();
let mut terminal = Terminal::new(backend).unwrap();
2016-11-03 22:59:04 +00:00
terminal.clear().unwrap();
terminal.hide_cursor().unwrap();
2016-10-11 17:54:35 +00:00
2016-10-09 17:46:53 +00:00
loop {
2016-11-05 18:20:04 +00:00
let size = terminal.size().unwrap();
if size != app.size {
2016-11-03 22:59:04 +00:00
terminal.resize(size).unwrap();
app.size = size;
}
2016-11-03 22:59:04 +00:00
draw(&mut terminal, &app).unwrap();
2016-10-09 17:46:53 +00:00
let evt = rx.recv().unwrap();
match evt {
2016-10-11 17:54:35 +00:00
Event::Input(input) => {
match input {
event::Key::Char('q') => {
break;
}
event::Key::Up => {
if app.selected > 0 {
app.selected -= 1
};
}
event::Key::Down => {
if app.selected < app.items.len() - 1 {
app.selected += 1;
}
}
2016-10-27 17:37:06 +00:00
event::Key::Left => {
app.tabs.previous();
}
event::Key::Right => {
app.tabs.next();
}
2016-10-12 09:36:39 +00:00
event::Key::Char('t') => {
2016-10-14 17:44:52 +00:00
app.show_chart = !app.show_chart;
2016-10-12 09:36:39 +00:00
}
2016-10-11 17:54:35 +00:00
_ => {}
}
2016-10-09 17:46:53 +00:00
}
Event::Tick => {
app.progress += 5;
if app.progress > 100 {
app.progress = 0;
}
2016-10-14 17:44:52 +00:00
app.data.insert(0, rand_signal.next().unwrap());
2016-10-12 17:43:39 +00:00
app.data.pop();
for _ in 0..5 {
app.data2.remove(0);
app.data2.push(sin_signal.next().unwrap());
2016-10-27 10:35:56 +00:00
}
for _ in 0..10 {
app.data3.remove(0);
2016-10-27 10:35:56 +00:00
app.data3.push(sin_signal2.next().unwrap());
}
2016-10-22 10:51:41 +00:00
let i = app.data4.pop().unwrap();
app.data4.insert(0, i);
app.window[0] += 1.0;
app.window[1] += 1.0;
let i = app.events.pop().unwrap();
app.events.insert(0, i);
2016-10-14 17:44:52 +00:00
app.color_index += 1;
if app.color_index >= app.colors.len() {
app.color_index = 0;
}
}
2016-10-09 17:46:53 +00:00
}
}
2016-11-03 22:59:04 +00:00
terminal.show_cursor().unwrap();
2016-10-09 17:46:53 +00:00
}
2016-11-05 18:20:04 +00:00
fn draw(t: &mut Terminal<TermionBackend>, app: &App) -> Result<(), io::Error> {
2016-10-09 17:46:53 +00:00
2016-10-27 17:37:06 +00:00
Group::default()
.direction(Direction::Vertical)
.sizes(&[Size::Fixed(3), Size::Min(0)])
.render(t, &app.size, |t, chunks| {
Tabs::default()
.block(Block::default().borders(border::ALL).title("Tabs"))
.titles(&app.tabs.titles)
2016-11-06 17:49:57 +00:00
.style(Style::default().fg(Color::Green))
.highlight_style(Style::default().fg(Color::Yellow))
2016-10-27 17:37:06 +00:00
.select(app.tabs.selection)
2016-11-06 17:49:57 +00:00
.render(t, &chunks[0]);
2016-10-27 17:37:06 +00:00
match app.tabs.selection {
0 => {
2016-11-07 14:57:46 +00:00
draw_first_tab(t, app, &chunks[1]);
2016-10-27 17:37:06 +00:00
}
2016-10-27 20:55:24 +00:00
1 => {
2016-11-07 14:57:46 +00:00
draw_second_tab(t, app, &chunks[1]);
2016-10-27 20:55:24 +00:00
}
2016-10-27 17:37:06 +00:00
_ => {}
};
});
2016-11-03 22:59:04 +00:00
try!(t.draw());
Ok(())
2016-10-27 17:37:06 +00:00
}
2016-10-20 15:17:35 +00:00
2016-11-07 14:57:46 +00:00
fn draw_first_tab(t: &mut Terminal<TermionBackend>, app: &App, area: &Rect) {
Group::default()
2016-10-09 17:46:53 +00:00
.direction(Direction::Vertical)
2016-10-23 12:14:43 +00:00
.sizes(&[Size::Fixed(7), Size::Min(7), Size::Fixed(7)])
2016-10-27 17:37:06 +00:00
.render(t, area, |t, chunks| {
Block::default()
.borders(border::ALL)
.title("Graphs")
2016-11-06 17:49:57 +00:00
.render(t, &chunks[0]);
Group::default()
.direction(Direction::Vertical)
.margin(1)
2016-10-23 12:14:43 +00:00
.sizes(&[Size::Fixed(2), Size::Fixed(3)])
2016-10-20 14:26:34 +00:00
.render(t, &chunks[0], |t, chunks| {
Gauge::default()
.block(Block::default().title("Gauge:"))
2016-11-06 17:49:57 +00:00
.style(Style::default().fg(Color::Magenta).bg(Color::Black).modifier(Modifier::Italic))
2016-11-07 14:57:46 +00:00
.label(&format!("{} / 100", app.progress))
.percent(app.progress)
2016-11-06 17:49:57 +00:00
.render(t, &chunks[0]);
Sparkline::default()
.block(Block::default().title("Sparkline:"))
2016-11-06 17:49:57 +00:00
.style(Style::default().fg(Color::Green))
2016-10-12 17:43:39 +00:00
.data(&app.data)
2016-11-06 17:49:57 +00:00
.render(t, &chunks[1]);
});
2016-10-14 17:44:52 +00:00
let sizes = if app.show_chart {
2016-10-23 12:14:43 +00:00
vec![Size::Percent(50), Size::Percent(50)]
2016-10-12 09:36:39 +00:00
} else {
2016-10-23 12:14:43 +00:00
vec![Size::Percent(100)]
2016-10-12 09:36:39 +00:00
};
Group::default()
2016-10-11 17:54:35 +00:00
.direction(Direction::Horizontal)
2016-10-23 12:14:43 +00:00
.sizes(&sizes)
2016-10-20 14:26:34 +00:00
.render(t, &chunks[1], |t, chunks| {
2016-10-22 09:26:46 +00:00
Group::default()
.direction(Direction::Vertical)
2016-10-23 12:14:43 +00:00
.sizes(&[Size::Percent(50), Size::Percent(50)])
2016-10-22 09:26:46 +00:00
.render(t, &chunks[0], |t, chunks| {
Group::default()
.direction(Direction::Horizontal)
2016-10-23 12:14:43 +00:00
.sizes(&[Size::Percent(50), Size::Percent(50)])
2016-10-22 09:26:46 +00:00
.render(t, &chunks[0], |t, chunks| {
SelectableList::default()
2016-10-27 17:37:06 +00:00
.block(Block::default()
.borders(border::ALL)
.title("List"))
2016-10-22 09:26:46 +00:00
.items(&app.items)
.select(app.selected)
2016-11-06 17:49:57 +00:00
.highlight_style(Style::default().fg(Color::Yellow).modifier(Modifier::Bold))
.highlight_symbol(">")
2016-11-06 17:49:57 +00:00
.render(t, &chunks[0]);
let info_style = Style::default().fg(Color::White);
let warning_style = Style::default().fg(Color::Yellow);
let error_style = Style::default().fg(Color::Magenta);
let critical_style = Style::default().fg(Color::Red);
2016-10-22 09:26:46 +00:00
List::default()
2016-10-27 17:37:06 +00:00
.block(Block::default()
.borders(border::ALL)
.title("List"))
.items(&app.events.iter().map(|&(evt, level)| (format!("{}: {}", level, evt), match level {
"ERROR" => &error_style,
"CRITICAL" => &critical_style,
"WARNING" => &warning_style,
_ => &info_style,
})).collect::<Vec<(String, &Style)>>())
2016-11-06 17:49:57 +00:00
.render(t, &chunks[1]);
2016-10-22 10:51:41 +00:00
});
BarChart::default()
2016-10-27 17:37:06 +00:00
.block(Block::default()
.borders(border::ALL)
.title("Bar chart"))
2016-10-22 10:51:41 +00:00
.data(&app.data4)
.bar_width(3)
.bar_gap(2)
2016-11-06 17:49:57 +00:00
.value_style(Style::default().fg(Color::Black).bg(Color::Green).modifier(Modifier::Italic))
.label_style(Style::default().fg(Color::Yellow))
.style(Style::default().fg(Color::Green))
.render(t, &chunks[1]);
2016-10-22 09:26:46 +00:00
});
2016-10-14 17:44:52 +00:00
if app.show_chart {
Chart::default()
2016-11-06 20:41:32 +00:00
.block(Block::default()
.title("Chart")
.title_style(Style::default()
.fg(Color::Cyan)
.modifier(Modifier::Bold))
.borders(border::ALL))
.x_axis(Axis::default()
.title("X Axis")
2016-11-06 17:49:57 +00:00
.style(Style::default().fg(Color::Gray))
.labels_style(Style::default().modifier(Modifier::Italic))
.bounds(app.window)
.labels(&[&format!("{}", app.window[0]),
&format!("{}", (app.window[0] + app.window[1]) / 2.0),
&format!("{}", app.window[1])]))
.y_axis(Axis::default()
.title("Y Axis")
2016-11-06 17:49:57 +00:00
.style(Style::default().fg(Color::Gray))
.labels_style(Style::default().modifier(Modifier::Italic))
2016-11-05 18:20:04 +00:00
.bounds([-20.0, 20.0])
.labels(&["-20", "0", "20"]))
2016-10-27 10:35:56 +00:00
.datasets(&[Dataset::default()
.name("data2")
2016-10-27 10:35:56 +00:00
.marker(Marker::Dot)
2016-11-06 17:49:57 +00:00
.style(Style::default().fg(Color::Cyan))
2016-10-27 10:35:56 +00:00
.data(&app.data2),
Dataset::default()
.name("data3")
2016-10-27 10:35:56 +00:00
.marker(Marker::Braille)
2016-11-06 17:49:57 +00:00
.style(Style::default().fg(Color::Yellow))
2016-10-27 10:35:56 +00:00
.data(&app.data3)])
2016-11-06 17:49:57 +00:00
.render(t, &chunks[1]);
2016-10-12 09:36:39 +00:00
}
});
2016-11-06 17:49:57 +00:00
Paragraph::default()
2016-11-07 00:07:53 +00:00
.block(Block::default()
.borders(border::ALL)
.title("Footer")
.title_style(Style::default().fg(Color::Magenta).modifier(Modifier::Bold)))
2016-10-22 17:25:17 +00:00
.wrap(true)
2016-11-03 22:59:04 +00:00
.text("This is a paragraph with several lines.\nYou can change the color.\nUse \
2016-11-06 17:49:57 +00:00
\\{fg=[color];bg=[color];mod=[modifier] [text]} to highlight the text with a color. For example, {fg=red \
u}{fg=green n}{fg=yellow d}{fg=magenta e}{fg=cyan r} {fg=gray t}{fg=light_gray h}{fg=light_red \
e} {fg=light_green r}{fg=light_yellow a}{fg=light_magenta i}{fg=light_cyan n}{fg=white \
b}{fg=red o}{fg=green w}.\nOh, and if you didn't {mod=italic notice} you can {mod=bold automatically} \
{mod=invert wrap} your {mod=underline text} =).\nOne more thing is that it should display unicode \
2016-11-03 22:59:04 +00:00
characters properly: , ٩(-̮̮̃-̃)۶ ٩(̮̮̃̃)۶ ٩(̯͡͡)۶ ٩(-̮̮̃̃).")
2016-11-06 17:49:57 +00:00
.render(t, &chunks[2]);
2016-10-09 17:46:53 +00:00
});
}
2016-11-07 14:57:46 +00:00
fn draw_second_tab(t: &mut Terminal<TermionBackend>, app: &App, area: &Rect) {
Group::default()
.direction(Direction::Horizontal)
.sizes(&[Size::Percent(30), Size::Percent(70)])
.render(t, area, |t, chunks| {
let up_style = Style::default().fg(Color::Green);
let failure_style = Style::default().fg(Color::Red);
Table::default()
.block(Block::default()
.title("Servers")
.borders(border::ALL))
.header(&["Server", "Location", "Status"])
.header_style(Style::default().fg(Color::Yellow))
.widths(&[15, 15, 10])
.rows(&app.servers
.iter()
.map(|s| {
(vec![s.name, s.location, s.status],
if s.status == "Up" {
&up_style
} else {
&failure_style
})
})
.collect::<Vec<(Vec<&str>, &Style)>>())
.render(t, &chunks[0]);
Canvas::default()
.block(Block::default().title("World").borders(border::ALL))
.paint(|ctx| {
ctx.draw(&Map {
color: Color::White,
resolution: MapResolution::High,
});
ctx.layer();
for (i, s1) in app.servers.iter().enumerate() {
for s2 in &app.servers[i + 1..] {
ctx.draw(&Line {
x1: s1.coords.1,
y1: s1.coords.0,
y2: s2.coords.0,
x2: s2.coords.1,
color: Color::Yellow,
});
}
}
for server in &app.servers {
let color = if server.status == "Up" {
Color::Green
} else {
Color::Red
};
ctx.print(server.coords.1, server.coords.0, "X", color);
}
})
.x_bounds([-180.0, 180.0])
.y_bounds([-90.0, 90.0])
.render(t, &chunks[1]);
})
}