From bc00f54c904cd7008048411c538466fb6964f43e Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Wed, 9 Oct 2024 15:06:48 -0700 Subject: [PATCH] feat(bar): add more logging around error paths --- komorebi-bar/src/komorebi.rs | 161 +++++++++++++++++++---------------- komorebi-bar/src/main.rs | 25 +++--- 2 files changed, 101 insertions(+), 85 deletions(-) diff --git a/komorebi-bar/src/komorebi.rs b/komorebi-bar/src/komorebi.rs index 4c60e05a..1d6040f0 100644 --- a/komorebi-bar/src/komorebi.rs +++ b/komorebi-bar/src/komorebi.rs @@ -5,6 +5,7 @@ use crate::widget::BarWidget; use crate::MAX_LABEL_WIDTH; use crate::WIDGET_SPACING; use crossbeam_channel::Receiver; +use crossbeam_channel::TryRecvError; use eframe::egui::text::LayoutJob; use eframe::egui::Color32; use eframe::egui::ColorImage; @@ -437,93 +438,105 @@ impl KomorebiNotificationState { rx_gui: Receiver, bg_color: Rc>, ) { - if let Ok(notification) = rx_gui.try_recv() { - if let NotificationEvent::Socket(SocketMessage::ReloadStaticConfiguration(path)) = - notification.event - { - if let Ok(config) = komorebi_client::StaticConfig::read(&path) { - if let Some(theme) = config.theme { - apply_theme(ctx, KomobarTheme::from(theme), bg_color); - tracing::info!("applied theme from updated komorebi.json"); + match rx_gui.try_recv() { + Err(error) => match error { + TryRecvError::Empty => {} + TryRecvError::Disconnected => { + tracing::error!( + "failed to receive komorebi notification on gui thread: {error}" + ); + } + }, + Ok(notification) => { + if let NotificationEvent::Socket(SocketMessage::ReloadStaticConfiguration(path)) = + notification.event + { + if let Ok(config) = komorebi_client::StaticConfig::read(&path) { + if let Some(theme) = config.theme { + apply_theme(ctx, KomobarTheme::from(theme), bg_color); + tracing::info!("applied theme from updated komorebi.json"); + } } } - } - self.mouse_follows_focus = notification.state.mouse_follows_focus; + self.mouse_follows_focus = notification.state.mouse_follows_focus; - let monitor = ¬ification.state.monitors.elements()[monitor_index]; - self.work_area_offset = - notification.state.monitors.elements()[monitor_index].work_area_offset(); + let monitor = ¬ification.state.monitors.elements()[monitor_index]; + self.work_area_offset = + notification.state.monitors.elements()[monitor_index].work_area_offset(); - let focused_workspace_idx = monitor.focused_workspace_idx(); + let focused_workspace_idx = monitor.focused_workspace_idx(); - let mut workspaces = vec![]; - self.selected_workspace = monitor.workspaces()[focused_workspace_idx] - .name() - .to_owned() - .unwrap_or_else(|| format!("{}", focused_workspace_idx + 1)); + let mut workspaces = vec![]; + self.selected_workspace = monitor.workspaces()[focused_workspace_idx] + .name() + .to_owned() + .unwrap_or_else(|| format!("{}", focused_workspace_idx + 1)); - for (i, ws) in monitor.workspaces().iter().enumerate() { - let should_add = if self.hide_empty_workspaces { - focused_workspace_idx == i || !ws.containers().is_empty() - } else { - true - }; + for (i, ws) in monitor.workspaces().iter().enumerate() { + let should_add = if self.hide_empty_workspaces { + focused_workspace_idx == i || !ws.containers().is_empty() + } else { + true + }; - if should_add { - workspaces.push(ws.name().to_owned().unwrap_or_else(|| format!("{}", i + 1))); + if should_add { + workspaces + .push(ws.name().to_owned().unwrap_or_else(|| format!("{}", i + 1))); + } } - } - self.workspaces = workspaces; - self.layout = match monitor.workspaces()[focused_workspace_idx].layout() { - komorebi_client::Layout::Default(layout) => KomorebiLayout::Default(*layout), - komorebi_client::Layout::Custom(_) => KomorebiLayout::Custom, - }; + self.workspaces = workspaces; + self.layout = match monitor.workspaces()[focused_workspace_idx].layout() { + komorebi_client::Layout::Default(layout) => KomorebiLayout::Default(*layout), + komorebi_client::Layout::Custom(_) => KomorebiLayout::Custom, + }; - if !*monitor.workspaces()[focused_workspace_idx].tile() { - self.layout = KomorebiLayout::Floating; - } + if !*monitor.workspaces()[focused_workspace_idx].tile() { + self.layout = KomorebiLayout::Floating; + } - if notification.state.is_paused { - self.layout = KomorebiLayout::Paused; - } + if notification.state.is_paused { + self.layout = KomorebiLayout::Paused; + } - if let Some(container) = monitor.workspaces()[focused_workspace_idx].monocle_container() - { - self.focused_container_information = ( - container - .windows() - .iter() - .map(|w| w.title().unwrap_or_default()) - .collect::>(), - container - .windows() - .iter() - .map(|w| windows_icons::get_icon_by_process_id(w.process_id())) - .collect::>(), - container.focused_window_idx(), - ); - } else if let Some(container) = - monitor.workspaces()[focused_workspace_idx].focused_container() - { - self.focused_container_information = ( - container - .windows() - .iter() - .map(|w| w.title().unwrap_or_default()) - .collect::>(), - container - .windows() - .iter() - .map(|w| windows_icons::get_icon_by_process_id(w.process_id())) - .collect::>(), - container.focused_window_idx(), - ); - } else { - self.focused_container_information.0.clear(); - self.focused_container_information.1.clear(); - self.focused_container_information.2 = 0; + if let Some(container) = + monitor.workspaces()[focused_workspace_idx].monocle_container() + { + self.focused_container_information = ( + container + .windows() + .iter() + .map(|w| w.title().unwrap_or_default()) + .collect::>(), + container + .windows() + .iter() + .map(|w| windows_icons::get_icon_by_process_id(w.process_id())) + .collect::>(), + container.focused_window_idx(), + ); + } else if let Some(container) = + monitor.workspaces()[focused_workspace_idx].focused_container() + { + self.focused_container_information = ( + container + .windows() + .iter() + .map(|w| w.title().unwrap_or_default()) + .collect::>(), + container + .windows() + .iter() + .map(|w| windows_icons::get_icon_by_process_id(w.process_id())) + .collect::>(), + container.focused_window_idx(), + ); + } else { + self.focused_container_information.0.clear(); + self.focused_container_information.1.clear(); + self.focused_container_information.2 = 0; + } } } } diff --git a/komorebi-bar/src/main.rs b/komorebi-bar/src/main.rs index d3d7033c..da2b87ea 100644 --- a/komorebi-bar/src/main.rs +++ b/komorebi-bar/src/main.rs @@ -1,7 +1,7 @@ mod bar; -mod cpu; mod battery; mod config; +mod cpu; mod date; mod komorebi; mod media; @@ -370,18 +370,21 @@ fn main() -> color_eyre::Result<()> { match String::from_utf8(buffer) { Ok(notification_string) => { - if let Ok(notification) = - serde_json::from_str::( - ¬ification_string, - ) - { - tracing::debug!("received notification from komorebi"); + match serde_json::from_str::( + ¬ification_string, + ) { + Ok(notification) => { + tracing::debug!("received notification from komorebi"); - if let Err(error) = tx_gui.send(notification) { - tracing::error!("could not send komorebi notification update to gui: {error}") - } + if let Err(error) = tx_gui.send(notification) { + tracing::error!("could not send komorebi notification update to gui thread: {error}") + } - ctx_komorebi.request_repaint(); + ctx_komorebi.request_repaint(); + } + Err(error) => { + tracing::error!("could not deserialize komorebi notification: {error}"); + } } } Err(error) => {