Skip to content

Commit

Permalink
Add duration format to battery block
Browse files Browse the repository at this point in the history
  • Loading branch information
bim9262 committed May 6, 2024
1 parent 0d867e9 commit 04cbd5d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
19 changes: 12 additions & 7 deletions src/blocks/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
//! -------------|-------------------------------------------------------------------------|-------------------|-----
//! `icon` | Icon based on battery's state | Icon | -
//! `percentage` | Battery level, in percent | Number | Percents
//! `time` | Time remaining until (dis)charge is complete. Presented only if battery's status is (dis)charging. | String | -
//! `time` | Time remaining until (dis)charge is complete. Presented only if battery's status is (dis)charging. | Duration or String *DEPRECATED* | -
//! `power` | Power consumption by the battery or from the power supply when charging | String or Float | Watts
//!
//! formatting `time` with the `str` formatter has been deprecated in favor of the `dur` formatter.
//!
//! # Examples
//!
//! Basic usage:
Expand All @@ -44,7 +46,7 @@
//! ```toml
//! [[block]]
//! block = "battery"
//! format = " $percentage {$time |}"
//! format = " $percentage {$time.dur(hms:true, min_unit:m) |}"
//! device = "DisplayDevice"
//! driver = "upower"
//! ```
Expand Down Expand Up @@ -164,11 +166,14 @@ pub async fn run(config: &Config, api: &CommonApi) -> Result<()> {
info.time_remaining.map(|t| {
values.insert(
"time".into(),
Value::text(format!(
"{}:{:02}",
(t / 3600.) as i32,
(t % 3600. / 60.) as i32
)),
Value::duration_with_str(
Duration::from_secs(t as u64),
Some(format!(
"{}:{:02}",
(t / 3600.) as i32,
(t % 3600. / 60.) as i32
)),
),
)
});

Expand Down
4 changes: 2 additions & 2 deletions src/formatting/formatter/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl DurationFormatter {
impl Formatter for DurationFormatter {
fn format(&self, val: &Value, _config: &SharedConfig) -> Result<String, FormatError> {
match val {
Value::Duration(duration) => {
Value::Duration(duration, _) => {
let mut v = self.get_time_parts(duration.as_millis());

if self.round_up {
Expand Down Expand Up @@ -277,7 +277,7 @@ mod tests {
.expect("unit must be one of \"y\", \"w\", \"d\", \"h\", \"m\", \"s\", or \"ms\"")]
as u64);
)*
Value::Duration(std::time::Duration::from_millis(ms))
Value::Duration(std::time::Duration::from_millis(ms), None)
}};
}

Expand Down
2 changes: 1 addition & 1 deletion src/formatting/formatter/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl StrFormatter {
impl Formatter for StrFormatter {
fn format(&self, val: &Value, config: &SharedConfig) -> Result<String, FormatError> {
match val {
Value::Text(text) => {
Value::Text(text) | Value::Duration(_, Some(text)) => {
let text: Vec<&str> = text.graphemes(true).collect();
let width = text.len();
Ok(match (self.rot_interval_ms, self.init_time) {
Expand Down
8 changes: 6 additions & 2 deletions src/formatting/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum ValueInner {
Icon(Cow<'static, str>, Option<f64>),
Number { val: f64, unit: Unit },
Datetime(DateTime<Utc>, Option<Tz>),
Duration(Duration),
Duration(Duration, Option<String>),
Flag,
}

Expand Down Expand Up @@ -71,7 +71,11 @@ impl Value {
}

pub fn duration(duration: Duration) -> Self {
Self::new(ValueInner::Duration(duration))
Self::new(ValueInner::Duration(duration, None))
}

pub fn duration_with_str(duration: Duration, duration_string: Option<String>) -> Self {
Self::new(ValueInner::Duration(duration, duration_string))
}

pub fn icon<S>(name: S) -> Self
Expand Down

0 comments on commit 04cbd5d

Please sign in to comment.