From d5fcc8286cfa955b7999b40434f8ad2d6b2f2b34 Mon Sep 17 00:00:00 2001 From: Lennard Kittner Date: Sat, 22 Mar 2025 12:22:32 +0100 Subject: [PATCH] Improve to_string and add name --- src/devices/mod.rs | 61 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/src/devices/mod.rs b/src/devices/mod.rs index ea0f2f1..8e329f7 100644 --- a/src/devices/mod.rs +++ b/src/devices/mod.rs @@ -26,6 +26,7 @@ pub fn connect_compatible_device() -> Result, DeviceError> { #[derive(Debug)] pub struct DeviceState { pub hid_device: HidDevice, + pub device_name: Option, pub battery_level: Option, pub charging: Option, pub muted: Option, @@ -44,19 +45,17 @@ impl Display for DeviceState { let unknown = "Unknown".to_string(); write!( f, - " - Battery level: {} - Carging status: {} - Muted: {} - Mic connected: {} - Automatic shutdown after: {} - Pairing info: {} - Product color: {} - Side tone on: {} - Side tone volume: {} - Voice prompt on: {} - Connected: {} - ", + "Battery level: {} +Carging status: {} +Muted: {} +Mic connected: {} +Automatic shutdown after: {} +Pairing info: {} +Product color: {} +Side tone on: {} +Side tone volume: {} +Voice prompt on: {} +Connected: {}", self.battery_level .map_or(unknown.clone(), |l| format!("{l}%")), self.charging.map_or(unknown.clone(), |c| c.to_string()), @@ -93,8 +92,10 @@ impl DeviceState { } }) .ok_or(DeviceError::NoDeviceFound())??; + let device_name = hid_device.get_product_string()?; Ok(DeviceState { hid_device, + device_name, charging: None, battery_level: None, muted: None, @@ -109,6 +110,40 @@ impl DeviceState { }) } + pub fn to_better_string(&self) -> String { + let unknown = "Unknown".to_string(); + format!( + "Battery level: {} +Carging status: {} +Muted: {} +Mic connected: {} +Automatic shutdown after: {} +Pairing info: {} +Product color: {} +Side tone on: {} +Side tone volume: {} +Voice prompt on: {} +Connected: {}", + 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.voice_prompt_on + .map_or(unknown.clone(), |v| v.to_string()), + self.connected.map_or(unknown.clone(), |c| c.to_string()), + ) + } + fn update_self_with_event(&mut self, event: &DeviceEvent) { match event { DeviceEvent::BatterLevel(level) => self.battery_level = Some(*level),