ha-now-playing/src/output/waybar.rs

80 lines
1.7 KiB
Rust

// {"text": "$text", "alt": "$alt", "tooltip": "$tooltip", "class": "$class", "percentage": $percentage }
use std::fmt::Display;
use serde::Serialize;
#[derive(Debug, Default)]
pub(crate) struct WaybarBuilder {
text: String,
alt: String,
tooltip: String,
class: String,
percentage: u8,
}
impl WaybarBuilder {
pub(crate) fn new() -> Self {
Self::default()
}
pub(crate) fn text(mut self, text: String) -> Self {
self.text = text;
self
}
pub(crate) fn alt(mut self, alt: String) -> Self {
self.alt = alt;
self
}
pub(crate) fn tooltip(mut self, tooltip: String) -> Self {
self.tooltip = tooltip;
self
}
pub(crate) fn class(mut self, class: String) -> Self {
self.class = class;
self
}
pub(crate) fn percentage(mut self, percentage: u8) -> Self {
self.percentage = percentage.clamp(0, 100);
self
}
pub(crate) fn build(self) -> Waybar {
Waybar {
text: self.text,
alt: self.alt,
tooltip: self.tooltip,
class: self.class,
percentage: self.percentage,
}
}
}
#[derive(Debug, Default, Serialize)]
pub(crate) struct Waybar {
text: String,
alt: String,
tooltip: String,
class: String,
percentage: u8,
}
impl Waybar {
pub(crate) fn builder() -> WaybarBuilder {
WaybarBuilder::new()
}
}
impl Display for Waybar {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Ok(out) = serde_json::to_string(self) {
write!(f, "{out}")
} else {
write!(f, "Unable to serialize to JSON: {self:?}")
}
}
}