mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 15:44:59 +02:00
Make config.filesize_format
/config.filesize_metric
conflict resolution consistent (#7410)
# Description Currently, `filesize_format`/`filesize_metric` conflicts are resolved as follows: if the `filesize_format` ends in "ib", then that overrides `filesize_metric`, otherwise, `filesize_metric` overrides `filesize_format`. This removes this difficult-to-predict asymmetric behaviour, and makes it so that `filesize_metric` always overrides `filesize_format`. This also adds tests for `$env.config.filesize.format` and `$env.config.filesize.metric` values. REMINDER: `filesize_metric` means "increments of 1000", and refers to KB-MB-GB-TB etc. # User-Facing Changes See above. # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
This commit is contained in:
@ -3211,25 +3211,35 @@ pub fn format_duration_as_timeperiod(duration: i64) -> (i32, Vec<TimePeriod>) {
|
||||
|
||||
pub fn format_filesize_from_conf(num_bytes: i64, config: &Config) -> String {
|
||||
// We need to take into account config.filesize_metric so, if someone asks for KB
|
||||
// filesize_metric is true, return KiB
|
||||
// and filesize_metric is false, return KiB
|
||||
format_filesize(
|
||||
num_bytes,
|
||||
config.filesize_format.as_str(),
|
||||
config.filesize_metric,
|
||||
Some(config.filesize_metric),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn format_filesize(num_bytes: i64, format_value: &str, filesize_metric: bool) -> String {
|
||||
// filesize_metric is explicit when printed a value according to user config;
|
||||
// other places (such as `format filesize`) don't.
|
||||
pub fn format_filesize(
|
||||
num_bytes: i64,
|
||||
format_value: &str,
|
||||
filesize_metric: Option<bool>,
|
||||
) -> String {
|
||||
// Allow the user to specify how they want their numbers formatted
|
||||
|
||||
// When format_value is "auto" or an invalid value, the returned ByteUnit doesn't matter
|
||||
// and is always B.
|
||||
let filesize_format_var = get_filesize_format(format_value, filesize_metric);
|
||||
|
||||
let byte = byte_unit::Byte::from_bytes(num_bytes.unsigned_abs() as u128);
|
||||
let adj_byte =
|
||||
if filesize_format_var.0 == byte_unit::ByteUnit::B && filesize_format_var.1 == "auto" {
|
||||
byte.get_appropriate_unit(!filesize_metric)
|
||||
} else {
|
||||
byte.get_adjusted_unit(filesize_format_var.0)
|
||||
};
|
||||
let adj_byte = if filesize_format_var.1 == "auto" {
|
||||
// When filesize_metric is None, format_value should never be "auto", so this
|
||||
// unwrap_or() should always work.
|
||||
byte.get_appropriate_unit(!filesize_metric.unwrap_or(false))
|
||||
} else {
|
||||
byte.get_adjusted_unit(filesize_format_var.0)
|
||||
};
|
||||
|
||||
match adj_byte.get_unit() {
|
||||
byte_unit::ByteUnit::B => {
|
||||
@ -3258,65 +3268,36 @@ pub fn format_filesize(num_bytes: i64, format_value: &str, filesize_metric: bool
|
||||
}
|
||||
}
|
||||
|
||||
fn get_filesize_format(format_value: &str, filesize_metric: bool) -> (ByteUnit, &str) {
|
||||
fn get_filesize_format(format_value: &str, filesize_metric: Option<bool>) -> (ByteUnit, &str) {
|
||||
macro_rules! either {
|
||||
($in:ident, $metric:ident, $binary:ident) => {
|
||||
(
|
||||
// filesize_metric always overrides the unit of
|
||||
// filesize_format.
|
||||
match filesize_metric {
|
||||
Some(true) => byte_unit::ByteUnit::$metric,
|
||||
Some(false) => byte_unit::ByteUnit::$binary,
|
||||
None => {
|
||||
if $in.ends_with("ib") {
|
||||
byte_unit::ByteUnit::$binary
|
||||
} else {
|
||||
byte_unit::ByteUnit::$metric
|
||||
}
|
||||
}
|
||||
},
|
||||
"",
|
||||
)
|
||||
};
|
||||
}
|
||||
match format_value {
|
||||
"b" => (byte_unit::ByteUnit::B, ""),
|
||||
"kb" => {
|
||||
if filesize_metric {
|
||||
(byte_unit::ByteUnit::KiB, "")
|
||||
} else {
|
||||
(byte_unit::ByteUnit::KB, "")
|
||||
}
|
||||
}
|
||||
"kib" => (byte_unit::ByteUnit::KiB, ""),
|
||||
"mb" => {
|
||||
if filesize_metric {
|
||||
(byte_unit::ByteUnit::MiB, "")
|
||||
} else {
|
||||
(byte_unit::ByteUnit::MB, "")
|
||||
}
|
||||
}
|
||||
"mib" => (byte_unit::ByteUnit::MiB, ""),
|
||||
"gb" => {
|
||||
if filesize_metric {
|
||||
(byte_unit::ByteUnit::GiB, "")
|
||||
} else {
|
||||
(byte_unit::ByteUnit::GB, "")
|
||||
}
|
||||
}
|
||||
"gib" => (byte_unit::ByteUnit::GiB, ""),
|
||||
"tb" => {
|
||||
if filesize_metric {
|
||||
(byte_unit::ByteUnit::TiB, "")
|
||||
} else {
|
||||
(byte_unit::ByteUnit::TB, "")
|
||||
}
|
||||
}
|
||||
"tib" => (byte_unit::ByteUnit::TiB, ""),
|
||||
"pb" => {
|
||||
if filesize_metric {
|
||||
(byte_unit::ByteUnit::PiB, "")
|
||||
} else {
|
||||
(byte_unit::ByteUnit::PB, "")
|
||||
}
|
||||
}
|
||||
"pib" => (byte_unit::ByteUnit::PiB, ""),
|
||||
"eb" => {
|
||||
if filesize_metric {
|
||||
(byte_unit::ByteUnit::EiB, "")
|
||||
} else {
|
||||
(byte_unit::ByteUnit::EB, "")
|
||||
}
|
||||
}
|
||||
"eib" => (byte_unit::ByteUnit::EiB, ""),
|
||||
"zb" => {
|
||||
if filesize_metric {
|
||||
(byte_unit::ByteUnit::ZiB, "")
|
||||
} else {
|
||||
(byte_unit::ByteUnit::ZB, "")
|
||||
}
|
||||
}
|
||||
"zib" => (byte_unit::ByteUnit::ZiB, ""),
|
||||
"kb" | "kib" => either!(format_value, KB, KiB),
|
||||
"mb" | "mib" => either!(format_value, MB, MiB),
|
||||
"gb" | "gib" => either!(format_value, GB, GiB),
|
||||
"tb" | "tib" => either!(format_value, TB, TiB),
|
||||
"pb" | "pib" => either!(format_value, TB, TiB),
|
||||
"eb" | "eib" => either!(format_value, EB, EiB),
|
||||
"zb" | "zib" => either!(format_value, ZB, ZiB),
|
||||
_ => (byte_unit::ByteUnit::B, "auto"),
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user