2016-11-03 22:59:04 +00:00
|
|
|
use std::io;
|
2016-10-14 17:44:52 +00:00
|
|
|
|
2019-02-10 21:35:44 +00:00
|
|
|
use tui::backend::Backend;
|
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};
|
2019-01-14 20:14:57 +00:00
|
|
|
use tui::widgets::canvas::{Canvas, Line, Map, MapResolution, Rectangle};
|
2018-08-12 17:44:52 +00:00
|
|
|
use tui::widgets::{
|
2018-09-08 06:03:30 +00:00
|
|
|
Axis, BarChart, Block, Borders, Chart, Dataset, Gauge, List, Marker, Paragraph, Row,
|
2018-08-12 22:27:56 +00:00
|
|
|
SelectableList, Sparkline, Table, Tabs, Text, Widget,
|
2018-08-12 17:44:52 +00:00
|
|
|
};
|
|
|
|
use tui::{Frame, Terminal};
|
2016-10-09 17:46:53 +00:00
|
|
|
|
2019-02-10 21:35:44 +00:00
|
|
|
use crate::demo::App;
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2019-02-10 21:35:44 +00:00
|
|
|
pub fn draw<B: Backend>(terminal: &mut Terminal<B>, app: &App) -> Result<(), io::Error> {
|
|
|
|
terminal.draw(|mut f| {
|
|
|
|
let chunks = Layout::default()
|
|
|
|
.constraints([Constraint::Length(3), Constraint::Min(0)].as_ref())
|
|
|
|
.split(f.size());
|
|
|
|
Tabs::default()
|
|
|
|
.block(Block::default().borders(Borders::ALL).title(app.title))
|
|
|
|
.titles(&app.tabs.titles)
|
|
|
|
.style(Style::default().fg(Color::Green))
|
|
|
|
.highlight_style(Style::default().fg(Color::Yellow))
|
|
|
|
.select(app.tabs.index)
|
|
|
|
.render(&mut f, chunks[0]);
|
|
|
|
match app.tabs.index {
|
|
|
|
0 => draw_first_tab(&mut f, &app, chunks[1]),
|
|
|
|
1 => draw_second_tab(&mut f, &app, chunks[1]),
|
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
})
|
2016-10-09 17:46:53 +00:00
|
|
|
}
|
|
|
|
|
2018-11-04 17:49:30 +00:00
|
|
|
fn draw_first_tab<B>(f: &mut Frame<B>, app: &App, area: Rect)
|
|
|
|
where
|
|
|
|
B: Backend,
|
|
|
|
{
|
2018-08-12 17:44:52 +00:00
|
|
|
let chunks = Layout::default()
|
|
|
|
.constraints(
|
|
|
|
[
|
|
|
|
Constraint::Length(7),
|
|
|
|
Constraint::Min(7),
|
|
|
|
Constraint::Length(7),
|
2018-09-23 18:59:51 +00:00
|
|
|
]
|
2018-12-07 15:17:33 +00:00
|
|
|
.as_ref(),
|
|
|
|
)
|
|
|
|
.split(area);
|
2018-08-12 22:27:56 +00:00
|
|
|
draw_gauges(f, app, chunks[0]);
|
|
|
|
draw_charts(f, app, chunks[1]);
|
|
|
|
draw_text(f, chunks[2]);
|
2017-05-21 09:13:24 +00:00
|
|
|
}
|
|
|
|
|
2018-11-04 17:49:30 +00:00
|
|
|
fn draw_gauges<B>(f: &mut Frame<B>, app: &App, area: Rect)
|
|
|
|
where
|
|
|
|
B: Backend,
|
|
|
|
{
|
2018-08-12 17:44:52 +00:00
|
|
|
let chunks = Layout::default()
|
|
|
|
.constraints([Constraint::Length(2), Constraint::Length(3)].as_ref())
|
|
|
|
.margin(1)
|
2018-08-12 22:27:56 +00:00
|
|
|
.split(area);
|
2017-05-21 09:13:24 +00:00
|
|
|
Block::default()
|
2017-12-26 15:55:13 +00:00
|
|
|
.borders(Borders::ALL)
|
2017-05-21 09:13:24 +00:00
|
|
|
.title("Graphs")
|
2018-08-12 17:44:52 +00:00
|
|
|
.render(f, area);
|
|
|
|
Gauge::default()
|
|
|
|
.block(Block::default().title("Gauge:"))
|
|
|
|
.style(
|
|
|
|
Style::default()
|
|
|
|
.fg(Color::Magenta)
|
|
|
|
.bg(Color::Black)
|
2019-03-10 16:43:56 +00:00
|
|
|
.modifier(Modifier::ITALIC | Modifier::BOLD),
|
2018-12-07 15:17:33 +00:00
|
|
|
)
|
|
|
|
.label(&format!("{} / 100", app.progress))
|
2018-08-12 17:44:52 +00:00
|
|
|
.percent(app.progress)
|
2018-08-12 22:27:56 +00:00
|
|
|
.render(f, chunks[0]);
|
2018-08-12 17:44:52 +00:00
|
|
|
Sparkline::default()
|
|
|
|
.block(Block::default().title("Sparkline:"))
|
|
|
|
.style(Style::default().fg(Color::Green))
|
2019-02-10 21:35:44 +00:00
|
|
|
.data(&app.sparkline.points)
|
2018-08-12 22:27:56 +00:00
|
|
|
.render(f, chunks[1]);
|
2017-05-21 09:13:24 +00:00
|
|
|
}
|
|
|
|
|
2018-11-04 17:49:30 +00:00
|
|
|
fn draw_charts<B>(f: &mut Frame<B>, app: &App, area: Rect)
|
|
|
|
where
|
|
|
|
B: Backend,
|
|
|
|
{
|
2018-08-12 17:44:52 +00:00
|
|
|
let constraints = if app.show_chart {
|
|
|
|
vec![Constraint::Percentage(50), Constraint::Percentage(50)]
|
2017-05-21 09:13:24 +00:00
|
|
|
} else {
|
2018-08-12 17:44:52 +00:00
|
|
|
vec![Constraint::Percentage(100)]
|
2017-05-21 09:13:24 +00:00
|
|
|
};
|
2018-08-12 17:44:52 +00:00
|
|
|
let chunks = Layout::default()
|
|
|
|
.constraints(constraints)
|
2017-05-21 09:13:24 +00:00
|
|
|
.direction(Direction::Horizontal)
|
2018-08-12 22:27:56 +00:00
|
|
|
.split(area);
|
2018-08-12 17:44:52 +00:00
|
|
|
{
|
|
|
|
let chunks = Layout::default()
|
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
2018-08-12 22:27:56 +00:00
|
|
|
.split(chunks[0]);
|
2018-08-12 17:44:52 +00:00
|
|
|
{
|
|
|
|
let chunks = Layout::default()
|
|
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
|
|
|
.direction(Direction::Horizontal)
|
2018-08-12 22:27:56 +00:00
|
|
|
.split(chunks[0]);
|
2018-08-12 17:44:52 +00:00
|
|
|
SelectableList::default()
|
|
|
|
.block(Block::default().borders(Borders::ALL).title("List"))
|
2019-02-10 21:35:44 +00:00
|
|
|
.items(&app.tasks.items)
|
|
|
|
.select(Some(app.tasks.selected))
|
2019-02-25 14:15:00 +00:00
|
|
|
.highlight_style(Style::default().fg(Color::Yellow).modifier(Modifier::BOLD))
|
2018-08-12 17:44:52 +00:00
|
|
|
.highlight_symbol(">")
|
2018-08-12 22:27:56 +00:00
|
|
|
.render(f, chunks[0]);
|
2018-08-12 17:44:52 +00:00
|
|
|
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);
|
2019-02-10 21:35:44 +00:00
|
|
|
let events = app.logs.items.iter().map(|&(evt, level)| {
|
2018-09-08 06:03:30 +00:00
|
|
|
Text::styled(
|
2018-08-12 17:44:52 +00:00
|
|
|
format!("{}: {}", level, evt),
|
|
|
|
match level {
|
2018-09-07 20:24:52 +00:00
|
|
|
"ERROR" => error_style,
|
|
|
|
"CRITICAL" => critical_style,
|
|
|
|
"WARNING" => warning_style,
|
|
|
|
_ => info_style,
|
2018-08-12 17:44:52 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
});
|
|
|
|
List::new(events)
|
|
|
|
.block(Block::default().borders(Borders::ALL).title("List"))
|
2018-08-12 22:27:56 +00:00
|
|
|
.render(f, chunks[1]);
|
2018-08-12 17:44:52 +00:00
|
|
|
}
|
|
|
|
BarChart::default()
|
|
|
|
.block(Block::default().borders(Borders::ALL).title("Bar chart"))
|
2019-02-10 21:35:44 +00:00
|
|
|
.data(&app.barchart)
|
2018-08-12 17:44:52 +00:00
|
|
|
.bar_width(3)
|
|
|
|
.bar_gap(2)
|
|
|
|
.value_style(
|
|
|
|
Style::default()
|
|
|
|
.fg(Color::Black)
|
|
|
|
.bg(Color::Green)
|
2019-02-25 14:15:00 +00:00
|
|
|
.modifier(Modifier::ITALIC),
|
2018-12-07 15:17:33 +00:00
|
|
|
)
|
|
|
|
.label_style(Style::default().fg(Color::Yellow))
|
2018-08-12 17:44:52 +00:00
|
|
|
.style(Style::default().fg(Color::Green))
|
2018-08-12 22:27:56 +00:00
|
|
|
.render(f, chunks[1]);
|
2018-08-12 17:44:52 +00:00
|
|
|
}
|
|
|
|
if app.show_chart {
|
|
|
|
Chart::default()
|
|
|
|
.block(
|
|
|
|
Block::default()
|
|
|
|
.title("Chart")
|
2019-02-25 14:15:00 +00:00
|
|
|
.title_style(Style::default().fg(Color::Cyan).modifier(Modifier::BOLD))
|
2018-08-12 17:44:52 +00:00
|
|
|
.borders(Borders::ALL),
|
2018-12-07 15:17:33 +00:00
|
|
|
)
|
|
|
|
.x_axis(
|
2018-08-12 17:44:52 +00:00
|
|
|
Axis::default()
|
|
|
|
.title("X Axis")
|
|
|
|
.style(Style::default().fg(Color::Gray))
|
2019-02-25 14:15:00 +00:00
|
|
|
.labels_style(Style::default().modifier(Modifier::ITALIC))
|
2019-02-10 21:35:44 +00:00
|
|
|
.bounds(app.signals.window)
|
2018-08-12 17:44:52 +00:00
|
|
|
.labels(&[
|
2019-02-10 21:35:44 +00:00
|
|
|
&format!("{}", app.signals.window[0]),
|
|
|
|
&format!("{}", (app.signals.window[0] + app.signals.window[1]) / 2.0),
|
|
|
|
&format!("{}", app.signals.window[1]),
|
2018-08-12 17:44:52 +00:00
|
|
|
]),
|
2018-12-07 15:17:33 +00:00
|
|
|
)
|
|
|
|
.y_axis(
|
2018-08-12 17:44:52 +00:00
|
|
|
Axis::default()
|
|
|
|
.title("Y Axis")
|
|
|
|
.style(Style::default().fg(Color::Gray))
|
2019-02-25 14:15:00 +00:00
|
|
|
.labels_style(Style::default().modifier(Modifier::ITALIC))
|
2018-08-12 17:44:52 +00:00
|
|
|
.bounds([-20.0, 20.0])
|
|
|
|
.labels(&["-20", "0", "20"]),
|
2018-12-07 15:17:33 +00:00
|
|
|
)
|
|
|
|
.datasets(&[
|
2018-08-12 17:44:52 +00:00
|
|
|
Dataset::default()
|
|
|
|
.name("data2")
|
|
|
|
.marker(Marker::Dot)
|
|
|
|
.style(Style::default().fg(Color::Cyan))
|
2019-02-10 21:35:44 +00:00
|
|
|
.data(&app.signals.sin1.points),
|
2018-08-12 17:44:52 +00:00
|
|
|
Dataset::default()
|
|
|
|
.name("data3")
|
|
|
|
.marker(Marker::Braille)
|
|
|
|
.style(Style::default().fg(Color::Yellow))
|
2019-02-10 21:35:44 +00:00
|
|
|
.data(&app.signals.sin2.points),
|
2018-12-07 15:17:33 +00:00
|
|
|
])
|
|
|
|
.render(f, chunks[1]);
|
2018-08-12 17:44:52 +00:00
|
|
|
}
|
2016-10-09 17:46:53 +00:00
|
|
|
}
|
2016-11-07 14:57:46 +00:00
|
|
|
|
2018-11-04 17:49:30 +00:00
|
|
|
fn draw_text<B>(f: &mut Frame<B>, area: Rect)
|
|
|
|
where
|
|
|
|
B: Backend,
|
|
|
|
{
|
2018-08-12 20:13:32 +00:00
|
|
|
let text = [
|
2018-09-08 05:33:37 +00:00
|
|
|
Text::raw("This is a paragraph with several lines. You can change style your text the way you want.\n\nFox example: "),
|
|
|
|
Text::styled("under", Style::default().fg(Color::Red)),
|
|
|
|
Text::raw(" "),
|
|
|
|
Text::styled("the", Style::default().fg(Color::Green)),
|
|
|
|
Text::raw(" "),
|
|
|
|
Text::styled("rainbow", Style::default().fg(Color::Blue)),
|
|
|
|
Text::raw(".\nOh and if you didn't "),
|
2019-02-25 14:15:00 +00:00
|
|
|
Text::styled("notice", Style::default().modifier(Modifier::ITALIC)),
|
2018-09-08 05:33:37 +00:00
|
|
|
Text::raw(" you can "),
|
2019-02-25 14:15:00 +00:00
|
|
|
Text::styled("automatically", Style::default().modifier(Modifier::BOLD)),
|
2018-09-08 05:33:37 +00:00
|
|
|
Text::raw(" "),
|
2019-02-25 14:15:00 +00:00
|
|
|
Text::styled("wrap", Style::default().modifier(Modifier::REVERSED)),
|
2018-09-08 05:33:37 +00:00
|
|
|
Text::raw(" your "),
|
2019-02-25 14:15:00 +00:00
|
|
|
Text::styled("text", Style::default().modifier(Modifier::UNDERLINED)),
|
2018-09-08 05:33:37 +00:00
|
|
|
Text::raw(".\nOne more thing is that it should display unicode characters: 10€")
|
2018-08-12 20:13:32 +00:00
|
|
|
];
|
|
|
|
Paragraph::new(text.iter())
|
2017-10-30 21:28:37 +00:00
|
|
|
.block(
|
|
|
|
Block::default()
|
2017-12-26 15:55:13 +00:00
|
|
|
.borders(Borders::ALL)
|
2017-10-30 21:28:37 +00:00
|
|
|
.title("Footer")
|
2019-02-25 14:15:00 +00:00
|
|
|
.title_style(Style::default().fg(Color::Magenta).modifier(Modifier::BOLD)),
|
2018-12-07 15:17:33 +00:00
|
|
|
)
|
|
|
|
.wrap(true)
|
2018-08-12 17:44:52 +00:00
|
|
|
.render(f, area);
|
2017-05-21 09:13:24 +00:00
|
|
|
}
|
|
|
|
|
2018-11-04 17:49:30 +00:00
|
|
|
fn draw_second_tab<B>(f: &mut Frame<B>, app: &App, area: Rect)
|
|
|
|
where
|
|
|
|
B: Backend,
|
|
|
|
{
|
2018-08-12 17:44:52 +00:00
|
|
|
let chunks = Layout::default()
|
|
|
|
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)].as_ref())
|
2016-11-07 14:57:46 +00:00
|
|
|
.direction(Direction::Horizontal)
|
2018-08-12 17:44:52 +00:00
|
|
|
.split(area);
|
|
|
|
let up_style = Style::default().fg(Color::Green);
|
2019-03-10 16:43:56 +00:00
|
|
|
let failure_style = Style::default()
|
|
|
|
.fg(Color::Red)
|
|
|
|
.modifier(Modifier::RAPID_BLINK | Modifier::CROSSED_OUT);
|
2018-09-23 18:59:51 +00:00
|
|
|
let header = ["Server", "Location", "Status"];
|
|
|
|
let rows = app.servers.iter().map(|s| {
|
|
|
|
let style = if s.status == "Up" {
|
|
|
|
up_style
|
|
|
|
} else {
|
|
|
|
failure_style
|
|
|
|
};
|
|
|
|
Row::StyledData(vec![s.name, s.location, s.status].into_iter(), style)
|
|
|
|
});
|
|
|
|
Table::new(header.into_iter(), rows)
|
|
|
|
.block(Block::default().title("Servers").borders(Borders::ALL))
|
2018-08-12 17:44:52 +00:00
|
|
|
.header_style(Style::default().fg(Color::Yellow))
|
2019-07-10 10:39:10 +00:00
|
|
|
.widths(&[
|
|
|
|
Constraint::Length(15),
|
|
|
|
Constraint::Length(15),
|
|
|
|
Constraint::Length(10),
|
|
|
|
])
|
2018-08-12 22:27:56 +00:00
|
|
|
.render(f, chunks[0]);
|
2016-11-07 14:57:46 +00:00
|
|
|
|
2018-08-12 17:44:52 +00:00
|
|
|
Canvas::default()
|
|
|
|
.block(Block::default().title("World").borders(Borders::ALL))
|
|
|
|
.paint(|ctx| {
|
|
|
|
ctx.draw(&Map {
|
|
|
|
color: Color::White,
|
|
|
|
resolution: MapResolution::High,
|
|
|
|
});
|
|
|
|
ctx.layer();
|
2019-01-14 20:14:57 +00:00
|
|
|
ctx.draw(&Rectangle {
|
|
|
|
rect: Rect {
|
|
|
|
x: 0,
|
|
|
|
y: 30,
|
|
|
|
width: 10,
|
|
|
|
height: 10,
|
|
|
|
},
|
|
|
|
color: Color::Yellow,
|
|
|
|
});
|
2018-08-12 17:44:52 +00:00
|
|
|
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,
|
2017-09-10 21:03:27 +00:00
|
|
|
});
|
2018-08-12 17:44:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2018-12-07 15:17:33 +00:00
|
|
|
})
|
|
|
|
.x_bounds([-180.0, 180.0])
|
2018-08-12 17:44:52 +00:00
|
|
|
.y_bounds([-90.0, 90.0])
|
2018-08-12 22:27:56 +00:00
|
|
|
.render(f, chunks[1]);
|
2016-11-07 14:57:46 +00:00
|
|
|
}
|