Skip to content

Commit 470ef6a

Browse files
committed
add sort_cpu to config
1 parent 34d54e7 commit 470ef6a

10 files changed

Lines changed: 50 additions & 17 deletions

File tree

docs/content/configuration/command-line-flags.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The following flags can be provided to bottom in the command line to change the
1616
| `--color <COLOR SCHEME>` | Use a color scheme, use --help for supported values. |
1717
| `-C, --config <CONFIG PATH>` | Sets the location of the config file. |
1818
| `-u, --current_usage` | Sets process CPU% to be based on current CPU%. |
19+
| `--sort_cpu` | Order CPUs based on current CPU%. |
1920
| `-t, --default_time_value <MS>` | Default time value for graphs in ms. |
2021
| `--default_widget_count <INT>` | Sets the n'th selected widget type as the default. |
2122
| `--default_widget_type <WIDGET TYPE>` | Sets the default widget type, use --help for more info. |

docs/content/configuration/config-file/flags.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Most of the [command line flags](../../command-line-flags) have config file equi
1212
| `dot_marker` | Boolean | Uses a dot marker for graphs. |
1313
| `left_legend` | Boolean | Puts the CPU chart legend to the left side. |
1414
| `current_usage` | Boolean | Sets process CPU% to be based on current CPU%. |
15+
| `sort_cpu` | Boolean | Order CPUs based on current CPU%. |
1516
| `group_processes` | Boolean | Groups processes with the same name by default. |
1617
| `case_sensitive` | Boolean | Enables case sensitivity by default. |
1718
| `whole_word` | Boolean | Enables whole-word matching by default. |

sample_configs/default_config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#left_legend = false
1818
# Whether to set CPU% on a process to be based on the total CPU or just current usage.
1919
#current_usage = false
20+
# Whether to order CPUs based on current CPU%.
21+
#sort_cpu = false
2022
# Whether to group processes with the same name together by default.
2123
#group_processes = false
2224
# Whether to make process searching case sensitive by default.

src/app.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub struct AppConfigFields {
5050
pub left_legend: bool,
5151
pub show_average_cpu: bool,
5252
pub use_current_cpu_total: bool,
53+
pub sort_cpu: bool,
5354
pub use_basic_mode: bool,
5455
pub default_time_value: u64,
5556
pub time_interval: u64,

src/bin/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ fn main() -> Result<()> {
207207
&app.data_collection,
208208
&mut app.canvas_data.cpu_data,
209209
false,
210+
app.app_config_fields.sort_cpu,
210211
);
211212
app.canvas_data.load_avg_data = app.data_collection.load_avg_harvest;
212213
}

src/clap.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ pub fn build_app() -> Command<'static> {
135135
.help("Sets process CPU% to be based on current CPU%.")
136136
.long_help("Sets process CPU% usage to be based on the current system CPU% usage rather than total CPU usage.");
137137

138+
let sort_cpu = Arg::new("sort_cpu")
139+
.long("sort_cpu")
140+
.help("Orders CPUs based on current CPU%.")
141+
.long_help("Sorts CPUs according to the current CPU usage.");
142+
138143
// TODO: [DEBUG] Add a proper debugging solution.
139144

140145
let disable_click = Arg::new("disable_click")
@@ -389,6 +394,7 @@ use CPU (3) as the default instead.
389394
.arg(network_use_log)
390395
.arg(network_use_binary_prefix)
391396
.arg(current_usage)
397+
.arg(sort_cpu)
392398
.arg(use_old_network_legend)
393399
.arg(whole_word);
394400

src/constants.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,8 @@ pub const CONFIG_TEXT: &str = r##"# This is a default config file for bottom. A
443443
#left_legend = false
444444
# Whether to set CPU% on a process to be based on the total CPU or just current usage.
445445
#current_usage = false
446+
# Whether to order CPUs based on current CPU%.
447+
#sort_cpu = false
446448
# Whether to group processes with the same name together by default.
447449
#group_processes = false
448450
# Whether to make process searching case sensitive by default.

src/data_conversion.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ pub fn convert_disk_row(current_data: &data_farmer::DataCollection) -> Vec<Vec<S
162162

