From 160b7d645c2e7a3380a774b881e76cfa5717cddc Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Sun, 3 Apr 2022 16:41:43 +0200 Subject: [PATCH] Improve window bar module --- src/bar/module/battery.rs | 6 +++--- src/bar/module/window.rs | 17 +++++++++++++---- src/fmt_replace.rs | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/bar/module/battery.rs b/src/bar/module/battery.rs index d3920ac..dce7185 100644 --- a/src/bar/module/battery.rs +++ b/src/bar/module/battery.rs @@ -85,12 +85,12 @@ impl BarModuleFn for BarModuleBattery { fn build(&self) -> s::Block { let fmt = - "🔋 Bat: {state_of_charge:{:5.1}}% {state} Health: {state_of_health:{:5.1}}%"; - let s = get_text(&self.manager, fmt); + "🔋 Bat: {state_of_charge:{:5.1}}%, {state}, Health: {state_of_health:{:5.1}}%"; + let text = get_text(&self.manager, fmt); s::Block { name: Some(Self::name()), instance: Some(self.instance.clone()), - full_text: s, + full_text: text, align: Some(s::Align::Right), markup: Some(s::Markup::Pango), short_text: None, diff --git a/src/bar/module/window.rs b/src/bar/module/window.rs index 83ac212..8b377c3 100644 --- a/src/bar/module/window.rs +++ b/src/bar/module/window.rs @@ -16,6 +16,7 @@ //! The window `swayrbar` module. use crate::bar::module::BarModuleFn; +use crate::fmt_replace::fmt_replace; use crate::ipc; use crate::ipc::NodeMethods; use swaybar_types as s; @@ -40,14 +41,22 @@ impl BarModuleFn for BarModuleWindow { } fn build(&self) -> s::Block { + let fmt = "🪟 {title} — {app_name}"; let root = ipc::get_root_node(false); - let focused_win = root.iter().find(|n| n.focused); - let app_name = focused_win.map_or("", |w| w.get_app_name()); - let title = focused_win.map_or("", |w| w.get_name()); + let focused_win = root + .iter() + .find(|n| n.focused && n.get_type() == ipc::Type::Window); + let text = match focused_win { + Some(win) => fmt_replace!(&fmt, false, { + "title" |"name" => win.get_name(), + "app_name" => win.get_app_name(), + }), + None => String::new(), + }; s::Block { name: Some(Self::name()), instance: Some(self.instance.clone()), - full_text: "🪟 ".to_string() + title + " — " + app_name, + full_text: text, align: Some(s::Align::Left), markup: Some(s::Markup::Pango), short_text: None, diff --git a/src/fmt_replace.rs b/src/fmt_replace.rs index 2c5109b..f24f41e 100644 --- a/src/fmt_replace.rs +++ b/src/fmt_replace.rs @@ -41,7 +41,7 @@ macro_rules! fmt_replace { .replace_all($fmt_str, |caps: ®ex::Captures| { let value: String = match &caps["name"] { $( - $( | $pat )+ => { + $( $pat )|+ => { let val = $crate::rtfmt::FmtArg::from($exp); let fmt_str = caps.name("fmtstr") .map_or("{}", |m| m.as_str());