From 470ef6a9853a5944dc9e2be04968263009b73da4 Mon Sep 17 00:00:00 2001 From: acxz <17132214+acxz@users.noreply.github.com> Date: Sat, 23 Apr 2022 01:38:42 -0400 Subject: [PATCH] add sort_cpu to config --- .../configuration/command-line-flags.md | 1 + .../configuration/config-file/flags.md | 1 + sample_configs/default_config.toml | 2 ++ src/app.rs | 1 + src/bin/main.rs | 1 + src/clap.rs | 6 ++++ src/constants.rs | 2 ++ src/data_conversion.rs | 36 ++++++++++--------- src/lib.rs | 1 + src/options.rs | 16 +++++++++ 10 files changed, 50 insertions(+), 17 deletions(-) diff --git a/docs/content/configuration/command-line-flags.md b/docs/content/configuration/command-line-flags.md index b5d6d3d31..9cd62cd3c 100644 --- a/docs/content/configuration/command-line-flags.md +++ b/docs/content/configuration/command-line-flags.md @@ -16,6 +16,7 @@ The following flags can be provided to bottom in the command line to change the | `--color ` | Use a color scheme, use --help for supported values. | | `-C, --config ` | Sets the location of the config file. | | `-u, --current_usage` | Sets process CPU% to be based on current CPU%. | +| `--sort_cpu` | Order CPUs based on current CPU%. | | `-t, --default_time_value ` | Default time value for graphs in ms. | | `--default_widget_count ` | Sets the n'th selected widget type as the default. | | `--default_widget_type ` | Sets the default widget type, use --help for more info. | diff --git a/docs/content/configuration/config-file/flags.md b/docs/content/configuration/config-file/flags.md index 93f75ac48..f3d091401 100644 --- a/docs/content/configuration/config-file/flags.md +++ b/docs/content/configuration/config-file/flags.md @@ -12,6 +12,7 @@ Most of the [command line flags](../../command-line-flags) have config file equi | `dot_marker` | Boolean | Uses a dot marker for graphs. | | `left_legend` | Boolean | Puts the CPU chart legend to the left side. | | `current_usage` | Boolean | Sets process CPU% to be based on current CPU%. | +| `sort_cpu` | Boolean | Order CPUs based on current CPU%. | | `group_processes` | Boolean | Groups processes with the same name by default. | | `case_sensitive` | Boolean | Enables case sensitivity by default. | | `whole_word` | Boolean | Enables whole-word matching by default. | diff --git a/sample_configs/default_config.toml b/sample_configs/default_config.toml index 1e2d56a45..ac00f76b1 100644 --- a/sample_configs/default_config.toml +++ b/sample_configs/default_config.toml @@ -17,6 +17,8 @@ #left_legend = false # Whether to set CPU% on a process to be based on the total CPU or just current usage. #current_usage = false +# Whether to order CPUs based on current CPU%. +#sort_cpu = false # Whether to group processes with the same name together by default. #group_processes = false # Whether to make process searching case sensitive by default. diff --git a/src/app.rs b/src/app.rs index e8d68e378..ecad2a424 100644 --- a/src/app.rs +++ b/src/app.rs @@ -50,6 +50,7 @@ pub struct AppConfigFields { pub left_legend: bool, pub show_average_cpu: bool, pub use_current_cpu_total: bool, + pub sort_cpu: bool, pub use_basic_mode: bool, pub default_time_value: u64, pub time_interval: u64, diff --git a/src/bin/main.rs b/src/bin/main.rs index 6a1f0aae7..d9b089ea0 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -207,6 +207,7 @@ fn main() -> Result<()> { &app.data_collection, &mut app.canvas_data.cpu_data, false, + app.app_config_fields.sort_cpu, ); app.canvas_data.load_avg_data = app.data_collection.load_avg_harvest; } diff --git a/src/clap.rs b/src/clap.rs index 8926d6bd5..b2d9ffc9e 100644 --- a/src/clap.rs +++ b/src/clap.rs @@ -135,6 +135,11 @@ pub fn build_app() -> Command<'static> { .help("Sets process CPU% to be based on current CPU%.") .long_help("Sets process CPU% usage to be based on the current system CPU% usage rather than total CPU usage."); + let sort_cpu = Arg::new("sort_cpu") + .long("sort_cpu") + .help("Orders CPUs based on current CPU%.") + .long_help("Sorts CPUs according to the current CPU usage."); + // TODO: [DEBUG] Add a proper debugging solution. let disable_click = Arg::new("disable_click") @@ -389,6 +394,7 @@ use CPU (3) as the default instead. .arg(network_use_log) .arg(network_use_binary_prefix) .arg(current_usage) + .arg(sort_cpu) .arg(use_old_network_legend) .arg(whole_word); diff --git a/src/constants.rs b/src/constants.rs index 9ac719b39..8ef80b77c 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -443,6 +443,8 @@ pub const CONFIG_TEXT: &str = r##"# This is a default config file for bottom. A #left_legend = false # Whether to set CPU% on a process to be based on the total CPU or just current usage. #current_usage = false +# Whether to order CPUs based on current CPU%. +#sort_cpu = false # Whether to group processes with the same name together by default. #group_processes = false # Whether to make process searching case sensitive by default. diff --git a/src/data_conversion.rs b/src/data_conversion.rs index 260fceec8..0e09c8be6 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -162,7 +162,7 @@ pub fn convert_disk_row(current_data: &data_farmer::DataCollection) -> Vec, - is_frozen: bool, + is_frozen: bool, sort_cpu: bool, ) { let current_time = if is_frozen { if let Some(frozen_instant) = current_data.frozen_instant { @@ -240,22 +240,24 @@ pub fn convert_cpu_data_points( } // order cpus in descending values excluding All & AVG - existing_cpu_data.sort_by(|a, b| { - let default_values = vec!["All".to_string(), "AVG".to_string()]; - if default_values.contains(&a.cpu_name) - || default_values.contains(&b.cpu_name) - || a.cpu_data.is_empty() - || b.cpu_data.is_empty() - { - std::cmp::Ordering::Equal - } else if a.cpu_data[a.cpu_data.len() - 1].1 < b.cpu_data[b.cpu_data.len() - 1].1 { - std::cmp::Ordering::Greater - } else if a.cpu_data[a.cpu_data.len() - 1].1 == b.cpu_data[b.cpu_data.len() - 1].1 { - std::cmp::Ordering::Equal - } else { - std::cmp::Ordering::Less - } - }); + if sort_cpu { + existing_cpu_data.sort_by(|a, b| { + let default_values = vec!["All".to_string(), "AVG".to_string()]; + if default_values.contains(&a.cpu_name) + || default_values.contains(&b.cpu_name) + || a.cpu_data.is_empty() + || b.cpu_data.is_empty() + { + std::cmp::Ordering::Equal + } else if a.cpu_data[a.cpu_data.len() - 1].1 < b.cpu_data[b.cpu_data.len() - 1].1 { + std::cmp::Ordering::Greater + } else if a.cpu_data[a.cpu_data.len() - 1].1 == b.cpu_data[b.cpu_data.len() - 1].1 { + std::cmp::Ordering::Equal + } else { + std::cmp::Ordering::Less + } + }); + } } pub fn convert_mem_data_points( diff --git a/src/lib.rs b/src/lib.rs index 11de8f058..8fbd33777 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -318,6 +318,7 @@ pub fn handle_force_redraws(app: &mut App) { &app.data_collection, &mut app.canvas_data.cpu_data, app.is_frozen, + app.app_config_fields.sort_cpu, ); app.canvas_data.load_avg_data = app.data_collection.load_avg_harvest; app.cpu_state.force_update = None; diff --git a/src/options.rs b/src/options.rs index b7377e6e4..15a09eaea 100644 --- a/src/options.rs +++ b/src/options.rs @@ -68,6 +68,9 @@ pub struct ConfigFlags { #[builder(default, setter(strip_option))] pub current_usage: Option, + #[builder(default, setter(strip_option))] + pub sort_cpu: Option, + #[builder(default, setter(strip_option))] pub group_processes: Option, @@ -417,6 +420,7 @@ pub fn build_app( use_dot: get_use_dot(matches, config), left_legend: get_use_left_legend(matches, config), use_current_cpu_total: get_use_current_cpu_total(matches, config), + sort_cpu: get_sort_cpu(matches, config), use_basic_mode, default_time_value, time_interval: get_time_interval(matches, config) @@ -691,6 +695,18 @@ fn get_use_current_cpu_total(matches: &clap::ArgMatches, config: &Config) -> boo false } +fn get_sort_cpu(matches: &clap::ArgMatches, config: &Config) -> bool { + if matches.is_present("sort_cpu") { + return true; + } else if let Some(flags) = &config.flags { + if let Some(sort_cpu) = flags.current_usage { + return sort_cpu; + } + } + + false +} + fn get_use_basic_mode(matches: &clap::ArgMatches, config: &Config) -> bool { if matches.is_present("basic") { return true;