diff options
author | ClementTsang <clementjhtsang@gmail.com> | 2020-01-01 17:55:15 -0500 |
---|---|---|
committer | ClementTsang <clementjhtsang@gmail.com> | 2020-01-01 17:55:15 -0500 |
commit | 2c138378fca780d03fc4d45efd5ab7046100c8d9 (patch) | |
tree | eb8d39317e9f31e20ba158946ce01c9be724b2c6 /src | |
parent | 4e6e32a0ea2a6592fb8a5c4231c95c273d491745 (diff) |
Add scrolling to CPU list.
Diffstat (limited to 'src')
-rw-r--r-- | src/app.rs | 29 | ||||
-rw-r--r-- | src/canvas.rs | 84 | ||||
-rw-r--r-- | src/data_conversion.rs | 42 |
3 files changed, 111 insertions, 44 deletions
@@ -29,13 +29,15 @@ pub struct App { pub process_sorting_reverse: bool, pub to_be_resorted: bool, // Positioning + pub scroll_direction: ScrollDirection, pub currently_selected_process_position: i64, pub currently_selected_disk_position: i64, pub currently_selected_temperature_position: i64, - pub scroll_direction: ScrollDirection, + pub currently_selected_cpu_table_position: i64, pub previous_disk_position: i64, pub previous_temp_position: i64, pub previous_process_position: i64, + pub previous_cpu_table_position: i64, pub temperature_type: temperature::TemperatureType, pub update_rate_in_milliseconds: u64, pub show_average_cpu: bool, @@ -60,17 +62,19 @@ impl App { process_sorting_type: processes::ProcessSorting::CPU, process_sorting_reverse: true, to_be_resorted: false, - currently_selected_process_position: 0, - currently_selected_disk_position: 0, - currently_selected_temperature_position: 0, temperature_type, update_rate_in_milliseconds, show_average_cpu, current_application_position: ApplicationPosition::Process, scroll_direction: ScrollDirection::DOWN, + currently_selected_process_position: 0, + currently_selected_disk_position: 0, + currently_selected_temperature_position: 0, + currently_selected_cpu_table_position: 0, previous_process_position: 0, previous_disk_position: 0, previous_temp_position: 0, + previous_cpu_table_position: 0, data: data_collection::Data::default(), awaiting_second_char: false, second_char: ' ', @@ -274,6 +278,7 @@ impl App { ApplicationPosition::Process => self.change_process_position(-1), ApplicationPosition::Temp => self.change_temp_position(-1), ApplicationPosition::Disk => self.change_disk_position(-1), + ApplicationPosition::Cpu => self.change_cpu_table_position(-1), // TODO: Temporary _ => {} } self.scroll_direction = ScrollDirection::UP; @@ -285,12 +290,28 @@ impl App { ApplicationPosition::Process => self.change_process_position(1), ApplicationPosition::Temp => self.change_temp_position(1), ApplicationPosition::Disk => self.change_disk_position(1), + ApplicationPosition::Cpu => self.change_cpu_table_position(1), // TODO: Temporary _ => {} } self.scroll_direction = ScrollDirection::DOWN; self.reset_multi_tap_keys(); } + fn change_cpu_table_position(&mut self, num_to_change_by: i64) { + if let Some(cpu_package) = self.data.list_of_cpu_packages.last() { + if self.currently_selected_cpu_table_position + num_to_change_by >= 0 + && self.currently_selected_cpu_table_position + num_to_change_by + < if self.show_average_cpu { + cpu_package.cpu_vec.len() + } else { + cpu_package.cpu_vec.len() - 1 + } as i64 + { + self.currently_selected_cpu_table_position += num_to_change_by; + } + } + } + fn change_process_position(&mut self, num_to_change_by: i64) { if self.currently_selected_process_position + num_to_change_by >= 0 && self.currently_selected_process_position + num_to_change_by < self.data.list_of_processes.len() as i64 diff --git a/src/canvas.rs b/src/canvas.rs index fc129fa8..080b8696 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -1,10 +1,10 @@ use crate::{app, constants, utils::error, utils::gen_util::*}; use tui::{ backend, - layout::{Alignment, Constraint, Corner, Direction, Layout, Rect}, + layout::{Alignment, Constraint, Direction, Layout, Rect}, style::{Color, Modifier, Style}, terminal::Frame, - widgets::{Axis, Block, Borders, Chart, Dataset, List, Marker, Paragraph, Row, Table, Text, Widget}, + widgets::{Axis, Block, Borders, Chart, Dataset, Marker, Paragraph, Row, Table, Text, Widget}, Terminal, }; @@ -81,14 +81,7 @@ fn gen_n_colours(num_to_gen: i32) -> Vec<Color> { } // Generate colours - let mut colour_vec: Vec<Color> = vec![ - Color::LightBlue, - Color::LightYellow, - Color::Red, - Color::Green, - Color::LightCyan, - Color::LightMagenta, - ]; + let mut colour_vec: Vec<Color> = vec![Color::LightCyan, Color::LightYellow, Color::Red, Color::Green, Color::LightMagenta]; let mut h: f32 = 0.4; // We don't need random colours... right? for _i in 0..num_to_gen { @@ -183,8 +176,8 @@ pub fn draw_data<B: backend::Backend>(terminal: &mut Terminal<B>, app_state: &mu // CPU graph draw_cpu_graph(&mut f, &app_state, &canvas_data.cpu_data, cpu_chunk[graph_index]); - // CPU label - draw_cpu_legend(&mut f, &app_state, &canvas_data.cpu_data, cpu_chunk[legend_index]); + // CPU legend + draw_cpu_legend(&mut f, app_state, &canvas_data.cpu_data, cpu_chunk[legend_index]); //Memory usage graph draw_memory_graph( @@ -219,6 +212,16 @@ pub fn draw_data<B: backend::Backend>(terminal: &mut Terminal<B>, app_state: &mu canvas_data.total_tx_display.clone(), network_chunk[1], ); + } else { + draw_network_labels( + &mut f, + app_state, + canvas_data.rx_display.clone(), + canvas_data.tx_display.clone(), + "N/A".to_string(), + "N/A".to_string(), + network_chunk[1], + ); } // Temperature table @@ -293,14 +296,42 @@ fn draw_cpu_graph<B: backend::Backend>(f: &mut Frame<B>, app_state: &app::App, c .render(f, draw_loc); } -fn draw_cpu_legend<B: backend::Backend>(f: &mut Frame<B>, app_state: &app::App, cpu_data: &[(String, Vec<(f64, f64)>)], draw_loc: Rect) { - let mut itx = 0; - let label_map = cpu_data.iter().map(|cpu| { - itx += 1; - Text::styled(&cpu.0, Style::default().fg(COLOUR_LIST[(itx - 1) % COLOUR_LIST.len()])) +fn draw_cpu_legend<B: backend::Backend>(f: &mut Frame<B>, app_state: &mut app::App, cpu_data: &[(String, Vec<(f64, f64)>)], 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_cpu_table_position, + &mut app_state.currently_selected_cpu_table_position, + ); + + let sliced_cpu_data = (&cpu_data[start_position as usize..]).to_vec(); + let mut stringified_cpu_data: Vec<Vec<String>> = Vec::new(); + + for cpu in sliced_cpu_data { + if let Some(cpu_data) = cpu.1.last() { + stringified_cpu_data.push(vec![cpu.0.clone(), format!("{:.0}%", cpu_data.1.round())]); + } + } + + let mut cpu_row_counter = 0; + + let cpu_rows = stringified_cpu_data.iter().enumerate().map(|(itx, cpu_string_row)| { + Row::StyledData( + cpu_string_row.iter(), + if cpu_row_counter == app_state.currently_selected_cpu_table_position - start_position { + cpu_row_counter = -1; + Style::default().fg(Color::Black).bg(Color::Cyan) + } else { + if cpu_row_counter >= 0 { + cpu_row_counter += 1; + } + Style::default().fg(COLOUR_LIST[itx % COLOUR_LIST.len()]) + }, + ) }); - List::new(label_map) + Table::new(["CPU", "Use%"].iter(), cpu_rows) .block( Block::default() .borders(Borders::ALL) @@ -309,7 +340,8 @@ fn draw_cpu_legend<B: backend::Backend>(f: &mut Frame<B>, app_state: &app::App, _ => *CANVAS_BORDER_STYLE, }), ) - .start_corner(Corner::TopLeft) + .header_style(Style::default().fg(Color::LightBlue)) + .widths(&[Constraint::Percentage(50), Constraint::Percentage(50)]) .render(f, draw_loc); } @@ -446,17 +478,17 @@ fn draw_temp_table<B: backend::Backend>(f: &mut Frame<B>, app_state: &mut app::A ); let sliced_vec: Vec<Vec<String>> = (&temp_sensor_data[start_position as usize..]).to_vec(); - let mut disk_counter = 0; + let mut temp_row_counter = 0; - let temperature_rows = sliced_vec.iter().map(|disk| { + let temperature_rows = sliced_vec.iter().map(|temp_row| { Row::StyledData( - disk.iter(), - if disk_counter == app_state.currently_selected_temperature_position - start_position { - disk_counter = -1; + temp_row.iter(), + if temp_row_counter == app_state.currently_selected_temperature_position - start_position { + temp_row_counter = -1; Style::default().fg(Color::Black).bg(Color::Cyan) } else { - if disk_counter >= 0 { - disk_counter += 1; + if temp_row_counter >= 0 { + temp_row_counter += 1; } Style::default().fg(TEXT_COLOUR) }, diff --git a/src/data_conversion.rs b/src/data_conversion.rs index bcf464f1..2648b96a 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -5,6 +5,15 @@ use crate::{ }; use constants::*; +pub struct ConvertedNetworkData { + pub rx: Vec<(f64, f64)>, + pub tx: Vec<(f64, f64)>, + pub rx_display: String, + pub tx_display: String, + pub total_rx_display: String, + pub total_tx_display: String, +} + pub fn update_temp_row(app_data: &data_collection::Data, temp_type: &data_collection::temperature::TemperatureType) -> Vec<Vec<String>> { let mut sensor_vector: Vec<Vec<String>> = Vec::new(); @@ -103,6 +112,7 @@ pub fn update_process_row(app_data: &data_collection::Data) -> Vec<Vec<String>> process_vector } +// TODO: You should really make this a struct... pub fn update_cpu_data_points(show_avg_cpu: bool, app_data: &data_collection::Data) -> Vec<(String, Vec<(f64, f64)>)> { let mut cpu_data_vector: Vec<(String, Vec<(f64, f64)>)> = Vec::new(); let mut cpu_collection: Vec<Vec<(f64, f64)>> = Vec::new(); @@ -141,15 +151,28 @@ pub fn update_cpu_data_points(show_avg_cpu: bool, app_data: &data_collection::Da // Finally, add it all onto the end for (i, data) in cpu_collection.iter().enumerate() { if !app_data.list_of_cpu_packages.is_empty() { + // Commented out: this version includes the percentage in the label... + // cpu_data_vector.push(( + // // + 1 to skip total CPU if show_avg_cpu is false + // format!( + // "{:4}: ", + // &*(app_data.list_of_cpu_packages.last().unwrap().cpu_vec[i + if show_avg_cpu { 0 } else { 1 }].cpu_name) + // ) + // .to_uppercase() + &format!("{:3}%", (data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64)), + // data.clone(), + // )) cpu_data_vector.push(( - // + 1 to skip total CPU if show_avg_cpu is false format!( - "{:4}: ", - &*(app_data.list_of_cpu_packages.last().unwrap().cpu_vec[i + if show_avg_cpu { 0 } else { 1 }].cpu_name) + "{} ", + if show_avg_cpu && i == 0 { + "AVG" + } else { + &*(app_data.list_of_cpu_packages.last().unwrap().cpu_vec[i + if show_avg_cpu { 0 } else { 1 }].cpu_name) + } ) - .to_uppercase() + &format!("{:3}%", (data.last().unwrap_or(&(0_f64, 0_f64)).1.round() as u64)), + .to_uppercase(), data.clone(), - )) + )); } } } @@ -217,15 +240,6 @@ fn convert_mem_data(mem_data: &[data_collection::mem::MemData]) -> Vec<(f64, f64 result } -pub struct ConvertedNetworkData { - pub rx: Vec<(f64, f64)>, - pub tx: Vec<(f64, f64)>, - pub rx_display: String, - pub tx_display: String, - pub total_rx_display: String, - pub total_tx_display: String, -} - pub fn update_network_data_points(app_data: &data_collection::Data) -> ConvertedNetworkData { convert_network_data_points(&app_data.network) } |