use crate::{
app, constants,
data_conversion::{ConvertedCpuData, ConvertedProcessData},
utils::{error, gen_util::*},
};
use tui::{
backend,
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style},
terminal::Frame,
widgets::{Axis, Block, Borders, Chart, Dataset, Marker, Paragraph, Row, Table, Text, Widget},
Terminal,
};
const TEXT_COLOUR: Color = Color::Gray;
const GRAPH_COLOUR: Color = Color::Gray;
const BORDER_STYLE_COLOUR: Color = Color::Gray;
const HIGHLIGHTED_BORDER_STYLE_COLOUR: Color = Color::LightBlue;
const GOLDEN_RATIO: f32 = 0.618_034; // Approx, good enough for use (also Clippy gets mad if it's too long)
lazy_static! {
static ref HELP_TEXT: [Text<'static>; 15] = [
Text::raw("\nGeneral Keybindings\n"),
Text::raw("q, Ctrl-c to quit.\n"),
Text::raw("Ctrl-r to reset all data.\n"),
Text::raw("f to toggle freezing and unfreezing the display.\n"),
Text::raw(
"Ctrl+Up/k, Ctrl+Down/j, Ctrl+Left/h, Ctrl+Right/l to navigate between panels.\n"
),
Text::raw("Up and Down scrolls through a list.\n"),
Text::raw("Esc to close a dialog window (help or dd confirmation).\n"),
Text::raw("? to get this help screen.\n"),
Text::raw("\n Process Panel Keybindings\n"),
Text::raw("dd to kill the selected process.\n"),
Text::raw("c to sort by CPU usage.\n"),
Text::raw("m to sort by memory usage.\n"),
Text::raw("p to sort by PID.\n"),
Text::raw("n to sort by process name.\n"),
Text::raw("`Tab` to group together processes with the same name.\n")
];
static ref COLOUR_LIST: Vec<Color> = gen_n_colours(constants::NUM_COLOURS);
static ref CANVAS_BORDER_STYLE: Style = Style::default().fg(BORDER_STYLE_COLOUR);
static ref CANVAS_HIGHLIGHTED_BORDER_STYLE: Style =
Style::default().fg(HIGHLIGHTED_BORDER_STYLE_COLOUR);
}
#[derive(Default)]
pub struct CanvasData {
pub rx_display: String,
pub tx_display: String,
pub total_rx_display: String,
pub total_tx_display: String,
pub network_data_rx: Vec<(f64, f64)>,
pub network_data_tx: Vec<(f64, f64)>,
pub disk_data: Vec<Vec<String>>,
pub temp_sensor_data: Vec<Vec<String>>,
pub process_data: Vec<ConvertedProcessData>,
pub grouped_process_data: Vec<ConvertedProcessData>,
pub memory_labels: Vec<(u64, u64)>,
pub mem_data: Vec<(f64, f64)>,
pub swap_data: Vec<(f64, f64)>,
pub cpu_data: Vec<ConvertedCpuData>,
}
/// Generates random colours.
/// Strategy found from https://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
fn gen_n_colours(num_to_gen: i32) -> Vec<Color> {
fn gen_hsv(h: f32) -> f32 {
let new_val = h + GOLDEN_RATIO;
if new_val > 1.0 {
new_val.fract()
} else {
new_val
}
}
/// This takes in an h, s, and v value of range [0, 1]
/// For explanation of what this does, see
/// https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative
fn hsv_to_rgb(hue: f32, saturation: f32, value: f32) -> (u8, u8, u8) {
fn hsv_helper(num: u32, hu: f32, sat: f32, val: f32) -> f32 {
let k = (num as f32 + hu * 6.0) % 6.0;
val - val * sat * float_max(float_min(k, float_min(4.1 - k, 1.1)), 0.0)
}
(
(hsv_helper(5, hue, saturation, value) * 255.0) as u8,
(hsv_helper(3, hue, saturation, value) * 255.0) as u8,
(hsv_helper(1, hue, saturation, value) * 255.0) as u8,
)
}
// Generate colours
let