From 89bcfa66ad4165369b648b53b2ff040d11a8162f Mon Sep 17 00:00:00 2001 From: Michael Palmos Date: Mon, 20 Dec 2021 12:40:48 +1000 Subject: [PATCH] Add percentage value markup tag: #40 --- src/bus/dbus.rs | 8 ++++---- src/maths_utility.rs | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/bus/dbus.rs b/src/bus/dbus.rs index 3cd280d..7aa4138 100644 --- a/src/bus/dbus.rs +++ b/src/bus/dbus.rs @@ -400,10 +400,10 @@ impl Notification { let percentage: Option; if let Some(value) = arg::prop_cast::(&hints, "value") { - let v = f64::from(*value); - let p = f64::clamp(v * 0.01, 0.0, 1.0); - // This conversion should not be lossy, since the maximum precision is 0.01 (1%). - percentage = Some(p as f32) + // This should be ok since we only support values from 0 to 100. + let v = *value as f32; + let p = f32::clamp(v * 0.01, 0.0, 1.0); + percentage = Some(p) } else { percentage = None; } diff --git a/src/maths_utility.rs b/src/maths_utility.rs index ade206d..825819d 100644 --- a/src/maths_utility.rs +++ b/src/maths_utility.rs @@ -430,12 +430,25 @@ pub fn format_action_notification_string(format_string: &str, action_name: &str, i += 2 + len; continue; } + "%p" => { + let percentage_100 = notification.percentage.map(|p| (p * 100.0)); + let string = if let Some(p) = percentage_100 { + format!("{:.0}", p) + } else { + "None".to_owned() + }; + + formatted.extend_from_slice(string.as_bytes()); + i += 2; + + continue + } + _ => (), "%s" => { formatted.extend_from_slice(notification.summary.as_bytes()); i += 2; continue }, "%b" => { formatted.extend_from_slice(notification.body.as_bytes()); i += 2; continue }, "%n" => { formatted.extend_from_slice(notification.app_name.as_bytes()); i += 2; continue }, "%a" => { formatted.extend_from_slice(action_name.as_bytes()); i += 2; continue }, "%i" => { formatted.extend_from_slice(notification.id.to_string().as_bytes()); i += 2; continue }, - _ => (), } formatted.push(b'%');