Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to sort cpus #707

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/content/configuration/command-line-flags.md
Expand Up @@ -16,6 +16,7 @@ The following flags can be provided to bottom in the command line to change the
| `--color <COLOR SCHEME>` | Use a color scheme, use --help for supported values. |
| `-C, --config <CONFIG PATH>` | 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 <MS>` | Default time value for graphs in ms. |
| `--default_widget_count <INT>` | Sets the n'th selected widget type as the default. |
| `--default_widget_type <WIDGET TYPE>` | Sets the default widget type, use --help for more info. |
Expand Down
1 change: 1 addition & 0 deletions docs/content/configuration/config-file/flags.md
Expand Up @@ -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. |
Expand Down
2 changes: 2 additions & 0 deletions sample_configs/default_config.toml
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/app.rs
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/bin/main.rs
Expand Up @@ -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;
}
Expand Down
6 changes: 6 additions & 0 deletions src/clap.rs
Expand Up @@ -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")
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions src/constants.rs
Expand Up @@ -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.
Expand Down
22 changes: 21 additions & 1 deletion src/data_conversion.rs
Expand Up @@ -162,7 +162,7 @@ pub fn convert_disk_row(current_data: &data_farmer::DataCollection) -> Vec<Vec<S

pub fn convert_cpu_data_points(
current_data: &data_farmer::DataCollection, existing_cpu_data: &mut Vec<ConvertedCpuData>,
is_frozen: bool,
is_frozen: bool, sort_cpu: bool,
) {
let current_time = if is_frozen {
if let Some(frozen_instant) = current_data.frozen_instant {
Expand Down Expand Up @@ -238,6 +238,26 @@ pub fn convert_cpu_data_points(
break;
}
}

// order cpus in descending values excluding All & AVG
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(
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions src/options.rs
Expand Up @@ -68,6 +68,9 @@ pub struct ConfigFlags {
#[builder(default, setter(strip_option))]
pub current_usage: Option<bool>,

#[builder(default, setter(strip_option))]
pub sort_cpu: Option<bool>,

#[builder(default, setter(strip_option))]
pub group_processes: Option<bool>,

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.sort_cpu {
return sort_cpu;
}
}

false
}

fn get_use_basic_mode(matches: &clap::ArgMatches, config: &Config) -> bool {
if matches.is_present("basic") {
return true;
Expand Down