bottom/src/canvas.rs

173 lines
6.6 KiB
Rust
Raw Normal View History

2019-09-12 02:10:49 +00:00
use std::io;
2019-09-11 03:37:20 +00:00
use tui::{
layout::{Constraint, Direction, Layout},
style::{Color, Style},
widgets::{Axis, Block, Borders, Chart, Dataset, Marker, Row, Table, Widget},
Terminal,
};
2019-09-12 02:10:49 +00:00
const COLOUR_LIST : [Color; 6] = [Color::LightRed, Color::LightGreen, Color::LightYellow, Color::LightBlue, Color::LightCyan, Color::LightMagenta];
2019-09-12 03:15:25 +00:00
const TEXT_COLOUR : Color = Color::Gray;
const GRAPH_COLOUR : Color = Color::Gray;
const BORDER_STYLE_COLOUR : Color = Color::Gray;
2019-09-12 02:10:49 +00:00
#[derive(Default)]
pub struct CanvasData {
pub disk_data : Vec<Vec<String>>,
pub temp_sensor_data : Vec<Vec<String>>,
pub process_data : Vec<Vec<String>>,
pub mem_data : Vec<(f64, f64)>,
pub swap_data : Vec<(f64, f64)>,
pub cpu_data : Vec<(String, Vec<(f64, f64)>)>,
}
// TODO: Change the error
pub fn draw_data<B : tui::backend::Backend>(terminal : &mut Terminal<B>, canvas_data : &CanvasData) -> Result<(), io::Error> {
let border_style : Style = Style::default().fg(BORDER_STYLE_COLOUR);
let temperature_rows = canvas_data.temp_sensor_data.iter().map(|sensor| Row::StyledData(sensor.iter(), Style::default().fg(TEXT_COLOUR)));
let disk_rows = canvas_data.disk_data.iter().map(|disk| Row::StyledData(disk.iter(), Style::default().fg(TEXT_COLOUR)));
let process_rows = canvas_data.process_data.iter().map(|process| Row::StyledData(process.iter(), Style::default().fg(TEXT_COLOUR)));
2019-09-12 02:10:49 +00:00
// TODO: Convert this into a separate func!
terminal.draw(|mut f| {
2019-09-12 03:15:25 +00:00
debug!("Drawing!");
2019-09-12 02:10:49 +00:00
let vertical_chunks = Layout::default()
.direction(Direction::Vertical)
.margin(1)
2019-09-12 03:15:25 +00:00
.constraints([Constraint::Percentage(34), Constraint::Percentage(34), Constraint::Percentage(32)].as_ref())
2019-09-12 02:10:49 +00:00
.split(f.size());
2019-09-12 03:15:25 +00:00
let _top_chunks = Layout::default()
2019-09-12 02:10:49 +00:00
.direction(Direction::Horizontal)
.margin(0)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.split(vertical_chunks[0]);
2019-09-12 03:15:25 +00:00
2019-09-12 02:10:49 +00:00
let middle_chunks = Layout::default()
.direction(Direction::Horizontal)
.margin(0)
2019-09-12 03:15:25 +00:00
.constraints([Constraint::Percentage(70), Constraint::Percentage(30)].as_ref())
2019-09-12 02:10:49 +00:00
.split(vertical_chunks[1]);
2019-09-12 03:15:25 +00:00
let _middle_divided_chunk_1 = Layout::default()
2019-09-12 02:10:49 +00:00
.direction(Direction::Vertical)
.margin(0)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.split(middle_chunks[0]);
2019-09-12 03:15:25 +00:00
let _middle_divided_chunk_2 = Layout::default()
2019-09-12 02:10:49 +00:00
.direction(Direction::Vertical)
.margin(0)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.split(middle_chunks[1]);
2019-09-12 03:15:25 +00:00
2019-09-12 02:10:49 +00:00
let bottom_chunks = Layout::default()
.direction(Direction::Horizontal)
.margin(0)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.split(vertical_chunks[2]);
2019-09-12 03:15:25 +00:00
let bottom_divided_chunk_1 = Layout::default()
.direction(Direction::Vertical)
.margin(0)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
.split(bottom_chunks[0]);
let bottom_divided_chunk_1_1 = Layout::default()
.direction(Direction::Horizontal)
.margin(0)
.constraints([Constraint::Percentage(40), Constraint::Percentage(60)].as_ref())
.split(bottom_divided_chunk_1[0]);
let bottom_divided_chunk_1_2 = Layout::default()
.direction(Direction::Horizontal)
.margin(0)
.constraints([Constraint::Percentage(40), Constraint::Percentage(60)].as_ref())
.split(bottom_divided_chunk_1[1]);
2019-09-12 02:10:49 +00:00
// Set up blocks and their components
// CPU usage graph
{
let x_axis : Axis<String> = Axis::default().style(Style::default().fg(GRAPH_COLOUR)).bounds([0.0, 60.0]);
let y_axis = Axis::default().style(Style::default().fg(GRAPH_COLOUR)).bounds([0.0, 100.0]).labels(&["0.0", "50.0", "100.0"]);
2019-09-12 02:10:49 +00:00
let mut dataset_vector : Vec<Dataset> = Vec::new();
for (i, cpu) in canvas_data.cpu_data.iter().enumerate() {
dataset_vector.push(
Dataset::default()
.name(&cpu.0)
.marker(Marker::Braille)
.style(Style::default().fg(COLOUR_LIST[i % COLOUR_LIST.len()]))
.data(&(cpu.1)),
);
}
Chart::default()
.block(Block::default().title("CPU Usage").borders(Borders::ALL).border_style(border_style))
2019-09-12 02:10:49 +00:00
.x_axis(x_axis)
.y_axis(y_axis)
.datasets(&dataset_vector)
2019-09-12 03:15:25 +00:00
.render(&mut f, vertical_chunks[0]);
2019-09-12 02:10:49 +00:00
}
//Memory usage graph
{
let x_axis : Axis<String> = Axis::default().style(Style::default().fg(GRAPH_COLOUR)).bounds([0.0, 60.0]);
let y_axis = Axis::default().style(Style::default().fg(GRAPH_COLOUR)).bounds([0.0, 100.0]).labels(&["0.0", "50.0", "100.0"]);
2019-09-12 02:10:49 +00:00
Chart::default()
.block(Block::default().title("Memory Usage").borders(Borders::ALL).border_style(border_style))
2019-09-12 02:10:49 +00:00
.x_axis(x_axis)
.y_axis(y_axis)
.datasets(&[
Dataset::default()
.name(&("MEM :".to_string() + &format!("{:3}%", (canvas_data.mem_data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64))))
.marker(Marker::Braille)
2019-09-12 03:15:25 +00:00
.style(Style::default().fg(Color::LightRed))
2019-09-12 02:10:49 +00:00
.data(&canvas_data.mem_data),
Dataset::default()
.name(&("SWAP:".to_string() + &format!("{:3}%", (canvas_data.swap_data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64))))
.marker(Marker::Braille)
.style(Style::default().fg(Color::LightGreen))
.data(&canvas_data.swap_data),
])
2019-09-12 03:15:25 +00:00
.render(&mut f, middle_chunks[0]);
2019-09-12 02:10:49 +00:00
}
2019-09-12 03:15:25 +00:00
// Network graph
Block::default().title("Network").borders(Borders::ALL).border_style(border_style).render(&mut f, middle_chunks[1]);
2019-09-12 03:15:25 +00:00
2019-09-12 02:10:49 +00:00
// Temperature table
Table::new(["Sensor", "Temperature"].iter(), temperature_rows)
.block(Block::default().title("Temperatures").borders(Borders::ALL).border_style(border_style))
2019-09-12 02:10:49 +00:00
.header_style(Style::default().fg(Color::LightBlue))
.widths(&[15, 5])
2019-09-12 03:15:25 +00:00
.render(&mut f, bottom_divided_chunk_1_1[0]);
2019-09-12 02:10:49 +00:00
// Disk usage table
Table::new(["Disk", "Mount", "Used", "Total", "Free"].iter(), disk_rows)
.block(Block::default().title("Disk Usage").borders(Borders::ALL).border_style(border_style))
2019-09-12 02:10:49 +00:00
.header_style(Style::default().fg(Color::LightBlue))
.widths(&[15, 10, 5, 5, 5])
2019-09-12 03:15:25 +00:00
.render(&mut f, bottom_divided_chunk_1_2[0]);
2019-09-12 02:10:49 +00:00
// Temp graph
Block::default()
.title("Temperatures")
.borders(Borders::ALL)
.border_style(border_style)
.render(&mut f, bottom_divided_chunk_1_1[1]);
2019-09-12 02:10:49 +00:00
// IO graph
Block::default()
.title("IO Usage")
.borders(Borders::ALL)
.border_style(border_style)
.render(&mut f, bottom_divided_chunk_1_2[1]);
2019-09-12 02:10:49 +00:00
// Processes table
Table::new(["PID", "Name", "CPU%", "Mem%"].iter(), process_rows)
.block(Block::default().title("Processes").borders(Borders::ALL).border_style(border_style))
2019-09-12 02:10:49 +00:00
.header_style(Style::default().fg(Color::LightBlue))
.widths(&[5, 15, 10, 10])
.render(&mut f, bottom_chunks[1]);
})?;
Ok(())
}