From 83c811b72d11d09dac907d9ae6ff4347a29d9f87 Mon Sep 17 00:00:00 2001 From: Lennard Kittner Date: Sun, 19 Oct 2025 15:25:19 +0200 Subject: [PATCH] Only display known information --- src/devices/mod.rs | 136 ++++++++++++++++++++------------------------- src/status_tray.rs | 2 +- 2 files changed, 61 insertions(+), 77 deletions(-) diff --git a/src/devices/mod.rs b/src/devices/mod.rs index 4720f76..4c0e87d 100644 --- a/src/devices/mod.rs +++ b/src/devices/mod.rs @@ -71,45 +71,7 @@ pub struct DeviceState { impl Display for DeviceState { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let unknown = "Unknown".to_string(); - write!( - f, - "Name: {} -Battery level: {} -Charging status: {} -Muted: {} -Mic connected: {} -Automatic shutdown after: {} -Pairing info: {} -Product color: {} -Side tone on: {} -Side tone volume: {} -Surround sound: {} -Voice prompt on: {} -Connected: {} -Silent: {}", - self.device_name.clone().unwrap_or("Unknown".to_string()), - self.battery_level - .map_or(unknown.clone(), |l| format!("{l}%")), - self.charging.map_or(unknown.clone(), |c| c.to_string()), - self.muted.map_or(unknown.clone(), |m| m.to_string()), - self.mic_connected - .map_or(unknown.clone(), |m| m.to_string()), - self.automatic_shutdown_after - .map_or(unknown.clone(), |a| format!("{} min", a.as_secs() / 60)), - self.pairing_info.map_or(unknown.clone(), |p| p.to_string()), - self.product_color - .map_or(unknown.clone(), |c| c.to_string()), - self.side_tone_on.map_or(unknown.clone(), |s| s.to_string()), - self.side_tone_volume - .map_or(unknown.clone(), |s| s.to_string()), - self.surround_sound - .map_or(unknown.clone(), |s| s.to_string()), - self.voice_prompt_on - .map_or(unknown.clone(), |v| v.to_string()), - self.connected.map_or(unknown.clone(), |c| c.to_string()), - self.silent.map_or(unknown.clone(), |s| s.to_string()), - ) + write!(f, "{}", self.to_string_with_padding(25)) } } @@ -155,43 +117,65 @@ impl DeviceState { }) } - pub fn to_string_no_padding(&self) -> String { - let unknown = "Unknown".to_string(); - format!( - "Battery level: {} -Charging status: {} -Muted: {} -Mic connected: {} -Automatic shutdown after: {} -Pairing info: {} -Product color: {} -Side tone on: {} -Side tone volume: {} -Surround sound: {} -Voice prompt on: {} -Connected: {} -Silent: {}", - self.battery_level - .map_or(unknown.clone(), |l| format!("{l}%")), - self.charging.map_or(unknown.clone(), |c| c.to_string()), - self.muted.map_or(unknown.clone(), |m| m.to_string()), - self.mic_connected - .map_or(unknown.clone(), |m| m.to_string()), - self.automatic_shutdown_after - .map_or(unknown.clone(), |a| format!("{} min", a.as_secs() / 60)), - self.pairing_info.map_or(unknown.clone(), |p| p.to_string()), - self.product_color - .map_or(unknown.clone(), |c| c.to_string()), - self.side_tone_on.map_or(unknown.clone(), |s| s.to_string()), - self.side_tone_volume - .map_or(unknown.clone(), |s| s.to_string()), - self.surround_sound - .map_or(unknown.clone(), |s| s.to_string()), - self.voice_prompt_on - .map_or(unknown.clone(), |v| v.to_string()), - self.connected.map_or(unknown.clone(), |c| c.to_string()), - self.silent.map_or(unknown.clone(), |s| s.to_string()), - ) + pub fn to_string_with_padding(&self, padding: usize) -> String { + let data = [ + ( + "Battery level:", + self.battery_level.map(|l| l.to_string()), + "%", + ), + ("Charging status:", self.charging.map(|c| c.to_string()), ""), + ("Muted:", self.muted.map(|c| c.to_string()), ""), + ( + "Mic connected:", + self.mic_connected.map(|c| c.to_string()), + "", + ), + ( + "Automatic shutdown after:", + self.automatic_shutdown_after + .map(|c| (c.as_secs() / 60).to_string()), + "min", + ), + ( + "pairing info:", + self.pairing_info.map(|c| c.to_string()), + "", + ), + ( + "product color:", + self.product_color.map(|c| c.to_string()), + "", + ), + ("Side tone:", self.side_tone_on.map(|c| c.to_string()), ""), + ( + "Side tone volume:", + self.side_tone_volume.map(|c| c.to_string()), + "", + ), + ( + "Surround sound:", + self.surround_sound.map(|c| c.to_string()), + "", + ), + ( + "Voice prompt:", + self.voice_prompt_on.map(|c| c.to_string()), + "", + ), + ("Connected:", self.connected.map(|c| c.to_string()), ""), + ("Playback muted:", self.silent.map(|c| c.to_string()), ""), + ]; + data.iter() + .filter_map(|(prefix, data, suffix)| { + if let Some(data) = data { + Some(format!("{:>() + .join("\n") } fn update_self_with_event(&mut self, event: &DeviceEvent) { diff --git a/src/status_tray.rs b/src/status_tray.rs index 402241c..0d7de69 100644 --- a/src/status_tray.rs +++ b/src/status_tray.rs @@ -23,7 +23,7 @@ impl TrayHandler { device_state.device_name.clone(), ), Some(true) => ( - device_state.to_string_no_padding(), + device_state.to_string_with_padding(0), device_state.device_name.clone(), ), };