Skip to content

Commit

Permalink
Add percentage value markup tag: #40
Browse files Browse the repository at this point in the history
  • Loading branch information
Toqozz committed Dec 20, 2021
1 parent 306fbf3 commit 89bcfa6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/bus/dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,10 @@ impl Notification {

let percentage: Option<f32>;
if let Some(value) = arg::prop_cast::<i32>(&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;
}
Expand Down
15 changes: 14 additions & 1 deletion src/maths_utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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'%');
Expand Down

0 comments on commit 89bcfa6

Please sign in to comment.