From 5d0c8a9f32bb9ead677e6dbd0210e67b14e48152 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Wed, 15 Jan 2020 22:49:47 -0500 Subject: [PATCH] Note this is not 100% complete - there's some nasty code used to just get functionality down. Simple search option added, flag added, no way of switching in-program yet however. --- src/app.rs | 6 ++- src/canvas.rs | 1 - src/data_conversion.rs | 115 +++++++++++++++++++++++++---------------- src/main.rs | 26 +++++++--- 4 files changed, 93 insertions(+), 55 deletions(-) diff --git a/src/app.rs b/src/app.rs index 1993fb1e..6cf27af0 100644 --- a/src/app.rs +++ b/src/app.rs @@ -63,9 +63,10 @@ pub struct App { last_key_press: Instant, pub canvas_data: canvas::CanvasData, enable_grouping: bool, - enable_searching: bool, + enable_searching: bool, // TODO: [OPT] group together? current_search_query: String, searching_pid: bool, + pub use_simple: bool, current_regex: std::result::Result, } @@ -109,7 +110,8 @@ impl App { enable_searching: false, current_search_query: String::default(), searching_pid: false, - current_regex: BASE_REGEX.clone(), //TODO: [OPT] seems like a thing we can switch to lt for if not fast + use_simple: false, + current_regex: BASE_REGEX.clone(), //TODO: [OPT] seems like a thing we can switch to lifetimes to avoid cloning } } diff --git a/src/canvas.rs b/src/canvas.rs index 1352b1cd..de27ed45 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -319,7 +319,6 @@ pub fn draw_data( ) .split(vertical_chunks[0]); - debug!("Height: {}", bottom_chunks[0].height); let network_chunk = Layout::default() .direction(Direction::Vertical) .margin(0) diff --git a/src/data_conversion.rs b/src/data_conversion.rs index 1d09b1b4..5def8bf0 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -140,7 +140,49 @@ pub fn update_disk_row(app_data: &data_collection::Data) -> Vec> { disk_vector } -pub fn update_process_row( +pub fn simple_update_process_row( + app_data: &data_collection::Data, matching_string: &str, use_pid: bool, +) -> (Vec, Vec) { + let process_vector: Vec = app_data + .list_of_processes + .iter() + .filter(|process| { + if use_pid { + process + .pid + .to_string() + .to_ascii_lowercase() + .contains(matching_string) + } else { + process.name.to_ascii_lowercase().contains(matching_string) + } + }) + .map(|process| return_mapped_process(process, app_data)) + .collect::>(); + + let mut grouped_process_vector: Vec = Vec::new(); + if let Some(grouped_list_of_processes) = &app_data.grouped_list_of_processes { + grouped_process_vector = grouped_list_of_processes + .iter() + .filter(|process| { + if use_pid { + process + .pid + .to_string() + .to_ascii_lowercase() + .contains(matching_string) + } else { + process.name.to_ascii_lowercase().contains(matching_string) + } + }) + .map(|process| return_mapped_process(process, app_data)) + .collect::>(); + } + + (process_vector, grouped_process_vector) +} + +pub fn regex_update_process_row( app_data: &data_collection::Data, regex_matcher: &std::result::Result, use_pid: bool, ) -> (Vec, Vec) { @@ -158,26 +200,7 @@ pub fn update_process_row( true } }) - .map(|process| ConvertedProcessData { - pid: process.pid, - name: process.name.to_string(), - cpu_usage: format!("{:.1}%", process.cpu_usage_percent), - mem_usage: format!( - "{:.1}%", - if let Some(mem_usage) = process.mem_usage_percent { - mem_usage - } else if let Some(mem_usage_kb) = process.mem_usage_kb { - if let Some(mem_data) = app_data.memory.last() { - (mem_usage_kb / 1000) as f64 / mem_data.mem_total_in_mb as f64 * 100_f64 - } else { - 0_f64 - } - } else { - 0_f64 - } - ), - group: vec![], - }) + .map(|process| return_mapped_process(process, app_data)) .collect::>(); let mut grouped_process_vector: Vec = Vec::new(); @@ -195,36 +218,38 @@ pub fn update_process_row( true } }) - .map(|process| ConvertedProcessData { - pid: process.pid, - name: process.name.to_string(), - cpu_usage: format!("{:.1}%", process.cpu_usage_percent), - mem_usage: format!( - "{:.1}%", - if let Some(mem_usage) = process.mem_usage_percent { - mem_usage - } else if let Some(mem_usage_kb) = process.mem_usage_kb { - if let Some(mem_data) = app_data.memory.last() { - (mem_usage_kb / 1000) as f64 / mem_data.mem_total_in_mb as f64 * 100_f64 - } else { - 0_f64 - } - } else { - 0_f64 - } - ), - group: if let Some(pid_vec) = &process.pid_vec { - pid_vec.to_vec() - } else { - vec![] - }, - }) + .map(|process| return_mapped_process(process, app_data)) .collect::>(); } (process_vector, grouped_process_vector) } +fn return_mapped_process( + process: &data_collection::processes::ProcessData, app_data: &data_collection::Data, +) -> ConvertedProcessData { + ConvertedProcessData { + pid: process.pid, + name: process.name.to_string(), + cpu_usage: format!("{:.1}%", process.cpu_usage_percent), + mem_usage: format!( + "{:.1}%", + if let Some(mem_usage) = process.mem_usage_percent { + mem_usage + } else if let Some(mem_usage_kb) = process.mem_usage_kb { + if let Some(mem_data) = app_data.memory.last() { + (mem_usage_kb / 1000) as f64 / mem_data.mem_total_in_mb as f64 * 100_f64 // TODO: [OPT] Get rid of this + } else { + 0_f64 + } + } else { + 0_f64 + } + ), + group: vec![], + } +} + pub fn update_cpu_data_points( show_avg_cpu: bool, app_data: &data_collection::Data, ) -> Vec { diff --git a/src/main.rs b/src/main.rs index 506707bb..3b2afade 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,6 +73,7 @@ fn main() -> error::Result<()> { //(@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.") (@arg GROUP_PROCESSES: -g --group "Groups processes with the same name together on launch.") + (@arg SEARCH_DEFAULT_USE_SIMPLE: -s --simple_search "Uses a simple case-insensitive string comparison to search processes by default.") ) .get_matches(); @@ -129,6 +130,11 @@ fn main() -> error::Result<()> { app.toggle_grouping(); } + // Set default search method + if matches.is_present("SEARCH_DEFAULT_USE_SIMPLE") { + app.use_simple = true; + } + // Set up up tui and crossterm let mut stdout = stdout(); enable_raw_mode()?; @@ -286,8 +292,6 @@ fn main() -> error::Result<()> { app.canvas_data.swap_data = update_swap_data_points(&app.data); app.canvas_data.cpu_data = update_cpu_data_points(app.show_average_cpu, &app.data); - - //debug!("Update event complete."); } } } @@ -384,11 +388,19 @@ fn handle_process_sorting(app: &mut app::App) { app.process_sorting_reverse, ); - let tuple_results = update_process_row( - &app.data, - app.get_current_regex_matcher(), - app.is_searching_with_pid(), - ); + let tuple_results = if app.use_simple { + simple_update_process_row( + &app.data, + &(app.get_current_search_query().to_ascii_lowercase()), + app.is_searching_with_pid(), + ) + } else { + regex_update_process_row( + &app.data, + app.get_current_regex_matcher(), + app.is_searching_with_pid(), + ) + }; app.canvas_data.process_data = tuple_results.0; app.canvas_data.grouped_process_data = tuple_results.1; }