From 4ca2ff84dc8c04097700ad06a925d6475b0c9849 Mon Sep 17 00:00:00 2001 From: Ransomwave <63156992+Ransomwave@users.noreply.github.com> Date: Wed, 20 May 2026 04:46:49 +0200 Subject: [PATCH] feat(bar): add default interface options to display friendly name or local IP Adds a new configuration to the Network widget from komorebi-bar, which allows you to toggle between viewing your Default Interface's friendly name, IPv4 or IPv6. Motivation: I find it very useful to check your local IP at a glance, and missed the feature heavily from other wms in Linux. --- komorebi-bar/src/widgets/network.rs | 51 ++++++++++++++++++++++++++--- schema.bar.json | 26 +++++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/komorebi-bar/src/widgets/network.rs b/komorebi-bar/src/widgets/network.rs index a1870891..740702ca 100644 --- a/komorebi-bar/src/widgets/network.rs +++ b/komorebi-bar/src/widgets/network.rs @@ -38,6 +38,8 @@ pub struct NetworkConfig { pub show_activity: bool, /// Show default interface pub show_default_interface: Option, + /// Default interface options: whether to show the friendly name, IPv4 or IPv6 address + pub default_interface_options: Option, /// Characters to reserve for received and transmitted activity #[serde(alias = "network_activity_fill_characters")] pub activity_left_padding: Option, @@ -64,6 +66,24 @@ pub struct NetworkSelectConfig { pub transmitted_over: Option, } +#[derive(Copy, Clone, Debug, Serialize, Deserialize, Default)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +/// Which IP address to show for the default interface +pub enum InterfaceDisplayType { + #[default] + FriendlyName, + Ipv4, + Ipv6, +} + +#[derive(Copy, Clone, Debug, Serialize, Deserialize)] +#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] +/// Default interface configuration +pub struct DefaultInterfaceConfig { + #[serde(default)] + pub display_type: InterfaceDisplayType, +} + impl From for Network { fn from(value: NetworkConfig) -> Self { let default_refresh_interval = 10; @@ -76,6 +96,7 @@ impl From for Network { show_total_activity: value.show_total_activity, show_activity: value.show_activity, show_default_interface: value.show_default_interface.unwrap_or(true), + default_interface_options: value.default_interface_options, networks_network_activity: Arc::new(Mutex::new(Networks::new_with_refreshed_list())), default_interface: Arc::new(Mutex::new(String::new())), interface_generation: Arc::new(AtomicU64::new(0)), @@ -104,6 +125,7 @@ pub struct Network { pub show_total_activity: bool, pub show_activity: bool, pub show_default_interface: bool, + pub default_interface_options: Option, networks_network_activity: Arc>, default_refresh_interval: u64, data_refresh_interval: u64, @@ -124,16 +146,37 @@ impl Network { let gen_ = self.interface_generation.fetch_add(1, Ordering::SeqCst) + 1; let gen_arc = Arc::clone(&self.interface_generation); let iface_arc = Arc::clone(&self.default_interface); + let default_interface_options = self.default_interface_options; thread::spawn(move || { - if let Ok(interface) = netdev::get_default_interface() - && let Some(friendly_name) = &interface.friendly_name - { + if let Ok(interface) = netdev::get_default_interface() { + // Determine what to show based on the configuration + let display_type = default_interface_options + .map(|config| config.display_type) + .unwrap_or(InterfaceDisplayType::FriendlyName); + + let value_to_show = match display_type { + InterfaceDisplayType::FriendlyName => interface + .friendly_name + .clone() + .unwrap_or_else(|| "Unknown Interface".to_string()), + InterfaceDisplayType::Ipv4 => interface + .ipv4 + .first() + .map(|ip| ip.to_string()) + .unwrap_or_else(|| "No IPv4".to_string()), + InterfaceDisplayType::Ipv6 => interface + .ipv6 + .first() + .map(|ip| ip.to_string()) + .unwrap_or_else(|| "No IPv6".to_string()), + }; + // Only update if this is the latest request if gen_ == gen_arc.load(Ordering::SeqCst) && let Ok(mut iface) = iface_arc.lock() { - *iface = friendly_name.clone(); + *iface = value_to_show; } } }); diff --git a/schema.bar.json b/schema.bar.json index 480b4f57..0b463297 100644 --- a/schema.bar.json +++ b/schema.bar.json @@ -4800,6 +4800,17 @@ "null" ] }, + "default_interface_options": { + "description": "Options related to showing the default interface", + "anyOf": [ + { + "$ref": "#/$defs/DefaultInterfaceOptions" + }, + { + "type": "null" + } + ] + }, "show_total_activity": { "description": "Show total received and transmitted activity", "type": "boolean" @@ -4853,6 +4864,21 @@ } } }, + "DefaultInterfaceOptions": { + "description": "Options related to showing the default network interface", + "type": "object", + "properties": { + "display_type": { + "description": "What to display from the default interface", + "type": "string", + "enum": [ + "FriendlyName", + "Ipv4", + "Ipv6" + ] + } + } + }, "OperationBehaviour": { "description": "Operation behaviour for temporarily unmanaged and floating windows", "oneOf": [