Better printing

This commit is contained in:
Lennard Kittner
2025-03-23 00:18:14 +01:00
parent 257f109695
commit 62b9bbb6f0
3 changed files with 46 additions and 46 deletions

View File

@@ -115,8 +115,8 @@ fn main() {
std::process::exit(1); std::process::exit(1);
}; };
println!( println!(
"Device State: \n{}", "{}",
device.get_device_state().to_better_string() device.get_device_state()
); );
} }

View File

@@ -43,36 +43,36 @@ pub struct DeviceState {
impl Display for DeviceState { impl Display for DeviceState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let unknown = "Unknown".to_string(); let unknown = "Unknown".to_string();
write!( write!(f, "Name: {}
f, Battery level: {}
"Battery level: {} Charging status: {}
Carging status: {} Muted: {}
Muted: {} Mic connected: {}
Mic connected: {}
Automatic shutdown after: {} Automatic shutdown after: {}
Pairing info: {} Pairing info: {}
Product color: {} Product color: {}
Side tone on: {} Side tone on: {}
Side tone volume: {} Side tone volume: {}
Voice prompt on: {} Voice prompt on: {}
Connected: {}", Connected: {}",
self.battery_level self.device_name.clone().unwrap_or("Unknown".to_string()),
.map_or(unknown.clone(), |l| format!("{l}%")), self.battery_level
self.charging.map_or(unknown.clone(), |c| c.to_string()), .map_or(unknown.clone(), |l| format!("{l}%")),
self.muted.map_or(unknown.clone(), |m| m.to_string()), self.charging.map_or(unknown.clone(), |c| c.to_string()),
self.mic_connected self.muted.map_or(unknown.clone(), |m| m.to_string()),
.map_or(unknown.clone(), |m| m.to_string()), self.mic_connected
self.automatic_shutdown_after .map_or(unknown.clone(), |m| m.to_string()),
.map_or(unknown.clone(), |a| format!("{} min", (a.as_secs() / 60))), self.automatic_shutdown_after
self.pairing_info.map_or(unknown.clone(), |p| p.to_string()), .map_or(unknown.clone(), |a| format!("{} min", a.as_secs() / 60)),
self.product_color self.pairing_info.map_or(unknown.clone(), |p| p.to_string()),
.map_or(unknown.clone(), |c| c.to_string()), self.product_color
self.side_tone_on.map_or(unknown.clone(), |s| s.to_string()), .map_or(unknown.clone(), |c| c.to_string()),
self.side_tone_volume self.side_tone_on.map_or(unknown.clone(), |s| s.to_string()),
.map_or(unknown.clone(), |s| s.to_string()), self.side_tone_volume
self.voice_prompt_on .map_or(unknown.clone(), |s| s.to_string()),
.map_or(unknown.clone(), |v| v.to_string()), self.voice_prompt_on
self.connected.map_or(unknown.clone(), |c| c.to_string()), .map_or(unknown.clone(), |v| v.to_string()),
self.connected.map_or(unknown.clone(), |c| c.to_string()),
) )
} }
} }
@@ -110,20 +110,19 @@ impl DeviceState {
}) })
} }
pub fn to_better_string(&self) -> String { pub fn to_string_no_padding(&self) -> String {
let unknown = "Unknown".to_string(); let unknown = "Unknown".to_string();
format!( format!("Battery level: {}
"Battery level: {} Charging status: {}
Carging status: {} Muted: {}
Muted: {} Mic connected: {}
Mic connected: {}
Automatic shutdown after: {} Automatic shutdown after: {}
Pairing info: {} Pairing info: {}
Product color: {} Product color: {}
Side tone on: {} Side tone on: {}
Side tone volume: {} Side tone volume: {}
Voice prompt on: {} Voice prompt on: {}
Connected: {}", Connected: {}",
self.battery_level self.battery_level
.map_or(unknown.clone(), |l| format!("{l}%")), .map_or(unknown.clone(), |l| format!("{l}%")),
self.charging.map_or(unknown.clone(), |c| c.to_string()), self.charging.map_or(unknown.clone(), |c| c.to_string()),
@@ -131,7 +130,7 @@ Connected: {}",
self.mic_connected self.mic_connected
.map_or(unknown.clone(), |m| m.to_string()), .map_or(unknown.clone(), |m| m.to_string()),
self.automatic_shutdown_after self.automatic_shutdown_after
.map_or(unknown.clone(), |a| format!("{} min", (a.as_secs() / 60))), .map_or(unknown.clone(), |a| format!("{} min", a.as_secs() / 60)),
self.pairing_info.map_or(unknown.clone(), |p| p.to_string()), self.pairing_info.map_or(unknown.clone(), |p| p.to_string()),
self.product_color self.product_color
.map_or(unknown.clone(), |c| c.to_string()), .map_or(unknown.clone(), |c| c.to_string()),

View File

@@ -22,7 +22,7 @@ impl TrayHandler {
"Headset is not connected".to_string(), "Headset is not connected".to_string(),
device_state.device_name.clone(), device_state.device_name.clone(),
), ),
Some(true) => (device_state.to_string(), device_state.device_name.clone()), Some(true) => (device_state.to_string_no_padding(), device_state.device_name.clone()),
}; };
self.handle.update(|tray| { self.handle.update(|tray| {
tray.message = message; tray.message = message;
@@ -53,9 +53,10 @@ impl Tray for StatusTray {
"audio-headset".into() "audio-headset".into()
} }
fn tool_tip(&self) -> ToolTip { fn tool_tip(&self) -> ToolTip {
let description = self.message.clone().lines().filter(|l| !l.contains("Unknown")).collect::<Vec<&str>>().join("\n");
ToolTip { ToolTip {
title: self.device_name.clone().unwrap_or("Unknown".to_string()), title: self.device_name.clone().unwrap_or("Unknown".to_string()),
description: self.message.clone(), description,
icon_name: "audio-headset".into(), icon_name: "audio-headset".into(),
icon_pixmap: Vec::new(), icon_pixmap: Vec::new(),
} }