Skip to content

Commit

Permalink
formatting(eng): add show parameter
Browse files Browse the repository at this point in the history
Fixes #2113

Example usage (will show the icon but only if $status >= 1):

```toml
[[block]]
block = "maildir"
format = " $icon $status.eng(range:1..,show:false)|"
```
  • Loading branch information
MaxVerevkin authored and bim9262 committed Jan 5, 2025
1 parent 99b09f1 commit 2cab976
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
//! `force_prefix` | force the prefix value instead of setting a "minimal prefix" | `false`
//! `pad_with` | the character that is used to pad the number to be `width` long | ` ` (a space)
//! `range` | a range of allowed values, in the format `<start>..<end>`, inclusive. Both start and end are optional. Can be used to, for example, hide the block when the value is not in a given range. | `..`
//! `show` | show this value. Can be used with `range` for conditional formatting | `true`
//!
//! ## `bar` - Display numbers as progress bars
//!
Expand Down
9 changes: 9 additions & 0 deletions src/formatting/formatter/eng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::*;
const DEFAULT_NUMBER_WIDTH: usize = 2;

pub const DEFAULT_NUMBER_FORMATTER: EngFormatter = EngFormatter {
show: true,
width: DEFAULT_NUMBER_WIDTH,
unit: None,
unit_has_space: false,
Expand All @@ -23,6 +24,7 @@ pub const DEFAULT_NUMBER_FORMATTER: EngFormatter = EngFormatter {

#[derive(Debug)]
pub struct EngFormatter {
show: bool,
width: usize,
unit: Option<Unit>,
unit_has_space: bool,
Expand Down Expand Up @@ -105,6 +107,9 @@ impl EngFormatter {
..=end.parse::<f64>().error("invalid range end")?;
}
}
"show" => {
result.show = arg.val.parse().ok().error("show must be true or false")?;
}
other => {
return Err(Error::new(format!("Unknown argument for 'eng': '{other}'")));
}
Expand All @@ -123,6 +128,10 @@ impl Formatter for EngFormatter {
return Err(FormatError::NumberOutOfRange(val));
}

if !self.show {
return Ok(String::new());
}

let is_negative = val.is_sign_negative();
if is_negative {
val = -val;
Expand Down

0 comments on commit 2cab976

Please sign in to comment.