Skip to content

Commit

Permalink
added a feature
Browse files Browse the repository at this point in the history
  • Loading branch information
GrecuAlexandru committed Jan 7, 2024
1 parent 0c161ae commit 16bcf7d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 18 deletions.
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 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,
Expand Down
6 changes: 3 additions & 3 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions src/data_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down
42 changes: 27 additions & 15 deletions src/data_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,27 +266,39 @@ pub fn convert_swap_data_points(current_data: &DataCollection) -> Vec<Point> {
///
/// 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}{}",
Expand Down Expand Up @@ -614,7 +626,7 @@ pub fn convert_gpu_data(current_data: &DataCollection) -> Option<Vec<ConvertedGp
mem_percent: format!("{:3.0}%", gpu.1.use_percent.unwrap_or(0.0)),
mem_total: {
let (unit, denominator) =
get_mem_binary_unit_and_denominator(gpu.1.total_bytes);
get_mem_binary_unit_and_denominator(gpu.1.total_bytes, false);

format!(
" {:.1}{unit}/{:.1}{unit}",
Expand Down
2 changes: 2 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub struct ConfigFlags {
left_legend: Option<bool>,
current_usage: Option<bool>,
unnormalized_cpu: Option<bool>,
memory_use_mega_prefix: Option<bool>,
group_processes: Option<bool>,
case_sensitive: Option<bool>,
whole_word: Option<bool>,
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions src/options/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
Expand All @@ -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,
Expand Down

0 comments on commit 16bcf7d

Please sign in to comment.