diff options
author | ClementTsang <clementjhtsang@gmail.com> | 2019-12-27 22:39:25 -0500 |
---|---|---|
committer | ClementTsang <clementjhtsang@gmail.com> | 2019-12-27 22:39:25 -0500 |
commit | f7243bd78b571065bf1292d0d06e7736903c519a (patch) | |
tree | 19fd9a70fee79d28fa5071372b7c469f560154ae | |
parent | 7b0bea57f4befc08025d36cde72d763c89445a54 (diff) |
Refactoring (note graphs are shrunk for next commit) of canvas
-rw-r--r-- | src/app.rs | 6 | ||||
-rw-r--r-- | src/canvas.rs | 697 | ||||
-rw-r--r-- | src/main.rs | 13 |
3 files changed, 384 insertions, 332 deletions
@@ -45,11 +45,14 @@ pub struct App { pub use_dot: bool, pub show_help: bool, pub is_frozen: bool, + pub left_legend: bool, last_key_press: Instant, } impl App { - pub fn new(show_average_cpu: bool, temperature_type: temperature::TemperatureType, update_rate_in_milliseconds: u64, use_dot: bool) -> App { + pub fn new( + show_average_cpu: bool, temperature_type: temperature::TemperatureType, update_rate_in_milliseconds: u64, use_dot: bool, left_legend: bool, + ) -> App { App { process_sorting_type: processes::ProcessSorting::CPU, process_sorting_reverse: true, @@ -72,6 +75,7 @@ impl App { use_dot, show_help: false, is_frozen: false, + left_legend, last_key_press: Instant::now(), } } diff --git a/src/canvas.rs b/src/canvas.rs index cf54ff9d..287d47fd 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -1,8 +1,9 @@ use crate::{app, constants, utils::error, utils::gen_util::*}; use tui::{ backend, - layout::{Alignment, Constraint, Direction, Layout}, + 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, }; @@ -31,6 +32,8 @@ lazy_static! { Text::raw("n to sort by process 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)] @@ -44,8 +47,8 @@ pub struct CanvasData { pub disk_data: Vec<Vec<String>>, pub temp_sensor_data: Vec<Vec<String>>, pub process_data: Vec<Vec<String>>, + pub memory_labels: Vec<(u64, u64)>, pub mem_data: Vec<(f64, f64)>, - pub mem_values: Vec<(u64, u64)>, pub swap_data: Vec<(f64, f64)>, pub cpu_data: Vec<(String, Vec<(f64, f64)>)>, } @@ -98,9 +101,6 @@ fn gen_n_colours(num_to_gen: i32) -> Vec<Color> { } pub fn draw_data<B: backend::Backend>(terminal: &mut Terminal<B>, app_state: &mut app::App, canvas_data: &CanvasData) -> error::Result<()> { - let border_style: Style = Style::default().fg(BORDER_STYLE_COLOUR); - let highlighted_border_style: Style = Style::default().fg(HIGHLIGHTED_BORDER_STYLE_COLOUR); - terminal.autoresize()?; terminal.draw(|mut f| { if app_state.show_help { @@ -149,361 +149,400 @@ pub fn draw_data<B: backend::Backend>(terminal: &mut Terminal<B>, app_state: &mu .split(vertical_chunks[2]); // Component specific chunks - let _cpu_chunk = Layout::default() + let cpu_chunk = Layout::default() .direction(Direction::Horizontal) .margin(0) - .constraints([Constraint::Percentage(90), Constraint::Percentage(10)].as_ref()) + .constraints( + if app_state.left_legend { + [Constraint::Percentage(10), Constraint::Percentage(90)] + } else { + [Constraint::Percentage(90), Constraint::Percentage(10)] + } + .as_ref(), + ) .split(vertical_chunks[0]); - let _mem_chunk = Layout::default() + let mem_chunk = Layout::default() .direction(Direction::Horizontal) .margin(0) - .constraints([Constraint::Percentage(90), Constraint::Percentage(10)].as_ref()) + .constraints( + if app_state.left_legend { + [Constraint::Percentage(10), Constraint::Percentage(90)] + } else { + [Constraint::Percentage(90), Constraint::Percentage(10)] + } + .as_ref(), + ) .split(middle_chunks[0]); - let _network_chunk = Layout::default() + let network_chunk = Layout::default() .direction(Direction::Horizontal) .margin(0) - .constraints([Constraint::Percentage(90), Constraint::Percentage(10)].as_ref()) + .constraints( + if app_state.left_legend { + [Constraint::Percentage(10), Constraint::Percentage(90)] + } else { + [Constraint::Percentage(90), Constraint::Percentage(10)] + } + .as_ref(), + ) .split(bottom_chunks[0]); - // 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, constants::TIME_STARTS_FROM as f64 * 10.0]); - let y_axis = Axis::default() - .style(Style::default().fg(GRAPH_COLOUR)) - .bounds([-0.5, 100.5]) - .labels(&["0%", "100%"]); - - let mut dataset_vector: Vec<Dataset> = Vec::new(); - - for (i, cpu) in canvas_data.cpu_data.iter().enumerate() { - let mut avg_cpu_exist_offset = 0; - if app_state.show_average_cpu { - if i == 0 { - // Skip, we want to render the average cpu last! - continue; - } else { - avg_cpu_exist_offset = 1; - } - } + // Default chunk index based on left or right legend setting + let legend_index = if app_state.left_legend { 0 } else { 1 }; + let graph_index = if app_state.left_legend { 1 } else { 0 }; - dataset_vector.push( - Dataset::default() - .name(&cpu.0) - .marker(if app_state.use_dot { Marker::Dot } else { Marker::Braille }) - .style(Style::default().fg(COLOUR_LIST[(i - avg_cpu_exist_offset) % COLOUR_LIST.len()])) - .data(&(cpu.1)), - ); - } + // Set up blocks and their components + // CPU graph + draw_cpu_graph(&mut f, &app_state, &canvas_data.cpu_data, cpu_chunk[graph_index]); - if !canvas_data.cpu_data.is_empty() && app_state.show_average_cpu { - // Unwrap should be safe here, this assumes that the cpu_data vector is populated... - dataset_vector.push( - Dataset::default() - .name(&canvas_data.cpu_data.first().unwrap().0) - .marker(if app_state.use_dot { Marker::Dot } else { Marker::Braille }) - .style(Style::default().fg(COLOUR_LIST[(canvas_data.cpu_data.len() - 1) % COLOUR_LIST.len()])) - .data(&(canvas_data.cpu_data.first().unwrap().1)), - ); - } - - Chart::default() - .block( - Block::default() - .title("CPU Usage") - .borders(Borders::ALL) - .border_style(match app_state.current_application_position { - app::ApplicationPosition::CPU => highlighted_border_style, - _ => border_style, - }), - ) - .x_axis(x_axis) - .y_axis(y_axis) - .datasets(&dataset_vector) - .render(&mut f, vertical_chunks[0]); - } + // CPU label //Memory usage graph - { - let x_axis: Axis<String> = Axis::default() - .style(Style::default().fg(GRAPH_COLOUR)) - .bounds([0.0, constants::TIME_STARTS_FROM as f64 * 10.0]); - let y_axis = Axis::default() - .style(Style::default().fg(GRAPH_COLOUR)) - .bounds([-0.5, 100.5]) // Offset as the zero value isn't drawn otherwise... - .labels(&["0%", "100%"]); - - let mem_name = "RAM:".to_string() - + &format!("{:3}%", (canvas_data.mem_data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64)) - + &format!( - " {:.1}GB/{:.1}GB", - canvas_data.mem_values.first().unwrap_or(&(0, 0)).0 as f64 / 1024.0, - canvas_data.mem_values.first().unwrap_or(&(0, 0)).1 as f64 / 1024.0 - ); - let swap_name: String; - - let mut mem_canvas_vec: Vec<Dataset> = vec![Dataset::default() - .name(&mem_name) - .marker(if app_state.use_dot { Marker::Dot } else { Marker::Braille }) - .style(Style::default().fg(COLOUR_LIST[0])) - .data(&canvas_data.mem_data)]; - - if !(&canvas_data.swap_data).is_empty() { - if let Some(last_canvas_result) = (&canvas_data.swap_data).last() { - if last_canvas_result.1 >= 0.0 { - swap_name = "SWP:".to_string() - + &format!("{:3}%", (canvas_data.swap_data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64)) - + &format!( - " {:.1}GB/{:.1}GB", - canvas_data.mem_values[1].0 as f64 / 1024.0, - canvas_data.mem_values[1].1 as f64 / 1024.0 - ); - mem_canvas_vec.push( - Dataset::default() - .name(&swap_name) - .marker(if app_state.use_dot { Marker::Dot } else { Marker::Braille }) - .style(Style::default().fg(COLOUR_LIST[1])) - .data(&canvas_data.swap_data), - ); - } - } - } + draw_memory_graph(&mut f, &app_state, &canvas_data.mem_data, &canvas_data.swap_data, mem_chunk[graph_index]); - Chart::default() - .block( - Block::default() - .title("Memory Usage") - .borders(Borders::ALL) - .border_style(match app_state.current_application_position { - app::ApplicationPosition::MEM => highlighted_border_style, - _ => border_style, - }), - ) - .x_axis(x_axis) - .y_axis(y_axis) - .datasets(&mem_canvas_vec) - .render(&mut f, middle_chunks[0]); - } + // Memory label // Temperature table - { - let num_rows = i64::from(middle_divided_chunk_2[0].height) - 4; - let start_position = get_start_position( - num_rows, - &(app_state.scroll_direction), - &mut app_state.previous_temp_position, - &mut app_state.currently_selected_temperature_position, - ); - - let sliced_vec: Vec<Vec<String>> = (&canvas_data.temp_sensor_data[start_position as usize..]).to_vec(); - let mut disk_counter = 0; - - let temperature_rows = sliced_vec.iter().map(|disk| { - Row::StyledData( - disk.iter(), - if disk_counter == app_state.currently_selected_temperature_position - start_position { - disk_counter = -1; - Style::default().fg(Color::Black).bg(Color::Cyan) - } else { - if disk_counter >= 0 { - disk_counter += 1; - } - Style::default().fg(TEXT_COLOUR) - }, - ) - }); - - let width = f64::from(middle_divided_chunk_2[0].width); - Table::new(["Sensor", "Temp"].iter(), temperature_rows) - .block( - Block::default() - .title("Temperatures") - .borders(Borders::ALL) - .border_style(match app_state.current_application_position { - app::ApplicationPosition::TEMP => highlighted_border_style, - _ => border_style, - }), - ) - .header_style(Style::default().fg(Color::LightBlue)) - .widths(&[Constraint::Length((width * 0.45) as u16), Constraint::Length((width * 0.4) as u16)]) - .render(&mut f, middle_divided_chunk_2[0]); - } + draw_temp_table(&mut f, app_state, &canvas_data.temp_sensor_data, middle_divided_chunk_2[0]); // Disk usage table - { - let num_rows = i64::from(middle_divided_chunk_2[1].height) - 4; - let start_position = get_start_position( - num_rows, - &(app_state.scroll_direction), - &mut app_state.previous_disk_position, - &mut app_state.currently_selected_disk_position, - ); - - let sliced_vec: Vec<Vec<String>> = (&canvas_data.disk_data[start_position as usize..]).to_vec(); - let mut disk_counter = 0; - - let disk_rows = sliced_vec.iter().map(|disk| { - Row::StyledData( - disk.iter(), - if disk_counter == app_state.currently_selected_disk_position - start_position { - disk_counter = -1; - Style::default().fg(Color::Black).bg(Color::Cyan) - } else { - if disk_counter >= 0 { - disk_counter += 1; - } - Style::default().fg(TEXT_COLOUR) - }, - ) - }); - - // TODO: We may have to dynamically remove some of these table elements based on size... - let width = f64::from(middle_divided_chunk_2[1].width); - Table::new(["Disk", "Mount", "Used", "Total", "Free", "R/s", "W/s"].iter(), disk_rows) - .block( - Block::default() - .title("Disk Usage") - .borders(Borders::ALL) - .border_style(match app_state.current_application_position { - app::ApplicationPosition::DISK => highlighted_border_style, - _ => border_style, - }), - ) - .header_style(Style::default().fg(Color::LightBlue).modifier(Modifier::BOLD)) - .widths(&[ - Constraint::Length((width * 0.18).floor() as u16), - Constraint::Length((width * 0.14).floor() as u16), - Constraint::Length((width * 0.11).floor() as u16), - Constraint::Length((width * 0.11).floor() as u16), - Constraint::Length((width * 0.11).floor() as u16), - Constraint::Length((width * 0.11).floor() as u16), - Constraint::Length((width * 0.11).floor() as u16), - ]) - .render(&mut f, middle_divided_chunk_2[1]); - } + draw_disk_table(&mut f, app_state, &canvas_data.disk_data, middle_divided_chunk_2[1]); // Network graph - { - let x_axis: Axis<String> = Axis::default().style(Style::default().fg(GRAPH_COLOUR)).bounds([0.0, 600_000.0]); - let y_axis = Axis::default() - .style(Style::default().fg(GRAPH_COLOUR)) - .bounds([-0.5, 30_f64]) - .labels(&["0B", "1KiB", "1MiB", "1GiB"]); - Chart::default() - .block( - Block::default() - .title("Network") - .borders(Borders::ALL) - .border_style(match app_state.current_application_position { - app::ApplicationPosition::NETWORK => highlighted_border_style, - _ => border_style, - }), - ) - .x_axis(x_axis) - .y_axis(y_axis) - .datasets(&[ - Dataset::default() - .name(&(canvas_data.rx_display)) - .marker(if app_state.use_dot { Marker::Dot } else { Marker::Braille }) - .style(Style::default().fg(COLOUR_LIST[0])) - .data(&canvas_data.network_data_rx), - Dataset::default() - .name(&(canvas_data.tx_display)) - .marker(if app_state.use_dot { Marker::Dot } else { Marker::Braille }) - .style(Style::default().fg(COLOUR_LIST[1])) - .data(&canvas_data.network_data_tx), - Dataset::default().name(&(canvas_data.total_rx_display)), - Dataset::default().name(&(canvas_data.total_tx_display)), - ]) - .render(&mut f, bottom_chunks[0]); - } + draw_network_graph( + &mut f, + &app_state, + &canvas_data.network_data_rx, + &canvas_data.network_data_tx, + network_chunk[graph_index], + ); - // Processes table - { - let width = f64::from(bottom_chunks[1].width); - - // Admittedly this is kinda a hack... but we need to: - // * Scroll - // * Show/hide elements based on scroll position - // As such, we use a process_counter to know when we've hit the process we've currently scrolled to. We also need to move the list - we can - // do so by hiding some elements! - let num_rows = i64::from(bottom_chunks[1].height) - 4; - - let start_position = get_start_position( - num_rows, - &(app_state.scroll_direction), - &mut app_state.previous_process_position, - &mut app_state.currently_selected_process_position, - ); + // Network label - let sliced_vec: Vec<Vec<String>> = (&canvas_data.process_data[start_position as usize..]).to_vec(); - let mut process_counter = 0; - - let process_rows = sliced_vec.iter().map(|process| { - Row::StyledData( - process.iter(), - if process_counter == app_state.currently_selected_process_position - start_position { - process_counter = -1; - Style::default().fg(Color::Black).bg(Color::Cyan) - } else { - if process_counter >= 0 { - process_counter += 1; - } - Style::default().fg(TEXT_COLOUR) - }, - ) - }); - - { - use app::data_collection::processes::ProcessSorting; - let mut pid = "PID(p)".to_string(); - let mut name = "Name(n)".to_string(); - let mut cpu = "CPU%(c)".to_string(); - let mut mem = "Mem%(m)".to_string(); - - let direction_val = if app_state.process_sorting_reverse { - "⯆".to_string() - } else { - "⯅".to_string() - }; - - match app_state.process_sorting_type { - ProcessSorting::CPU => cpu += &direction_val, - ProcessSorting::MEM => mem += &direction_val, - ProcessSorting::PID => pid += &direction_val, - ProcessSorting::NAME => name += &direction_val, - }; - - Table::new([pid, name, cpu, mem].iter(), process_rows) - .block( - Block::default() - .title("Processes") - .borders(Borders::ALL) - .border_style(match app_state.current_application_position { - app::ApplicationPosition::PROCESS => highlighted_border_style, - _ => border_style, - }), - ) - .header_style(Style::default().fg(Color::LightBlue)) - .widths(&[ - Constraint::Length((width * 0.2) as u16), - Constraint::Length((width * 0.35) as u16), - Constraint::Length((width * 0.2) as u16), - Constraint::Length((width * 0.2) as u16), - ]) - .render(&mut f, bottom_chunks[1]); - } - } + // Processes table + draw_processes_table(&mut f, app_state, &canvas_data.process_data, bottom_chunks[1]); } })?; Ok(()) } +fn draw_cpu_graph<B: backend::Backend>(f: &mut Frame<B>, app_state: &app::App, cpu_data: &[(String, Vec<(f64, f64)>)], draw_loc: Rect) { + // CPU usage graph + + let x_axis: Axis<String> = Axis::default() + .style(Style::default().fg(GRAPH_COLOUR)) + .bounds([0.0, constants::TIME_STARTS_FROM as f64 * 10.0]); + let y_axis = Axis::default() + .style(Style::default().fg(GRAPH_COLOUR)) + .bounds([-0.5, 100.5]) + .labels(&["0%", "100%"]); + + let mut dataset_vector: Vec<Dataset> = Vec::new(); + + for (i, cpu) in cpu_data.iter().enumerate() { + let mut avg_cpu_exist_offset = 0; + if app_state.show_average_cpu { + if i == 0 { + // Skip, we want to render the average cpu last! + continue; + } else { + avg_cpu_exist_offset = 1; + } + } + + dataset_vector.push( + Dataset::default() + .name(&cpu.0) + .marker(if app_state.use_dot { Marker::Dot } else { Marker::Braille }) + .style(Style::default().fg(COLOUR_LIST[(i - avg_cpu_exist_offset) % COLOUR_LIST.len()])) + .data(&(cpu.1)), + ); + } + + if !cpu_data.is_empty() && app_state.show_average_cpu { + // Unwrap should be safe here, this assumes that the cpu_data vector is populated... + dataset_vector.push( + Dataset::default() + //.name(&cpu_data.first().unwrap().0) + .marker(if app_state.use_dot { Marker::Dot } else { Marker::Braille }) + .style(Style::default().fg(COLOUR_LIST[(cpu_data.len() - 1) % COLOUR_LIST.len()])) + .data(&(cpu_data.first().unwrap().1)), + ); + } + + Chart::default() + .block( + Block::default() + .title("CPU Usage") + .borders(Borders::ALL) + .border_style(match app_state.current_application_position { + app::ApplicationPosition::CPU => *CANVAS_HIGHLIGHTED_BORDER_STYLE, + _ => *CANVAS_BORDER_STYLE, + }), + ) + .x_axis(x_axis) + .y_axis(y_axis) + .datasets(&dataset_vector) + .render(f, draw_loc); +} + +fn draw_cpu_legend() {} + +fn draw_memory_graph<B: backend::Backend>(f: &mut Frame<B>, app_state: &app::App, swap_data: &[(f64, f64)], mem_data: &[(f64, f64)], draw_loc: Rect) { + let x_axis: Axis<String> = Axis::default() + .style(Style::default().fg(GRAPH_COLOUR)) + .bounds([0.0, constants::TIME_STARTS_FROM as f64 * 10.0]); + let y_axis = Axis::default() + .style(Style::default().fg(GRAPH_COLOUR)) + .bounds([-0.5, 100.5]) // Offset as the zero value isn't drawn otherwise... + .labels(&["0%", "100%"]); + + let mut mem_canvas_vec: Vec<Dataset> = vec![Dataset::default() + //.name(&mem_name) + .marker(if app_state.use_dot { Marker::Dot } else { Marker::Braille }) + .style(Style::default().fg(COLOUR_LIST[0])) + .data(&mem_data)]; + + if !(&swap_data).is_empty() { + if let Some(last_canvas_result) = (&swap_data).last() { + if last_canvas_result.1 >= 0.0 { + mem_canvas_vec.push( + Dataset::default() + //.name(&swap_name) + .marker(if app_state.use_dot { Marker::Dot } else { Marker::Braille }) + .style(Style::default().fg(COLOUR_LIST[1])) + .data(&swap_data), + ); + } + } + } + + Chart::default() + .block( + Block::default() + .title("Memory Usage") + .borders(Borders::ALL) + .border_style(match app_state.current_application_position { + app::ApplicationPosition::MEM => *CANVAS_HIGHLIGHTED_BORDER_STYLE, + _ => *CANVAS_BORDER_STYLE, + }), + ) + .x_axis(x_axis) + .y_axis(y_axis) + .datasets(&mem_canvas_vec) + .render(f, draw_loc); +} + +fn draw_memory_legend() {} + +fn draw_network_graph<B: backend::Backend>( + f: &mut Frame<B>, app_state: &app::App, network_data_rx: &[(f64, f64)], network_data_tx: &[(f64, f64)], draw_loc: Rect, +) { + let x_axis: Axis<String> = Axis::default().style(Style::default().fg(GRAPH_COLOUR)).bounds([0.0, 600_000.0]); + let y_axis = Axis::default() + .style(Style::default().fg(GRAPH_COLOUR)) + .bounds([-0.5, 30_f64]) + .labels(&["0B", "1KiB", "1MiB", "1GiB"]); + Chart::default() + .block( + Block::default() + .title("Network") + .borders(Borders::ALL) + .border_style(match app_state.current_application_position { + app::ApplicationPosition::NETWORK => *CANVAS_HIGHLIGHTED_BORDER_STYLE, + _ => *CANVAS_BORDER_STYLE, + }), + ) + .x_axis(x_axis) + .y_axis(y_axis) + .datasets(&[ + Dataset::default() + .marker(if app_state.use_dot { Marker::Dot } else { Marker::Braille }) + .style(Style::default().fg(COLOUR_LIST[0])) + .data(&network_data_rx), + Dataset::default() + .marker(if app_state.use_dot { Marker::Dot } else { Marker::Braille }) + .style(Style::default().fg(COLOUR_LIST[1])) + .data(&network_data_tx), + ]) + .render(f, draw_loc); +} + +fn draw_network_legend() {} + +fn draw_temp_table<B: backend::Backend>(f: &mut Frame<B>, app_state: &mut app::App, temp_sensor_data: &[Vec<String>], draw_loc: Rect) { + let num_rows = i64::from(draw_loc.height) - 4; + let start_position = get_start_position( + num_rows, + &(app_state.scroll_direction), + &mut app_state.previous_temp_position, + &mut app_state.currently_selected_temperature_position, + ); + + let sliced_vec: Vec<Vec<String>> = (&temp_sensor_data[start_position as usize..]).to_vec(); + let mut disk_counter = 0; + + let temperature_rows = sliced_vec.iter().map(|disk| { + Row::StyledData( + disk.iter(), + if disk_counter == app_state.currently_selected_temperature_position - start_position { + disk_counter = -1; + Style::default().fg(Color::Black).bg(Color::Cyan) + } else { + if disk_counter >= 0 { + disk_counter += 1; + } + Style::default().fg(TEXT_COLOUR) + }, + ) + }); + + let width = f64::from(draw_loc.width); + Table::new(["Sensor", "Temp"].iter(), temperature_rows) + .block( + Block::default() + .title("Temperatures") + .borders(Borders::ALL) + .border_style(match app_state.current_application_position { + app::ApplicationPosition::TEMP => *CANVAS_HIGHLIGHTED_BORDER_STYLE, + _ => *CANVAS_BORDER_STYLE, + }), + ) + .header_style(Style::default().fg(Color::LightBlue)) + .widths(&[Constraint::Length((width * 0.45) as u16), Constraint::Length((width * 0.4) as u16)]) + .render(f, draw_loc); +} + +fn draw_disk_table<B: backend::Backend>(f: &mut Frame<B>, app_state: &mut app::App, disk_data: &[Vec<String>], draw_loc: Rect) { + let num_rows = i64::from(draw_loc.height) - 4; + let start_position = get_start_position( + num_rows, + &(app_state.scroll_direction), + &mut app_state.previous_disk_position, + &mut app_state.currently_selected_disk_position, + ); + + let sliced_vec: Vec<Vec<String>> = (&disk_data[start_position as usize..]).to_vec(); + let mut disk_counter = 0; + + let disk_rows = sliced_vec.iter().map(|disk| { + Row::StyledData( + disk.iter(), + if disk_counter == app_state.currently_selected_disk_position - start_position { + disk_counter = -1; + Style::default().fg(Color::Black).bg(Color::Cyan) + } else { + if disk_counter >= 0 { + disk_counter += 1; + } + Style::default().fg(TEXT_COLOUR) + }, + ) + }); + + // TODO: We may have to dynamically remove some of these table elements based on size... + let width = f64::from(draw_loc.width); + Table::new(["Disk", "Mount", "Used", "Total", "Free", "R/s", "W/s"].iter(), disk_rows) + .block( + Block::default() + .title("Disk Usage") + .borders(Borders::ALL) + .border_style(match app_state.current_application_position { + app::ApplicationPosition::DISK => *CANVAS_HIGHLIGHTED_BORDER_STYLE, + _ => *CANVAS_BORDER_STYLE, + }), + ) + .header_style(Style::default().fg(Color::LightBlue).modifier(Modifier::BOLD)) + .widths(&[ + Constraint::Length((width * 0.18).floor() as u16), + Constraint::Length((width * 0.14).floor() as u16), + Constraint::Length((width * 0.11).floor() as u16), + Constraint::Length((width * 0.11).floor() as u16), + Constraint::Length((width * 0.11).floor() as u16), + Constraint::Length((width * 0.11).floor() as u16), + Constraint::Length((width * 0.11).floor() as u16), + ]) + .render(f, draw_loc); +} + +fn draw_processes_table<B: backend::Backend>(f: &mut Frame<B>, app_state: &mut app::App, process_data: &[Vec<String>], draw_loc: Rect) { + let width = f64::from(draw_loc.width); + + // Admittedly this is kinda a hack... but we need to: + // * Scroll + // * Show/hide elements based on scroll position + // As such, we use a process_counter to know when we've hit the process we've currently scrolled to. We also need to move the list - we can + // do so by hiding some elements! + let num_rows = i64::from(draw_loc.height) - 4; + + let start_position = get_start_position( + num_rows, + &(app_state.scroll_direction), + &mut app_state.previous_process_position, + &mut app_state.currently_selected_process_position, + ); + + let sliced_vec: Vec<Vec<String>> = (&process_data[start_position as usize..]).to_vec(); + let mut process_counter = 0; + + let process_rows = sliced_vec.iter().map(|process| { + Row::StyledData( + process.iter(), + if process_counter == app_state.currently_selected_process_position - start_position { + process_counter = -1; + Style::default().fg(Color::Black).bg(Color::Cyan) + } else { + if process_counter >= 0 { + process_counter += 1; + } + Style::default().fg(TEXT_COLOUR) + }, + ) + }); + + { + use app::data_collection::processes::ProcessSorting; + let mut pid = "PID(p)".to_string(); + let mut name = "Name(n)".to_string(); + let mut cpu = "CPU%(c)".to_string(); + let mut mem = "Mem%(m)".to_string(); + + let direction_val = if app_state.process_sorting_reverse { + "⯆".to_string() + } else { + "⯅".to_string() + }; + + match app_state.process_sorting_type { + ProcessSorting::CPU => cpu += &direction_val, + ProcessSorting::MEM => mem += &direction_val, + ProcessSorting::PID => pid += &direction_val, + ProcessSorting::NAME => name += &direction_val, + }; + + Table::new([pid, name, cpu, mem].iter(), process_rows) + .block( + Block::default() + .title("Processes") + .borders(Borders::ALL) + .border_style(match app_state.current_application_position { + app::ApplicationPosition::PROCESS => *CANVAS_HIGHLIGHTED_BORDER_STYLE, + _ => *CANVAS_BORDER_STYLE, + }), + ) + .header_style(Style::default().fg(Color::LightBlue)) + .widths(&[ + Constraint::Length((width * 0.2) as u16), + Constraint::Length((width * 0.35) as u16), + Constraint::Length((width * 0.2) as u16), + Constraint::Length((width * 0.2) as u16), + ]) + .render(f, draw_loc); + } +} + fn get_start_position( num_rows: i64, scroll_direction: &app::ScrollDirection, previous_position: &mut i64, currently_selected_position: &mut i64, ) -> i64 { diff --git a/src/main.rs b/src/main.rs index 465c2a1f..fd8536e0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,6 +65,7 @@ fn main() -> error::Result<()> { (@arg KELVIN : -k --kelvin "Sets the temperature type to Kelvin.") ) (@arg RATE_MILLIS: -r --rate +takes_value "Sets a refresh rate in milliseconds; the minimum is 250ms, defaults to 1000ms. Smaller values may take more resources.") + (@arg LEFT_LEGEND: -l --left_legend "Puts table legends on the left side rather than the default right side.") //(@arg CONFIG_LOCATION: -co --config +takes_value "Sets the location of the config file. Expects a config file in the JSON format.") //(@arg BASIC_MODE: -b --basic "Sets bottom to basic mode, not showing graphs and only showing basic tables.") ) @@ -95,6 +96,7 @@ fn main() -> error::Result<()> { utils::logging::init_logger()?; } + // Set other settings let temperature_type = if matches.is_present("FAHRENHEIT") { data_collection::temperature::TemperatureType::Fahrenheit } else if matches.is_present("KELVIN") { @@ -104,9 +106,16 @@ fn main() -> error::Result<()> { }; let show_average_cpu = matches.is_present("AVG_CPU"); let use_dot = matches.is_present("DOT_MARKER"); + let left_legend = matches.is_present("LEFT_LEGEND"); // Create "app" struct, which will control most of the program and store settings/state - let mut app = app::App::new(show_average_cpu, temperature_type, update_rate_in_milliseconds as u64, use_dot); + let mut app = app::App::new( + show_average_cpu, + temperature_type, + update_rate_in_milliseconds as u64, + use_dot, + left_legend, + ); // Set up up tui and crossterm let mut stdout = stdout(); @@ -278,7 +287,7 @@ fn main() -> error::Result<()> { canvas_data.temp_sensor_data = update_temp_row(&app.data, &app.temperature_type); canvas_data.process_data = update_process_row(&app.data); canvas_data.mem_data = update_mem_data_points(&app.data); - canvas_data.mem_values = update_mem_data_values(&app.data); + canvas_data.memory_labels = update_mem_data_values(&app.data); canvas_data.swap_data = update_swap_data_points(&app.data); canvas_data.cpu_data = update_cpu_data_points(app.show_average_cpu, &app.data); |