From 16bcf7d820d82a60aa6da015e762baacd87a3af6 Mon Sep 17 00:00:00 2001 From: GrecuAlexandru Date: Sun, 7 Jan 2024 11:45:01 +0200 Subject: [PATCH 1/4] 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 2/4] 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 3/4] 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 4/4] 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) } }