From 16bcf7d820d82a60aa6da015e762baacd87a3af6 Mon Sep 17 00:00:00 2001 From: GrecuAlexandru Date: Sun, 7 Jan 2024 11:45:01 +0200 Subject: [PATCH 01/15] added a feature --- src/app.rs | 1 + src/bin/main.rs | 6 +++--- src/constants.rs | 2 ++ src/data_collection.rs | 6 ++++++ src/data_conversion.rs | 42 +++++++++++++++++++++++++++--------------- src/options.rs | 2 ++ src/options/args.rs | 7 +++++++ 7 files changed, 48 insertions(+), 18 deletions(-) diff --git a/src/app.rs b/src/app.rs index f9e7de16d..6d70a3cff 100644 --- a/src/app.rs +++ b/src/app.rs @@ -50,6 +50,7 @@ pub struct AppConfigFields { pub show_average_cpu: bool, // TODO: Unify this in CPU options pub use_current_cpu_total: bool, pub unnormalized_cpu: bool, + pub memory_use_mega_prefix: 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 0ee3289f3..8181d789a 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -276,9 +276,9 @@ fn main() -> Result<()> { } app.converted_data.mem_labels = - convert_mem_label(&app.data_collection.memory_harvest); + convert_mem_label(&app.data_collection.memory_harvest, app.app_config_fields.memory_use_mega_prefix); app.converted_data.swap_labels = - convert_mem_label(&app.data_collection.swap_harvest); + convert_mem_label(&app.data_collection.swap_harvest, app.app_config_fields.memory_use_mega_prefix); #[cfg(not(target_os = "windows"))] { app.converted_data.cache_labels = @@ -288,7 +288,7 @@ fn main() -> Result<()> { #[cfg(feature = "zfs")] { let arc_labels = - convert_mem_label(&app.data_collection.arc_harvest); + convert_mem_label(&app.data_collection.arc_harvest, app.app_config_fields.memory_use_mega_prefix); app.converted_data.arc_labels = arc_labels; } } diff --git a/src/constants.rs b/src/constants.rs index d736e928f..c4ec5b467 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -545,6 +545,8 @@ pub const CONFIG_TEXT: &str = r#"# This is a default config file for bottom. Al #whole_word = false # Whether to make process searching use regex by default. #regex = false +# Whether to display memory usage in megabibytes (MiB) or gigabibytes (GiB). +#memory_use_mega_prefix = false # Defaults to Celsius. Temperature is one of: #temperature_type = "k" #temperature_type = "f" diff --git a/src/data_collection.rs b/src/data_collection.rs index d00d88e01..cc370e149 100644 --- a/src/data_collection.rs +++ b/src/data_collection.rs @@ -104,6 +104,7 @@ pub struct DataCollector { temperature_type: TemperatureType, use_current_cpu_total: bool, unnormalized_cpu: bool, + memory_use_mega_prefix: bool, last_collection_time: Instant, total_rx: u64, total_tx: u64, @@ -146,6 +147,7 @@ impl DataCollector { temperature_type: TemperatureType::Celsius, use_current_cpu_total: false, unnormalized_cpu: false, + memory_use_mega_prefix: false, last_collection_time: Instant::now(), total_rx: 0, total_tx: 0, @@ -226,6 +228,10 @@ impl DataCollector { self.unnormalized_cpu = unnormalized_cpu; } + pub fn set_memory_use_mega_prefix(&mut self, memory_use_mega_prefix: bool) { + self.memory_use_mega_prefix = memory_use_mega_prefix; + } + pub fn set_show_average_cpu(&mut self, show_average_cpu: bool) { self.show_average_cpu = show_average_cpu; } diff --git a/src/data_conversion.rs b/src/data_conversion.rs index 0d1c46048..f34022364 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -266,27 +266,39 @@ pub fn convert_swap_data_points(current_data: &DataCollection) -> Vec { /// /// The expected usage is to divide out the given value with the returned denominator in order to be able to use it /// with the returned binary unit (e.g. divide 3000 bytes by 1024 to have a value in KiB). -fn get_mem_binary_unit_and_denominator(bytes: u64) -> (&'static str, f64) { - if bytes < KIBI_LIMIT { - // Stick with bytes if under a kibibyte. - ("B", 1.0) - } else if bytes < MEBI_LIMIT { - ("KiB", KIBI_LIMIT_F64) - } else if bytes < GIBI_LIMIT { - ("MiB", MEBI_LIMIT_F64) - } else if bytes < TEBI_LIMIT { - ("GiB", GIBI_LIMIT_F64) +fn get_mem_binary_unit_and_denominator(bytes: u64, memory_use_mega_prefix: bool) -> (&'static str, f64) { + if memory_use_mega_prefix { + if bytes < KIBI_LIMIT { + // Stick with bytes if under a kibibyte. + ("B", 1.0) + } else if bytes < MEBI_LIMIT { + ("KiB", KIBI_LIMIT_F64) + } else { + // Otherwise just use mebibytes, which is probably safe for most use cases. + ("MiB", MEBI_LIMIT_F64) + } } else { - // Otherwise just use tebibytes, which is probably safe for most use cases. - ("TiB", TEBI_LIMIT_F64) + if bytes < KIBI_LIMIT { + // Stick with bytes if under a kibibyte. + ("B", 1.0) + } else if bytes < MEBI_LIMIT { + ("KiB", KIBI_LIMIT_F64) + } else if bytes < GIBI_LIMIT { + ("MiB", MEBI_LIMIT_F64) + } else if bytes < TEBI_LIMIT { + ("GiB", GIBI_LIMIT_F64) + } else { + // Otherwise just use tebibytes, which is probably safe for most use cases. + ("TiB", TEBI_LIMIT_F64) + } } } /// Returns the unit type and denominator for given total amount of memory in kibibytes. -pub fn convert_mem_label(harvest: &MemHarvest) -> Option<(String, String)> { +pub fn convert_mem_label(harvest: &MemHarvest, memory_use_mega_prefix: bool) -> Option<(String, String)> { if harvest.total_bytes > 0 { Some((format!("{:3.0}%", harvest.use_percent.unwrap_or(0.0)), { - let (unit, denominator) = get_mem_binary_unit_and_denominator(harvest.total_bytes); + let (unit, denominator) = get_mem_binary_unit_and_denominator(harvest.total_bytes, memory_use_mega_prefix); format!( " {:.1}{}/{:.1}{}", @@ -614,7 +626,7 @@ pub fn convert_gpu_data(current_data: &DataCollection) -> Option, current_usage: Option, unnormalized_cpu: Option, + memory_use_mega_prefix: Option, group_processes: Option, case_sensitive: Option, whole_word: Option, @@ -271,6 +272,7 @@ pub fn init_app( left_legend: is_flag_enabled!(left_legend, matches, config), use_current_cpu_total: is_flag_enabled!(current_usage, matches, config), unnormalized_cpu: is_flag_enabled!(unnormalized_cpu, matches, config), + memory_use_mega_prefix: is_flag_enabled!(memory_use_mega_prefix, matches, config), use_basic_mode, default_time_value, time_interval: get_time_interval(matches, config, retention_ms) diff --git a/src/options/args.rs b/src/options/args.rs index 2c524da8a..ca7ee8628 100644 --- a/src/options/args.rs +++ b/src/options/args.rs @@ -403,6 +403,12 @@ use CPU (3) as the default instead. .action(ArgAction::Version) .help("Prints version information."); + let memory_use_mega_prefix = Arg::new("memory_use_mega_prefix") + .long("memory_use_mega_prefix") + .action(ArgAction::SetTrue) + .help("Displays the memory widget with a mega prefix.") + .long_help("Displays the memory widget in megabibytes instead of rounding it to the nearest prefix. Defaults to rounding it to the nearest prefix. Example: 1.2GiB will be displayed as 1228MiB."); + const VERSION: &str = match option_env!("NIGHTLY_VERSION") { Some(nightly_version) => nightly_version, None => crate_version!(), @@ -426,6 +432,7 @@ use CPU (3) as the default instead. config_location, color, mem_as_value, + memory_use_mega_prefix, default_time_value, default_widget_count, default_widget_type, From 39286a44255387f9aed8b96de416e4e3b260b9d1 Mon Sep 17 00:00:00 2001 From: GrecuAlexandru Date: Mon, 8 Jan 2024 01:32:23 +0200 Subject: [PATCH 02/15] fixed formatting issues --- src/bin/main.rs | 24 ++++++++++++++++-------- src/data_conversion.rs | 11 ++++++++--- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 8181d789a..a1d8e65da 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -275,20 +275,28 @@ fn main() -> Result<()> { convert_gpu_data(&app.data_collection); } - app.converted_data.mem_labels = - convert_mem_label(&app.data_collection.memory_harvest, app.app_config_fields.memory_use_mega_prefix); - app.converted_data.swap_labels = - convert_mem_label(&app.data_collection.swap_harvest, app.app_config_fields.memory_use_mega_prefix); + app.converted_data.mem_labels = convert_mem_label( + &app.data_collection.memory_harvest, + app.app_config_fields.memory_use_mega_prefix + ); + app.converted_data.swap_labels = convert_mem_label( + &app.data_collection.swap_harvest, + app.app_config_fields.memory_use_mega_prefix + ); #[cfg(not(target_os = "windows"))] { - app.converted_data.cache_labels = - convert_mem_label(&app.data_collection.cache_harvest); + app.converted_data.cache_labels = convert_mem_label( + &app.data_collection.cache_harvest, + app.app_config_fields.memory_use_mega_prefix + ); } #[cfg(feature = "zfs")] { - let arc_labels = - convert_mem_label(&app.data_collection.arc_harvest, app.app_config_fields.memory_use_mega_prefix); + let arc_labels = convert_mem_label( + &app.data_collection.arc_harvest, + app.app_config_fields.memory_use_mega_prefix + ); app.converted_data.arc_labels = arc_labels; } } diff --git a/src/data_conversion.rs b/src/data_conversion.rs index f34022364..cee144a23 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -266,7 +266,9 @@ pub fn convert_swap_data_points(current_data: &DataCollection) -> Vec { /// /// The expected usage is to divide out the given value with the returned denominator in order to be able to use it /// with the returned binary unit (e.g. divide 3000 bytes by 1024 to have a value in KiB). -fn get_mem_binary_unit_and_denominator(bytes: u64, memory_use_mega_prefix: bool) -> (&'static str, f64) { +fn get_mem_binary_unit_and_denominator( + bytes: u64, memory_use_mega_prefix: bool +) -> (&'static str, f64) { if memory_use_mega_prefix { if bytes < KIBI_LIMIT { // Stick with bytes if under a kibibyte. @@ -295,10 +297,13 @@ fn get_mem_binary_unit_and_denominator(bytes: u64, memory_use_mega_prefix: bool) } /// Returns the unit type and denominator for given total amount of memory in kibibytes. -pub fn convert_mem_label(harvest: &MemHarvest, memory_use_mega_prefix: bool) -> Option<(String, String)> { +pub fn convert_mem_label( + harvest: &MemHarvest, memory_use_mega_prefix: bool +) -> Option<(String, String)> { if harvest.total_bytes > 0 { Some((format!("{:3.0}%", harvest.use_percent.unwrap_or(0.0)), { - let (unit, denominator) = get_mem_binary_unit_and_denominator(harvest.total_bytes, memory_use_mega_prefix); + let (unit, denominator) = + get_mem_binary_unit_and_denominator(harvest.total_bytes, memory_use_mega_prefix); format!( " {:.1}{}/{:.1}{}", From bef4292b39b30b12b04c31755d2b7ec381540d91 Mon Sep 17 00:00:00 2001 From: GrecuAlexandru Date: Mon, 8 Jan 2024 11:05:02 +0200 Subject: [PATCH 03/15] ran cargo fmt locally --- src/bin/main.rs | 8 ++++---- src/data_conversion.rs | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index a1d8e65da..f3e568fec 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -277,17 +277,17 @@ fn main() -> Result<()> { app.converted_data.mem_labels = convert_mem_label( &app.data_collection.memory_harvest, - app.app_config_fields.memory_use_mega_prefix + app.app_config_fields.memory_use_mega_prefix, ); app.converted_data.swap_labels = convert_mem_label( &app.data_collection.swap_harvest, - app.app_config_fields.memory_use_mega_prefix + app.app_config_fields.memory_use_mega_prefix, ); #[cfg(not(target_os = "windows"))] { app.converted_data.cache_labels = convert_mem_label( &app.data_collection.cache_harvest, - app.app_config_fields.memory_use_mega_prefix + app.app_config_fields.memory_use_mega_prefix, ); } @@ -295,7 +295,7 @@ fn main() -> Result<()> { { let arc_labels = convert_mem_label( &app.data_collection.arc_harvest, - app.app_config_fields.memory_use_mega_prefix + app.app_config_fields.memory_use_mega_prefix, ); app.converted_data.arc_labels = arc_labels; } diff --git a/src/data_conversion.rs b/src/data_conversion.rs index cee144a23..382d5c371 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -267,7 +267,7 @@ pub fn convert_swap_data_points(current_data: &DataCollection) -> Vec { /// The expected usage is to divide out the given value with the returned denominator in order to be able to use it /// with the returned binary unit (e.g. divide 3000 bytes by 1024 to have a value in KiB). fn get_mem_binary_unit_and_denominator( - bytes: u64, memory_use_mega_prefix: bool + bytes: u64, memory_use_mega_prefix: bool, ) -> (&'static str, f64) { if memory_use_mega_prefix { if bytes < KIBI_LIMIT { @@ -298,11 +298,11 @@ fn get_mem_binary_unit_and_denominator( /// Returns the unit type and denominator for given total amount of memory in kibibytes. pub fn convert_mem_label( - harvest: &MemHarvest, memory_use_mega_prefix: bool + harvest: &MemHarvest, memory_use_mega_prefix: bool, ) -> Option<(String, String)> { if harvest.total_bytes > 0 { Some((format!("{:3.0}%", harvest.use_percent.unwrap_or(0.0)), { - let (unit, denominator) = + let (unit, denominator) = get_mem_binary_unit_and_denominator(harvest.total_bytes, memory_use_mega_prefix); format!( From c9e5bb7fb5ace478a8c2a8ecd4127cc1aa27dafc Mon Sep 17 00:00:00 2001 From: GrecuAlexandru Date: Mon, 8 Jan 2024 11:16:56 +0200 Subject: [PATCH 04/15] fixed more formatting issues --- src/data_conversion.rs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/data_conversion.rs b/src/data_conversion.rs index 382d5c371..e8c2a07e5 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -279,20 +279,18 @@ fn get_mem_binary_unit_and_denominator( // Otherwise just use mebibytes, which is probably safe for most use cases. ("MiB", MEBI_LIMIT_F64) } + } else if bytes < KIBI_LIMIT { + // Stick with bytes if under a kibibyte. + ("B", 1.0) + } else if bytes < MEBI_LIMIT { + ("KiB", KIBI_LIMIT_F64) + } else if bytes < GIBI_LIMIT { + ("MiB", MEBI_LIMIT_F64) + } else if bytes < TEBI_LIMIT { + ("GiB", GIBI_LIMIT_F64) } else { - if bytes < KIBI_LIMIT { - // Stick with bytes if under a kibibyte. - ("B", 1.0) - } else if bytes < MEBI_LIMIT { - ("KiB", KIBI_LIMIT_F64) - } else if bytes < GIBI_LIMIT { - ("MiB", MEBI_LIMIT_F64) - } else if bytes < TEBI_LIMIT { - ("GiB", GIBI_LIMIT_F64) - } else { - // Otherwise just use tebibytes, which is probably safe for most use cases. - ("TiB", TEBI_LIMIT_F64) - } + // Otherwise just use tebibytes, which is probably safe for most use cases. + ("TiB", TEBI_LIMIT_F64) } } From b8d3c26b956125577b57d5c9dd107c133ab1e561 Mon Sep 17 00:00:00 2001 From: GrecuAlexandru Date: Sun, 7 Jan 2024 11:45:01 +0200 Subject: [PATCH 05/15] added a feature --- src/app.rs | 1 + src/bin/main.rs | 6 +++--- src/constants.rs | 2 ++ src/data_collection.rs | 6 ++++++ src/data_conversion.rs | 42 +++++++++++++++++++++++++++--------------- src/options.rs | 2 ++ src/options/args.rs | 7 +++++++ 7 files changed, 48 insertions(+), 18 deletions(-) diff --git a/src/app.rs b/src/app.rs index f9e7de16d..6d70a3cff 100644 --- a/src/app.rs +++ b/src/app.rs @@ -50,6 +50,7 @@ pub struct AppConfigFields { pub show_average_cpu: bool, // TODO: Unify this in CPU options pub use_current_cpu_total: bool, pub unnormalized_cpu: bool, + pub memory_use_mega_prefix: 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 0ee3289f3..8181d789a 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -276,9 +276,9 @@ fn main() -> Result<()> { } app.converted_data.mem_labels = - convert_mem_label(&app.data_collection.memory_harvest); + convert_mem_label(&app.data_collection.memory_harvest, app.app_config_fields.memory_use_mega_prefix); app.converted_data.swap_labels = - convert_mem_label(&app.data_collection.swap_harvest); + convert_mem_label(&app.data_collection.swap_harvest, app.app_config_fields.memory_use_mega_prefix); #[cfg(not(target_os = "windows"))] { app.converted_data.cache_labels = @@ -288,7 +288,7 @@ fn main() -> Result<()> { #[cfg(feature = "zfs")] { let arc_labels = - convert_mem_label(&app.data_collection.arc_harvest); + convert_mem_label(&app.data_collection.arc_harvest, app.app_config_fields.memory_use_mega_prefix); app.converted_data.arc_labels = arc_labels; } } diff --git a/src/constants.rs b/src/constants.rs index f09a5d5bb..94e3aeb03 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -545,6 +545,8 @@ pub const CONFIG_TEXT: &str = r#"# This is a default config file for bottom. Al #whole_word = false # Whether to make process searching use regex by default. #regex = false +# Whether to display memory usage in megabibytes (MiB) or gigabibytes (GiB). +#memory_use_mega_prefix = false # Defaults to Celsius. Temperature is one of: #temperature_type = "k" #temperature_type = "f" diff --git a/src/data_collection.rs b/src/data_collection.rs index d00d88e01..cc370e149 100644 --- a/src/data_collection.rs +++ b/src/data_collection.rs @@ -104,6 +104,7 @@ pub struct DataCollector { temperature_type: TemperatureType, use_current_cpu_total: bool, unnormalized_cpu: bool, + memory_use_mega_prefix: bool, last_collection_time: Instant, total_rx: u64, total_tx: u64, @@ -146,6 +147,7 @@ impl DataCollector { temperature_type: TemperatureType::Celsius, use_current_cpu_total: false, unnormalized_cpu: false, + memory_use_mega_prefix: false, last_collection_time: Instant::now(), total_rx: 0, total_tx: 0, @@ -226,6 +228,10 @@ impl DataCollector { self.unnormalized_cpu = unnormalized_cpu; } + pub fn set_memory_use_mega_prefix(&mut self, memory_use_mega_prefix: bool) { + self.memory_use_mega_prefix = memory_use_mega_prefix; + } + pub fn set_show_average_cpu(&mut self, show_average_cpu: bool) { self.show_average_cpu = show_average_cpu; } diff --git a/src/data_conversion.rs b/src/data_conversion.rs index 0d1c46048..f34022364 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -266,27 +266,39 @@ pub fn convert_swap_data_points(current_data: &DataCollection) -> Vec { /// /// The expected usage is to divide out the given value with the returned denominator in order to be able to use it /// with the returned binary unit (e.g. divide 3000 bytes by 1024 to have a value in KiB). -fn get_mem_binary_unit_and_denominator(bytes: u64) -> (&'static str, f64) { - if bytes < KIBI_LIMIT { - // Stick with bytes if under a kibibyte. - ("B", 1.0) - } else if bytes < MEBI_LIMIT { - ("KiB", KIBI_LIMIT_F64) - } else if bytes < GIBI_LIMIT { - ("MiB", MEBI_LIMIT_F64) - } else if bytes < TEBI_LIMIT { - ("GiB", GIBI_LIMIT_F64) +fn get_mem_binary_unit_and_denominator(bytes: u64, memory_use_mega_prefix: bool) -> (&'static str, f64) { + if memory_use_mega_prefix { + if bytes < KIBI_LIMIT { + // Stick with bytes if under a kibibyte. + ("B", 1.0) + } else if bytes < MEBI_LIMIT { + ("KiB", KIBI_LIMIT_F64) + } else { + // Otherwise just use mebibytes, which is probably safe for most use cases. + ("MiB", MEBI_LIMIT_F64) + } } else { - // Otherwise just use tebibytes, which is probably safe for most use cases. - ("TiB", TEBI_LIMIT_F64) + if bytes < KIBI_LIMIT { + // Stick with bytes if under a kibibyte. + ("B", 1.0) + } else if bytes < MEBI_LIMIT { + ("KiB", KIBI_LIMIT_F64) + } else if bytes < GIBI_LIMIT { + ("MiB", MEBI_LIMIT_F64) + } else if bytes < TEBI_LIMIT { + ("GiB", GIBI_LIMIT_F64) + } else { + // Otherwise just use tebibytes, which is probably safe for most use cases. + ("TiB", TEBI_LIMIT_F64) + } } } /// Returns the unit type and denominator for given total amount of memory in kibibytes. -pub fn convert_mem_label(harvest: &MemHarvest) -> Option<(String, String)> { +pub fn convert_mem_label(harvest: &MemHarvest, memory_use_mega_prefix: bool) -> Option<(String, String)> { if harvest.total_bytes > 0 { Some((format!("{:3.0}%", harvest.use_percent.unwrap_or(0.0)), { - let (unit, denominator) = get_mem_binary_unit_and_denominator(harvest.total_bytes); + let (unit, denominator) = get_mem_binary_unit_and_denominator(harvest.total_bytes, memory_use_mega_prefix); format!( " {:.1}{}/{:.1}{}", @@ -614,7 +626,7 @@ pub fn convert_gpu_data(current_data: &DataCollection) -> Option, current_usage: Option, unnormalized_cpu: Option, + memory_use_mega_prefix: Option, group_processes: Option, case_sensitive: Option, whole_word: Option, @@ -271,6 +272,7 @@ pub fn init_app( left_legend: is_flag_enabled!(left_legend, matches, config), use_current_cpu_total: is_flag_enabled!(current_usage, matches, config), unnormalized_cpu: is_flag_enabled!(unnormalized_cpu, matches, config), + memory_use_mega_prefix: is_flag_enabled!(memory_use_mega_prefix, matches, config), use_basic_mode, default_time_value, time_interval: get_time_interval(matches, config, retention_ms) diff --git a/src/options/args.rs b/src/options/args.rs index 2c524da8a..ca7ee8628 100644 --- a/src/options/args.rs +++ b/src/options/args.rs @@ -403,6 +403,12 @@ use CPU (3) as the default instead. .action(ArgAction::Version) .help("Prints version information."); + let memory_use_mega_prefix = Arg::new("memory_use_mega_prefix") + .long("memory_use_mega_prefix") + .action(ArgAction::SetTrue) + .help("Displays the memory widget with a mega prefix.") + .long_help("Displays the memory widget in megabibytes instead of rounding it to the nearest prefix. Defaults to rounding it to the nearest prefix. Example: 1.2GiB will be displayed as 1228MiB."); + const VERSION: &str = match option_env!("NIGHTLY_VERSION") { Some(nightly_version) => nightly_version, None => crate_version!(), @@ -426,6 +432,7 @@ use CPU (3) as the default instead. config_location, color, mem_as_value, + memory_use_mega_prefix, default_time_value, default_widget_count, default_widget_type, From 0b7af5544897bcfa69de75de4df515e968b09b28 Mon Sep 17 00:00:00 2001 From: GrecuAlexandru Date: Mon, 8 Jan 2024 01:32:23 +0200 Subject: [PATCH 06/15] fixed formatting issues --- src/bin/main.rs | 24 ++++++++++++++++-------- src/data_conversion.rs | 11 ++++++++--- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 8181d789a..a1d8e65da 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -275,20 +275,28 @@ fn main() -> Result<()> { convert_gpu_data(&app.data_collection); } - app.converted_data.mem_labels = - convert_mem_label(&app.data_collection.memory_harvest, app.app_config_fields.memory_use_mega_prefix); - app.converted_data.swap_labels = - convert_mem_label(&app.data_collection.swap_harvest, app.app_config_fields.memory_use_mega_prefix); + app.converted_data.mem_labels = convert_mem_label( + &app.data_collection.memory_harvest, + app.app_config_fields.memory_use_mega_prefix + ); + app.converted_data.swap_labels = convert_mem_label( + &app.data_collection.swap_harvest, + app.app_config_fields.memory_use_mega_prefix + ); #[cfg(not(target_os = "windows"))] { - app.converted_data.cache_labels = - convert_mem_label(&app.data_collection.cache_harvest); + app.converted_data.cache_labels = convert_mem_label( + &app.data_collection.cache_harvest, + app.app_config_fields.memory_use_mega_prefix + ); } #[cfg(feature = "zfs")] { - let arc_labels = - convert_mem_label(&app.data_collection.arc_harvest, app.app_config_fields.memory_use_mega_prefix); + let arc_labels = convert_mem_label( + &app.data_collection.arc_harvest, + app.app_config_fields.memory_use_mega_prefix + ); app.converted_data.arc_labels = arc_labels; } } diff --git a/src/data_conversion.rs b/src/data_conversion.rs index f34022364..cee144a23 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -266,7 +266,9 @@ pub fn convert_swap_data_points(current_data: &DataCollection) -> Vec { /// /// The expected usage is to divide out the given value with the returned denominator in order to be able to use it /// with the returned binary unit (e.g. divide 3000 bytes by 1024 to have a value in KiB). -fn get_mem_binary_unit_and_denominator(bytes: u64, memory_use_mega_prefix: bool) -> (&'static str, f64) { +fn get_mem_binary_unit_and_denominator( + bytes: u64, memory_use_mega_prefix: bool +) -> (&'static str, f64) { if memory_use_mega_prefix { if bytes < KIBI_LIMIT { // Stick with bytes if under a kibibyte. @@ -295,10 +297,13 @@ fn get_mem_binary_unit_and_denominator(bytes: u64, memory_use_mega_prefix: bool) } /// Returns the unit type and denominator for given total amount of memory in kibibytes. -pub fn convert_mem_label(harvest: &MemHarvest, memory_use_mega_prefix: bool) -> Option<(String, String)> { +pub fn convert_mem_label( + harvest: &MemHarvest, memory_use_mega_prefix: bool +) -> Option<(String, String)> { if harvest.total_bytes > 0 { Some((format!("{:3.0}%", harvest.use_percent.unwrap_or(0.0)), { - let (unit, denominator) = get_mem_binary_unit_and_denominator(harvest.total_bytes, memory_use_mega_prefix); + let (unit, denominator) = + get_mem_binary_unit_and_denominator(harvest.total_bytes, memory_use_mega_prefix); format!( " {:.1}{}/{:.1}{}", From 4ef4ab936d5190fa29bae3e300104d4b232d9955 Mon Sep 17 00:00:00 2001 From: GrecuAlexandru Date: Mon, 8 Jan 2024 11:05:02 +0200 Subject: [PATCH 07/15] ran cargo fmt locally --- src/bin/main.rs | 8 ++++---- src/data_conversion.rs | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index a1d8e65da..f3e568fec 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -277,17 +277,17 @@ fn main() -> Result<()> { app.converted_data.mem_labels = convert_mem_label( &app.data_collection.memory_harvest, - app.app_config_fields.memory_use_mega_prefix + app.app_config_fields.memory_use_mega_prefix, ); app.converted_data.swap_labels = convert_mem_label( &app.data_collection.swap_harvest, - app.app_config_fields.memory_use_mega_prefix + app.app_config_fields.memory_use_mega_prefix, ); #[cfg(not(target_os = "windows"))] { app.converted_data.cache_labels = convert_mem_label( &app.data_collection.cache_harvest, - app.app_config_fields.memory_use_mega_prefix + app.app_config_fields.memory_use_mega_prefix, ); } @@ -295,7 +295,7 @@ fn main() -> Result<()> { { let arc_labels = convert_mem_label( &app.data_collection.arc_harvest, - app.app_config_fields.memory_use_mega_prefix + app.app_config_fields.memory_use_mega_prefix, ); app.converted_data.arc_labels = arc_labels; } diff --git a/src/data_conversion.rs b/src/data_conversion.rs index cee144a23..382d5c371 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -267,7 +267,7 @@ pub fn convert_swap_data_points(current_data: &DataCollection) -> Vec { /// The expected usage is to divide out the given value with the returned denominator in order to be able to use it /// with the returned binary unit (e.g. divide 3000 bytes by 1024 to have a value in KiB). fn get_mem_binary_unit_and_denominator( - bytes: u64, memory_use_mega_prefix: bool + bytes: u64, memory_use_mega_prefix: bool, ) -> (&'static str, f64) { if memory_use_mega_prefix { if bytes < KIBI_LIMIT { @@ -298,11 +298,11 @@ fn get_mem_binary_unit_and_denominator( /// Returns the unit type and denominator for given total amount of memory in kibibytes. pub fn convert_mem_label( - harvest: &MemHarvest, memory_use_mega_prefix: bool + harvest: &MemHarvest, memory_use_mega_prefix: bool, ) -> Option<(String, String)> { if harvest.total_bytes > 0 { Some((format!("{:3.0}%", harvest.use_percent.unwrap_or(0.0)), { - let (unit, denominator) = + let (unit, denominator) = get_mem_binary_unit_and_denominator(harvest.total_bytes, memory_use_mega_prefix); format!( From 87288f3997f741336c91368490a8d1f08f420655 Mon Sep 17 00:00:00 2001 From: GrecuAlexandru Date: Mon, 8 Jan 2024 11:16:56 +0200 Subject: [PATCH 08/15] fixed more formatting issues --- src/data_conversion.rs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/data_conversion.rs b/src/data_conversion.rs index 382d5c371..e8c2a07e5 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -279,20 +279,18 @@ fn get_mem_binary_unit_and_denominator( // Otherwise just use mebibytes, which is probably safe for most use cases. ("MiB", MEBI_LIMIT_F64) } + } else if bytes < KIBI_LIMIT { + // Stick with bytes if under a kibibyte. + ("B", 1.0) + } else if bytes < MEBI_LIMIT { + ("KiB", KIBI_LIMIT_F64) + } else if bytes < GIBI_LIMIT { + ("MiB", MEBI_LIMIT_F64) + } else if bytes < TEBI_LIMIT { + ("GiB", GIBI_LIMIT_F64) } else { - if bytes < KIBI_LIMIT { - // Stick with bytes if under a kibibyte. - ("B", 1.0) - } else if bytes < MEBI_LIMIT { - ("KiB", KIBI_LIMIT_F64) - } else if bytes < GIBI_LIMIT { - ("MiB", MEBI_LIMIT_F64) - } else if bytes < TEBI_LIMIT { - ("GiB", GIBI_LIMIT_F64) - } else { - // Otherwise just use tebibytes, which is probably safe for most use cases. - ("TiB", TEBI_LIMIT_F64) - } + // Otherwise just use tebibytes, which is probably safe for most use cases. + ("TiB", TEBI_LIMIT_F64) } } From 178e211889b9f4c85541de3420a1394e79846d12 Mon Sep 17 00:00:00 2001 From: GrecuAlexandru Date: Sun, 7 Jan 2024 11:45:01 +0200 Subject: [PATCH 09/15] added a feature --- src/bin/main.rs | 12 ++++++++++++ src/data_conversion.rs | 29 +++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index f3e568fec..6b63105a0 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -275,6 +275,7 @@ fn main() -> Result<()> { convert_gpu_data(&app.data_collection); } +<<<<<<< HEAD app.converted_data.mem_labels = convert_mem_label( &app.data_collection.memory_harvest, app.app_config_fields.memory_use_mega_prefix, @@ -283,6 +284,12 @@ fn main() -> Result<()> { &app.data_collection.swap_harvest, app.app_config_fields.memory_use_mega_prefix, ); +======= + app.converted_data.mem_labels = + convert_mem_label(&app.data_collection.memory_harvest, app.app_config_fields.memory_use_mega_prefix); + app.converted_data.swap_labels = + convert_mem_label(&app.data_collection.swap_harvest, app.app_config_fields.memory_use_mega_prefix); +>>>>>>> 16bcf7d8 (added a feature) #[cfg(not(target_os = "windows"))] { app.converted_data.cache_labels = convert_mem_label( @@ -293,10 +300,15 @@ fn main() -> Result<()> { #[cfg(feature = "zfs")] { +<<<<<<< HEAD let arc_labels = convert_mem_label( &app.data_collection.arc_harvest, app.app_config_fields.memory_use_mega_prefix, ); +======= + let arc_labels = + convert_mem_label(&app.data_collection.arc_harvest, app.app_config_fields.memory_use_mega_prefix); +>>>>>>> 16bcf7d8 (added a feature) app.converted_data.arc_labels = arc_labels; } } diff --git a/src/data_conversion.rs b/src/data_conversion.rs index e8c2a07e5..08aaf0236 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -266,9 +266,13 @@ pub fn convert_swap_data_points(current_data: &DataCollection) -> Vec { /// /// The expected usage is to divide out the given value with the returned denominator in order to be able to use it /// with the returned binary unit (e.g. divide 3000 bytes by 1024 to have a value in KiB). +<<<<<<< HEAD fn get_mem_binary_unit_and_denominator( bytes: u64, memory_use_mega_prefix: bool, ) -> (&'static str, f64) { +======= +fn get_mem_binary_unit_and_denominator(bytes: u64, memory_use_mega_prefix: bool) -> (&'static str, f64) { +>>>>>>> 16bcf7d8 (added a feature) if memory_use_mega_prefix { if bytes < KIBI_LIMIT { // Stick with bytes if under a kibibyte. @@ -279,6 +283,7 @@ fn get_mem_binary_unit_and_denominator( // Otherwise just use mebibytes, which is probably safe for most use cases. ("MiB", MEBI_LIMIT_F64) } +<<<<<<< HEAD } else if bytes < KIBI_LIMIT { // Stick with bytes if under a kibibyte. ("B", 1.0) @@ -288,13 +293,27 @@ fn get_mem_binary_unit_and_denominator( ("MiB", MEBI_LIMIT_F64) } else if bytes < TEBI_LIMIT { ("GiB", GIBI_LIMIT_F64) +======= +>>>>>>> 16bcf7d8 (added a feature) } else { - // Otherwise just use tebibytes, which is probably safe for most use cases. - ("TiB", TEBI_LIMIT_F64) + if bytes < KIBI_LIMIT { + // Stick with bytes if under a kibibyte. + ("B", 1.0) + } else if bytes < MEBI_LIMIT { + ("KiB", KIBI_LIMIT_F64) + } else if bytes < GIBI_LIMIT { + ("MiB", MEBI_LIMIT_F64) + } else if bytes < TEBI_LIMIT { + ("GiB", GIBI_LIMIT_F64) + } else { + // Otherwise just use tebibytes, which is probably safe for most use cases. + ("TiB", TEBI_LIMIT_F64) + } } } /// Returns the unit type and denominator for given total amount of memory in kibibytes. +<<<<<<< HEAD pub fn convert_mem_label( harvest: &MemHarvest, memory_use_mega_prefix: bool, ) -> Option<(String, String)> { @@ -302,6 +321,12 @@ pub fn convert_mem_label( Some((format!("{:3.0}%", harvest.use_percent.unwrap_or(0.0)), { let (unit, denominator) = get_mem_binary_unit_and_denominator(harvest.total_bytes, memory_use_mega_prefix); +======= +pub fn convert_mem_label(harvest: &MemHarvest, memory_use_mega_prefix: bool) -> Option<(String, String)> { + if harvest.total_bytes > 0 { + Some((format!("{:3.0}%", harvest.use_percent.unwrap_or(0.0)), { + let (unit, denominator) = get_mem_binary_unit_and_denominator(harvest.total_bytes, memory_use_mega_prefix); +>>>>>>> 16bcf7d8 (added a feature) format!( " {:.1}{}/{:.1}{}", From 7b5c689281260020c67548e3983277779e827d80 Mon Sep 17 00:00:00 2001 From: GrecuAlexandru Date: Mon, 8 Jan 2024 14:52:56 +0200 Subject: [PATCH 10/15] conflict fixes --- src/bin/main.rs | 12 ------------ src/data_conversion.rs | 38 +++++++++++--------------------------- 2 files changed, 11 insertions(+), 39 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 6b63105a0..f3e568fec 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -275,7 +275,6 @@ fn main() -> Result<()> { convert_gpu_data(&app.data_collection); } -<<<<<<< HEAD app.converted_data.mem_labels = convert_mem_label( &app.data_collection.memory_harvest, app.app_config_fields.memory_use_mega_prefix, @@ -284,12 +283,6 @@ fn main() -> Result<()> { &app.data_collection.swap_harvest, app.app_config_fields.memory_use_mega_prefix, ); -======= - app.converted_data.mem_labels = - convert_mem_label(&app.data_collection.memory_harvest, app.app_config_fields.memory_use_mega_prefix); - app.converted_data.swap_labels = - convert_mem_label(&app.data_collection.swap_harvest, app.app_config_fields.memory_use_mega_prefix); ->>>>>>> 16bcf7d8 (added a feature) #[cfg(not(target_os = "windows"))] { app.converted_data.cache_labels = convert_mem_label( @@ -300,15 +293,10 @@ fn main() -> Result<()> { #[cfg(feature = "zfs")] { -<<<<<<< HEAD let arc_labels = convert_mem_label( &app.data_collection.arc_harvest, app.app_config_fields.memory_use_mega_prefix, ); -======= - let arc_labels = - convert_mem_label(&app.data_collection.arc_harvest, app.app_config_fields.memory_use_mega_prefix); ->>>>>>> 16bcf7d8 (added a feature) app.converted_data.arc_labels = arc_labels; } } diff --git a/src/data_conversion.rs b/src/data_conversion.rs index 08aaf0236..d914d1e78 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -266,13 +266,9 @@ pub fn convert_swap_data_points(current_data: &DataCollection) -> Vec { /// /// The expected usage is to divide out the given value with the returned denominator in order to be able to use it /// with the returned binary unit (e.g. divide 3000 bytes by 1024 to have a value in KiB). -<<<<<<< HEAD fn get_mem_binary_unit_and_denominator( bytes: u64, memory_use_mega_prefix: bool, ) -> (&'static str, f64) { -======= -fn get_mem_binary_unit_and_denominator(bytes: u64, memory_use_mega_prefix: bool) -> (&'static str, f64) { ->>>>>>> 16bcf7d8 (added a feature) if memory_use_mega_prefix { if bytes < KIBI_LIMIT { // Stick with bytes if under a kibibyte. @@ -283,7 +279,6 @@ fn get_mem_binary_unit_and_denominator(bytes: u64, memory_use_mega_prefix: bool) // Otherwise just use mebibytes, which is probably safe for most use cases. ("MiB", MEBI_LIMIT_F64) } -<<<<<<< HEAD } else if bytes < KIBI_LIMIT { // Stick with bytes if under a kibibyte. ("B", 1.0) @@ -293,27 +288,22 @@ fn get_mem_binary_unit_and_denominator(bytes: u64, memory_use_mega_prefix: bool) ("MiB", MEBI_LIMIT_F64) } else if bytes < TEBI_LIMIT { ("GiB", GIBI_LIMIT_F64) -======= ->>>>>>> 16bcf7d8 (added a feature) + } else if bytes < KIBI_LIMIT { + // Stick with bytes if under a kibibyte. + ("B", 1.0) + } else if bytes < MEBI_LIMIT { + ("KiB", KIBI_LIMIT_F64) + } else if bytes < GIBI_LIMIT { + ("MiB", MEBI_LIMIT_F64) + } else if bytes < TEBI_LIMIT { + ("GiB", GIBI_LIMIT_F64) } else { - if bytes < KIBI_LIMIT { - // Stick with bytes if under a kibibyte. - ("B", 1.0) - } else if bytes < MEBI_LIMIT { - ("KiB", KIBI_LIMIT_F64) - } else if bytes < GIBI_LIMIT { - ("MiB", MEBI_LIMIT_F64) - } else if bytes < TEBI_LIMIT { - ("GiB", GIBI_LIMIT_F64) - } else { - // Otherwise just use tebibytes, which is probably safe for most use cases. - ("TiB", TEBI_LIMIT_F64) - } + // Otherwise just use tebibytes, which is probably safe for most use cases. + ("TiB", TEBI_LIMIT_F64) } } /// Returns the unit type and denominator for given total amount of memory in kibibytes. -<<<<<<< HEAD pub fn convert_mem_label( harvest: &MemHarvest, memory_use_mega_prefix: bool, ) -> Option<(String, String)> { @@ -321,12 +311,6 @@ pub fn convert_mem_label( Some((format!("{:3.0}%", harvest.use_percent.unwrap_or(0.0)), { let (unit, denominator) = get_mem_binary_unit_and_denominator(harvest.total_bytes, memory_use_mega_prefix); -======= -pub fn convert_mem_label(harvest: &MemHarvest, memory_use_mega_prefix: bool) -> Option<(String, String)> { - if harvest.total_bytes > 0 { - Some((format!("{:3.0}%", harvest.use_percent.unwrap_or(0.0)), { - let (unit, denominator) = get_mem_binary_unit_and_denominator(harvest.total_bytes, memory_use_mega_prefix); ->>>>>>> 16bcf7d8 (added a feature) format!( " {:.1}{}/{:.1}{}", From 1c2a7d0e4f886586252369b231877decd5923f67 Mon Sep 17 00:00:00 2001 From: GrecuAlexandru Date: Mon, 8 Jan 2024 15:07:10 +0200 Subject: [PATCH 11/15] little fix to my else if statements --- src/data_conversion.rs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/data_conversion.rs b/src/data_conversion.rs index d914d1e78..e8c2a07e5 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -288,15 +288,6 @@ fn get_mem_binary_unit_and_denominator( ("MiB", MEBI_LIMIT_F64) } else if bytes < TEBI_LIMIT { ("GiB", GIBI_LIMIT_F64) - } else if bytes < KIBI_LIMIT { - // Stick with bytes if under a kibibyte. - ("B", 1.0) - } else if bytes < MEBI_LIMIT { - ("KiB", KIBI_LIMIT_F64) - } else if bytes < GIBI_LIMIT { - ("MiB", MEBI_LIMIT_F64) - } else if bytes < TEBI_LIMIT { - ("GiB", GIBI_LIMIT_F64) } else { // Otherwise just use tebibytes, which is probably safe for most use cases. ("TiB", TEBI_LIMIT_F64) From eedc8536decac3714834bc5ad7f4343dde2a89f2 Mon Sep 17 00:00:00 2001 From: GrecuAlexandru Date: Tue, 23 Jan 2024 23:01:51 +0200 Subject: [PATCH 12/15] Added possibility to use "--change_memory_prefix" --- .../configuration/config-file/flags.md | 1 + sample_configs/default_config.toml | 6 ++ src/app.rs | 2 +- src/bin/main.rs | 20 ++---- src/constants.rs | 8 ++- src/data_collection.rs | 8 +-- src/data_conversion.rs | 67 +++++++------------ src/options.rs | 19 +++++- src/options/args.rs | 8 +-- 9 files changed, 69 insertions(+), 70 deletions(-) diff --git a/docs/content/configuration/config-file/flags.md b/docs/content/configuration/config-file/flags.md index ed9d0aefb..0fa99a63d 100644 --- a/docs/content/configuration/config-file/flags.md +++ b/docs/content/configuration/config-file/flags.md @@ -42,3 +42,4 @@ each time: | `retention` | String (human readable time, such as "10m", "1h", etc.) | How much data is stored at once in terms of time. | | `unnormalized_cpu` | Boolean | Show process CPU% without normalizing over the number of cores. | | `expanded_on_startup` | Boolean | Expand the default widget upon starting the app. | +| `change_memory_prefix` | String (one of ["KiB", "MiB", "GiB", "TiB", "auto"]) | Change the memory prefix. | diff --git a/sample_configs/default_config.toml b/sample_configs/default_config.toml index a7731dd66..5bd497f69 100644 --- a/sample_configs/default_config.toml +++ b/sample_configs/default_config.toml @@ -27,6 +27,12 @@ #whole_word = false # Whether to make process searching use regex by default. #regex = false +# What prefix should be used for memory. Defaults to "auto". Prefix is one of: +#change_memory_prefix = "auto" +#change_memory_prefix = "KiB" +#change_memory_prefix = "MiB" +#change_memory_prefix = "GiB" +#change_memory_prefix = "TiB" # Defaults to Celsius. Temperature is one of: #temperature_type = "k" #temperature_type = "f" diff --git a/src/app.rs b/src/app.rs index 6d70a3cff..691275719 100644 --- a/src/app.rs +++ b/src/app.rs @@ -50,7 +50,6 @@ pub struct AppConfigFields { pub show_average_cpu: bool, // TODO: Unify this in CPU options pub use_current_cpu_total: bool, pub unnormalized_cpu: bool, - pub memory_use_mega_prefix: bool, pub use_basic_mode: bool, pub default_time_value: u64, pub time_interval: u64, @@ -63,6 +62,7 @@ pub struct AppConfigFields { pub enable_cache_memory: bool, pub show_table_scroll_position: bool, pub is_advanced_kill: bool, + pub change_memory_prefix: String, // TODO: Remove these, move network details state-side. pub network_unit_type: DataUnit, pub network_scale_type: AxisScaling, diff --git a/src/bin/main.rs b/src/bin/main.rs index 6b63105a0..637cd2b36 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -275,40 +275,28 @@ fn main() -> Result<()> { convert_gpu_data(&app.data_collection); } -<<<<<<< HEAD app.converted_data.mem_labels = convert_mem_label( &app.data_collection.memory_harvest, - app.app_config_fields.memory_use_mega_prefix, + app.app_config_fields.change_memory_prefix.clone(), ); app.converted_data.swap_labels = convert_mem_label( &app.data_collection.swap_harvest, - app.app_config_fields.memory_use_mega_prefix, + app.app_config_fields.change_memory_prefix.clone(), ); -======= - app.converted_data.mem_labels = - convert_mem_label(&app.data_collection.memory_harvest, app.app_config_fields.memory_use_mega_prefix); - app.converted_data.swap_labels = - convert_mem_label(&app.data_collection.swap_harvest, app.app_config_fields.memory_use_mega_prefix); ->>>>>>> 16bcf7d8 (added a feature) #[cfg(not(target_os = "windows"))] { app.converted_data.cache_labels = convert_mem_label( &app.data_collection.cache_harvest, - app.app_config_fields.memory_use_mega_prefix, + app.app_config_fields.change_memory_prefix.clone(), ); } #[cfg(feature = "zfs")] { -<<<<<<< HEAD let arc_labels = convert_mem_label( &app.data_collection.arc_harvest, - app.app_config_fields.memory_use_mega_prefix, + app.app_config_fields.change_memory_prefix.clone(), ); -======= - let arc_labels = - convert_mem_label(&app.data_collection.arc_harvest, app.app_config_fields.memory_use_mega_prefix); ->>>>>>> 16bcf7d8 (added a feature) app.converted_data.arc_labels = arc_labels; } } diff --git a/src/constants.rs b/src/constants.rs index 94e3aeb03..95c8edd20 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -545,8 +545,12 @@ pub const CONFIG_TEXT: &str = r#"# This is a default config file for bottom. Al #whole_word = false # Whether to make process searching use regex by default. #regex = false -# Whether to display memory usage in megabibytes (MiB) or gigabibytes (GiB). -#memory_use_mega_prefix = false +# What prefix should be used for memory. Defaults to "auto". Prefix is one of: +#change_memory_prefix = "auto" +#change_memory_prefix = "KiB" +#change_memory_prefix = "MiB" +#change_memory_prefix = "GiB" +#change_memory_prefix = "TiB" # Defaults to Celsius. Temperature is one of: #temperature_type = "k" #temperature_type = "f" diff --git a/src/data_collection.rs b/src/data_collection.rs index cc370e149..5aee28b2d 100644 --- a/src/data_collection.rs +++ b/src/data_collection.rs @@ -104,7 +104,7 @@ pub struct DataCollector { temperature_type: TemperatureType, use_current_cpu_total: bool, unnormalized_cpu: bool, - memory_use_mega_prefix: bool, + change_memory_prefix: String, last_collection_time: Instant, total_rx: u64, total_tx: u64, @@ -147,7 +147,7 @@ impl DataCollector { temperature_type: TemperatureType::Celsius, use_current_cpu_total: false, unnormalized_cpu: false, - memory_use_mega_prefix: false, + change_memory_prefix: String::from("auto"), last_collection_time: Instant::now(), total_rx: 0, total_tx: 0, @@ -228,8 +228,8 @@ impl DataCollector { self.unnormalized_cpu = unnormalized_cpu; } - pub fn set_memory_use_mega_prefix(&mut self, memory_use_mega_prefix: bool) { - self.memory_use_mega_prefix = memory_use_mega_prefix; + pub fn set_change_memory_prefix(&mut self, change_memory_prefix: String) { + self.change_memory_prefix = change_memory_prefix; } pub fn set_show_average_cpu(&mut self, show_average_cpu: bool) { diff --git a/src/data_conversion.rs b/src/data_conversion.rs index 08aaf0236..c4f8b6c3c 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -266,24 +266,30 @@ pub fn convert_swap_data_points(current_data: &DataCollection) -> Vec { /// /// The expected usage is to divide out the given value with the returned denominator in order to be able to use it /// with the returned binary unit (e.g. divide 3000 bytes by 1024 to have a value in KiB). -<<<<<<< HEAD fn get_mem_binary_unit_and_denominator( - bytes: u64, memory_use_mega_prefix: bool, + bytes: u64, change_memory_prefix: String, ) -> (&'static str, f64) { -======= -fn get_mem_binary_unit_and_denominator(bytes: u64, memory_use_mega_prefix: bool) -> (&'static str, f64) { ->>>>>>> 16bcf7d8 (added a feature) - if memory_use_mega_prefix { - if bytes < KIBI_LIMIT { - // Stick with bytes if under a kibibyte. - ("B", 1.0) - } else if bytes < MEBI_LIMIT { - ("KiB", KIBI_LIMIT_F64) - } else { - // Otherwise just use mebibytes, which is probably safe for most use cases. - ("MiB", MEBI_LIMIT_F64) - } -<<<<<<< HEAD + if change_memory_prefix != "auto" { + return match change_memory_prefix.as_str() { + "KiB" => ("KiB", KIBI_LIMIT_F64), + "MiB" => ("MiB", MEBI_LIMIT_F64), + "GiB" => ("GiB", GIBI_LIMIT_F64), + "TiB" => ("TiB", TEBI_LIMIT_F64), + _ => { + if bytes < KIBI_LIMIT { + // Stick with bytes if under a kibibyte. + ("B", 1.0) + } else if bytes < MEBI_LIMIT { + ("KiB", KIBI_LIMIT_F64) + } else if bytes < GIBI_LIMIT { + ("MiB", MEBI_LIMIT_F64) + } else if bytes < TEBI_LIMIT { + ("GiB", GIBI_LIMIT_F64) + } else { + ("TiB", TEBI_LIMIT_F64) + } + }, + }; } else if bytes < KIBI_LIMIT { // Stick with bytes if under a kibibyte. ("B", 1.0) @@ -293,40 +299,19 @@ fn get_mem_binary_unit_and_denominator(bytes: u64, memory_use_mega_prefix: bool) ("MiB", MEBI_LIMIT_F64) } else if bytes < TEBI_LIMIT { ("GiB", GIBI_LIMIT_F64) -======= ->>>>>>> 16bcf7d8 (added a feature) } else { - if bytes < KIBI_LIMIT { - // Stick with bytes if under a kibibyte. - ("B", 1.0) - } else if bytes < MEBI_LIMIT { - ("KiB", KIBI_LIMIT_F64) - } else if bytes < GIBI_LIMIT { - ("MiB", MEBI_LIMIT_F64) - } else if bytes < TEBI_LIMIT { - ("GiB", GIBI_LIMIT_F64) - } else { - // Otherwise just use tebibytes, which is probably safe for most use cases. - ("TiB", TEBI_LIMIT_F64) - } + ("TiB", TEBI_LIMIT_F64) } } /// Returns the unit type and denominator for given total amount of memory in kibibytes. -<<<<<<< HEAD pub fn convert_mem_label( - harvest: &MemHarvest, memory_use_mega_prefix: bool, + harvest: &MemHarvest, change_memory_prefix: String, ) -> Option<(String, String)> { if harvest.total_bytes > 0 { Some((format!("{:3.0}%", harvest.use_percent.unwrap_or(0.0)), { let (unit, denominator) = - get_mem_binary_unit_and_denominator(harvest.total_bytes, memory_use_mega_prefix); -======= -pub fn convert_mem_label(harvest: &MemHarvest, memory_use_mega_prefix: bool) -> Option<(String, String)> { - if harvest.total_bytes > 0 { - Some((format!("{:3.0}%", harvest.use_percent.unwrap_or(0.0)), { - let (unit, denominator) = get_mem_binary_unit_and_denominator(harvest.total_bytes, memory_use_mega_prefix); ->>>>>>> 16bcf7d8 (added a feature) + get_mem_binary_unit_and_denominator(harvest.total_bytes, change_memory_prefix); format!( " {:.1}{}/{:.1}{}", @@ -654,7 +639,7 @@ pub fn convert_gpu_data(current_data: &DataCollection) -> Option, dot_marker: Option, temperature_type: Option, + change_memory: Option, rate: Option, left_legend: Option, current_usage: Option, unnormalized_cpu: Option, - memory_use_mega_prefix: Option, + change_memory_prefix: Option, group_processes: Option, case_sensitive: Option, whole_word: Option, @@ -272,7 +273,8 @@ pub fn init_app( left_legend: is_flag_enabled!(left_legend, matches, config), use_current_cpu_total: is_flag_enabled!(current_usage, matches, config), unnormalized_cpu: is_flag_enabled!(unnormalized_cpu, matches, config), - memory_use_mega_prefix: is_flag_enabled!(memory_use_mega_prefix, matches, config), + change_memory_prefix: get_change_memory_prefix(matches, config) + .context("Update 'change_memory_prefix' in your config file.")?, use_basic_mode, default_time_value, time_interval: get_time_interval(matches, config, retention_ms) @@ -617,6 +619,19 @@ fn get_show_average_cpu(matches: &ArgMatches, config: &Config) -> bool { true } +fn get_change_memory_prefix(matches: &ArgMatches, config: &Config) -> error::Result { + if let Some(change_memory_prefix) = matches.get_one::("change_memory_prefix") { + Ok(change_memory_prefix.to_string()) + } else if let Some(flags) = &config.flags { + if let Some(prefix) = &flags.change_memory_prefix { + Ok(prefix.clone()) + } else { + Ok("auto".to_string()) + } + } else { + Ok("auto".to_string()) + } +} fn try_parse_ms(s: &str) -> error::Result { if let Ok(val) = humantime::parse_duration(s) { Ok(val.as_millis().try_into()?) diff --git a/src/options/args.rs b/src/options/args.rs index ca7ee8628..c69b74d55 100644 --- a/src/options/args.rs +++ b/src/options/args.rs @@ -403,9 +403,9 @@ use CPU (3) as the default instead. .action(ArgAction::Version) .help("Prints version information."); - let memory_use_mega_prefix = Arg::new("memory_use_mega_prefix") - .long("memory_use_mega_prefix") - .action(ArgAction::SetTrue) + let change_memory_prefix = Arg::new("change_memory_prefix") + .long("change_memory_prefix") + .action(ArgAction::Set) .help("Displays the memory widget with a mega prefix.") .long_help("Displays the memory widget in megabibytes instead of rounding it to the nearest prefix. Defaults to rounding it to the nearest prefix. Example: 1.2GiB will be displayed as 1228MiB."); @@ -432,7 +432,7 @@ use CPU (3) as the default instead. config_location, color, mem_as_value, - memory_use_mega_prefix, + change_memory_prefix, default_time_value, default_widget_count, default_widget_type, From 8ed907d69052119bc589997aed03df0eac412405 Mon Sep 17 00:00:00 2001 From: GrecuAlexandru Date: Tue, 23 Jan 2024 23:05:33 +0200 Subject: [PATCH 13/15] cargo fmt --- src/data_conversion.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/data_conversion.rs b/src/data_conversion.rs index c4f8b6c3c..6920751d2 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -288,7 +288,7 @@ fn get_mem_binary_unit_and_denominator( } else { ("TiB", TEBI_LIMIT_F64) } - }, + } }; } else if bytes < KIBI_LIMIT { // Stick with bytes if under a kibibyte. @@ -638,8 +638,10 @@ pub fn convert_gpu_data(current_data: &DataCollection) -> Option Date: Tue, 23 Jan 2024 23:15:15 +0200 Subject: [PATCH 14/15] resolved more problems --- src/data_conversion.rs | 23 +---------------------- 1 file changed, 1 insertion(+), 22 deletions(-) diff --git a/src/data_conversion.rs b/src/data_conversion.rs index 0d32b1471..973431b85 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -723,25 +723,4 @@ mod test { "10.4TB/s".to_string() ); } -} -s_per_second_string(GIGA_LIMIT), - "1.0GB/s".to_string() - ); - assert_eq!( - dec_bytes_per_second_string(2 * GIGA_LIMIT), - "2.0GB/s".to_string() - ); - assert_eq!( - dec_bytes_per_second_string((2.5 * GIGA_LIMIT as f64) as u64), - "2.5GB/s".to_string() - ); - assert_eq!( - dec_bytes_per_second_string((10.34 * TERA_LIMIT as f64) as u64), - "10.3TB/s".to_string() - ); - assert_eq!( - dec_bytes_per_second_string((10.36 * TERA_LIMIT as f64) as u64), - "10.4TB/s".to_string() - ); - } -} +} \ No newline at end of file From f14993ae4b77f27062e02a7e2d4bd14acef7c2a0 Mon Sep 17 00:00:00 2001 From: GrecuAlexandru Date: Tue, 23 Jan 2024 23:18:29 +0200 Subject: [PATCH 15/15] fixed some formatting --- src/data_conversion.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/data_conversion.rs b/src/data_conversion.rs index 973431b85..6920751d2 100644 --- a/src/data_conversion.rs +++ b/src/data_conversion.rs @@ -723,4 +723,4 @@ mod test { "10.4TB/s".to_string() ); } -} \ No newline at end of file +}