Skip to content

Commit

Permalink
add sort_cpu to config
Browse files Browse the repository at this point in the history
  • Loading branch information
acxz committed Apr 23, 2022
1 parent 34d54e7 commit e68c0ff
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 17 deletions.
1 change: 1 addition & 0 deletions docs/content/configuration/command-line-flags.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
36 changes: 19 additions & 17 deletions src/data_conversion.rs
Original file line number Diff line number Diff line change
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 @@ -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(
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit e68c0ff

Please sign in to comment.