diff --git a/README.md b/README.md index f8bafbf4..d6766edd 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,8 @@ The compatibility of each widget and operating systems are, as of version 0.1.0, - `-u`, `--current_usage` will make a process' CPU usage be based on the current total CPU usage, rather than assuming 100% CPU usage. Only affects Linux for now. +- `g`, `--group` will group together processes with the same name by default (equivalent to pressing `Tab`). + ### Keybindings #### General @@ -93,6 +95,8 @@ The compatibility of each widget and operating systems are, as of version 0.1.0, - `n` to sort by process name. Sorts in ascending order by default. Press again to reverse sorting order. +- `Tab` to group together processes with the same name. Disables PID sorting. `dd` will now kill all processes covered by that name. + ### Mouse actions - Scrolling with the mouse will scroll through the currently selected list, similar to using the up/down arrow keys. diff --git a/src/app.rs b/src/app.rs index 8687d587..684ab9ad 100644 --- a/src/app.rs +++ b/src/app.rs @@ -124,6 +124,14 @@ impl App { } } + pub fn on_tab(&mut self) { + match self.current_application_position { + ApplicationPosition::Process => self.toggle_grouping(), + ApplicationPosition::Disk => {} + _ => {} + } + } + pub fn is_grouped(&self) -> bool { self.enable_grouping } diff --git a/src/canvas.rs b/src/canvas.rs index 90ab09b9..96a6b824 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -19,7 +19,7 @@ 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>; 14] = [ + 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"), @@ -36,6 +36,7 @@ lazy_static! { 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 = gen_n_colours(constants::NUM_COLOURS); static ref CANVAS_BORDER_STYLE: Style = Style::default().fg(BORDER_STYLE_COLOUR); diff --git a/src/main.rs b/src/main.rs index 8cdc4e50..1ff5ba57 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,6 +72,7 @@ fn main() -> error::Result<()> { (@arg USE_CURR_USAGE: -u --current_usage "Within Linux, sets a process' CPU usage to be based on the total current CPU usage, rather than assuming 100% usage.") //(@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.") ) .get_matches(); @@ -123,6 +124,11 @@ fn main() -> error::Result<()> { use_current_cpu_total, ); + // Enable grouping immediately if set. + if matches.is_present("GROUP_PROCESSES") { + app.toggle_grouping(); + } + // Set up up tui and crossterm let mut stdout = stdout(); enable_raw_mode()?; @@ -219,7 +225,7 @@ fn main() -> error::Result<()> { KeyCode::Char(uncaught_char) => app.on_char_key(uncaught_char), KeyCode::Esc => app.reset(), KeyCode::Enter => app.on_enter(), - KeyCode::Tab => app.toggle_grouping(), + KeyCode::Tab => app.on_tab(), _ => {} } } else { @@ -299,6 +305,15 @@ fn main() -> error::Result<()> { } } } + + // Quick fix for tab updating the table headers + if let data_collection::processes::ProcessSorting::PID = &app.process_sorting_type { + if app.is_grouped() { + app.process_sorting_type = data_collection::processes::ProcessSorting::CPU; // Go back to default, negate PID for group + app.process_sorting_reverse = true; + } + } + // Draw! if let Err(err) = canvas::draw_data(&mut terminal, &mut app) { cleanup(&mut terminal)?; @@ -361,15 +376,19 @@ fn handle_process_sorting(app: &mut app::App) { ); if let Some(grouped_list_of_processes) = &mut app.data.grouped_list_of_processes { - data_collection::processes::sort_processes( - grouped_list_of_processes, - if let data_collection::processes::ProcessSorting::PID = &app.process_sorting_type { - &data_collection::processes::ProcessSorting::CPU // Go back to default, negate PID for group - } else { - &app.process_sorting_type - }, - app.process_sorting_reverse, - ); + if let data_collection::processes::ProcessSorting::PID = &app.process_sorting_type { + data_collection::processes::sort_processes( + grouped_list_of_processes, + &data_collection::processes::ProcessSorting::CPU, // Go back to default, negate PID for group + true, + ); + } else { + data_collection::processes::sort_processes( + grouped_list_of_processes, + &app.process_sorting_type, + app.process_sorting_reverse, + ); + } } data_collection::processes::sort_processes(