163163
pub fn convert_cpu_data_points(
164164
current_data: &data_farmer::DataCollection, existing_cpu_data: &mut Vec<ConvertedCpuData>,
165-
is_frozen: bool,
165+
is_frozen: bool, sort_cpu: bool,
166166
) {
167167
let current_time = if is_frozen {
168168
if let Some(frozen_instant) = current_data.frozen_instant {
@@ -240,22 +240,24 @@ pub fn convert_cpu_data_points(
240240
}
241241

242242
// order cpus in descending values excluding All & AVG
243-
existing_cpu_data.sort_by(|a, b| {
244-
let default_values = vec!["All".to_string(), "AVG".to_string()];
245-
if default_values.contains(&a.cpu_name)
246-
|| default_values.contains(&b.cpu_name)
247-
|| a.cpu_data.is_empty()
248-
|| b.cpu_data.is_empty()
249-
{
250-
std::cmp::Ordering::Equal
251-
} else if a.cpu_data[a.cpu_data.len() - 1].1 < b.cpu_data[b.cpu_data.len() - 1].1 {
252-
std::cmp::Ordering::Greater
253-
} else if a.cpu_data[a.cpu_data.len() - 1].1 == b.cpu_data[b.cpu_data.len() - 1].1 {
254-
std::cmp::Ordering::Equal
255-
} else {
256-
std::cmp::Ordering::Less
257-
}
258-
});
243+
if sort_cpu {
244+
existing_cpu_data.sort_by(|a, b| {
245+
let default_values = vec!["All".to_string(), "AVG".to_string()];
246+
if default_values.contains(&a.cpu_name)
247+
|| default_values.contains(&b.cpu_name)
248+
|| a.cpu_data.is_empty()
249+
|| b.cpu_data.is_empty()
250+
{
251+
std::cmp::Ordering::Equal
252+
} else if a.cpu_data[a.cpu_data.len() - 1].1 < b.cpu_data[b.cpu_data.len() - 1].1 {
253+
std::cmp::Ordering::Greater
254+
} else if a.cpu_data[a.cpu_data.len() - 1].1 == b.cpu_data[b.cpu_data.len() - 1].1 {
255+
std::cmp::Ordering::Equal
256+
} else {
257+
std::cmp::Ordering::Less
258+
}
259+
});
260+
}
259261
}
260262

261263
pub fn convert_mem_data_points(

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ pub fn handle_force_redraws(app: &mut App) {
318318
&app.data_collection,
319319
&mut app.canvas_data.cpu_data,
320320
app.is_frozen,
321+
app.app_config_fields.sort_cpu,
321322
);
322323
app.canvas_data.load_avg_data = app.data_collection.load_avg_harvest;
323324
app.cpu_state.force_update = None;

src/options.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ pub struct ConfigFlags {
6868
#[builder(default, setter(strip_option))]
6969
pub current_usage: Option<bool>,
7070

71+
#[builder(default, setter(strip_option))]
72+
pub sort_cpu: Option<bool>,
73+
7174
#[builder(default, setter(strip_option))]
7275
pub group_processes: Option<bool>,
7376

@@ -417,6 +420,7 @@ pub fn build_app(
417420
use_dot: get_use_dot(matches, config),
418421
left_legend: get_use_left_legend(matches, config),
419422
use_current_cpu_total: get_use_current_cpu_total(matches, config),
423+
sort_cpu: get_sort_cpu(matches, config),
420424
use_basic_mode,
421425
default_time_value,
422426
time_interval: get_time_interval(matches, config)
@@ -691,6 +695,18 @@ fn get_use_current_cpu_total(matches: &clap::ArgMatches, config: &Config) -> boo
691695
false
692696
}
693697

698+
fn get_sort_cpu(matches: &clap::ArgMatches, config: &Config) -> bool {
699+
if matches.is_present("sort_cpu") {
700+
return true;
701+
} else if let Some(flags) = &config.flags {
702+
if let Some(sort_cpu) = flags.current_usage {
703+
return sort_cpu;
704+
}
705+
}
706+
707+
false
708+
}
709+
694710
fn get_use_basic_mode(matches: &clap::ArgMatches, config: &Config) -> bool {
695711
if matches.is_present("basic") {
696712
return true;

0 commit comments

Comments
 (0)