diff --git a/swayr/src/shared/fmt.rs b/swayr/src/shared/fmt.rs index f6f5575..4616aa1 100644 --- a/swayr/src/shared/fmt.rs +++ b/swayr/src/shared/fmt.rs @@ -111,7 +111,7 @@ impl FormatArgument for FmtArg { } } -pub fn format(fmt: &str, arg: FmtArg, clipped_str: &str) -> String { +pub fn do_format(fmt: &str, arg: FmtArg, clipped_str: &str) -> String { let arg_string = arg.to_string(); if let Ok(pf) = ParsedFormat::parse(fmt, &[arg], &NoNamedArguments) { @@ -136,15 +136,21 @@ fn remove_last_n_chars(s: &mut String, n: usize) { #[test] fn test_format() { - assert_eq!(format("{:.10}", "sway", ""), "sway"); - assert_eq!(format("{:.10}", "sway", "…"), "sway"); - assert_eq!(format("{:.4}", "𝔰𝔴𝔞𝔶", "……"), "𝔰𝔴𝔞𝔶"); - - assert_eq!(format("{:.3}", "sway", ""), "swa"); - assert_eq!(format("{:.3}", "sway", "…"), "sw…"); - assert_eq!(format("{:.5}", "𝔰𝔴𝔞𝔶 𝔴𝔦𝔫𝔡𝔬𝔴", "…?"), "𝔰𝔴𝔞…?"); - assert_eq!(format("{:.5}", "sway window", "..."), "sw..."); - assert_eq!(format("{:.2}", "sway", "..."), "..."); + assert_eq!(do_format("{:.10}", FmtArg::from("sway"), ""), "sway"); + assert_eq!(do_format("{:.10}", FmtArg::from("sway"), "…"), "sway"); + assert_eq!(do_format("{:.4}", FmtArg::from("𝔰𝔴𝔞𝔶"), "……"), "𝔰𝔴𝔞𝔶"); + + assert_eq!(do_format("{:.3}", FmtArg::from("sway"), ""), "swa"); + assert_eq!(do_format("{:.3}", FmtArg::from("sway"), "…"), "sw…"); + assert_eq!( + do_format("{:.5}", FmtArg::from("𝔰𝔴𝔞𝔶 𝔴𝔦𝔫𝔡𝔬𝔴"), "…?"), + "𝔰𝔴𝔞…?" + ); + assert_eq!( + do_format("{:.5}", FmtArg::from("sway window"), "..."), + "sw..." + ); + assert_eq!(do_format("{:.2}", FmtArg::from("sway"), "..."), "..."); } pub static PLACEHOLDER_RX: Lazy = Lazy::new(|| { @@ -154,6 +160,29 @@ pub static PLACEHOLDER_RX: Lazy = Lazy::new(|| { .unwrap() }); +#[test] +fn test_placeholder_rx() { + let caps = PLACEHOLDER_RX.captures("Hello, {place}!").unwrap(); + assert_eq!(caps.name("name").unwrap().as_str(), "place"); + assert_eq!(caps.name("fmtstr"), None); + assert_eq!(caps.name("clipstr"), None); + + let caps = PLACEHOLDER_RX.captures("Hi, {place:{:>10.10}}!").unwrap(); + assert_eq!(caps.name("name").unwrap().as_str(), "place"); + assert_eq!(caps.name("fmtstr").unwrap().as_str(), "{:>10.10}"); + assert_eq!(caps.name("clipstr").unwrap().as_str(), ""); + + let caps = PLACEHOLDER_RX.captures("Hello, {place:{:.5}…}!").unwrap(); + assert_eq!(caps.name("name").unwrap().as_str(), "place"); + assert_eq!(caps.name("fmtstr").unwrap().as_str(), "{:.5}"); + assert_eq!(caps.name("clipstr").unwrap().as_str(), "…"); + + let caps = PLACEHOLDER_RX.captures("Hello, {place:{:.5}...}!").unwrap(); + assert_eq!(caps.name("name").unwrap().as_str(), "place"); + assert_eq!(caps.name("fmtstr").unwrap().as_str(), "{:.5}"); + assert_eq!(caps.name("clipstr").unwrap().as_str(), "..."); +} + pub fn maybe_html_escape(do_it: bool, text: String) -> String { if do_it { text.replace('<', "<") @@ -164,7 +193,7 @@ pub fn maybe_html_escape(do_it: bool, text: String) -> String { } } -macro_rules! fmt_replace { +macro_rules! format_placeholders { ( $fmt_str:expr, $html_escape:expr, { $( $($pat:pat_param)|+ => $exp:expr, )+ } ) => { @@ -180,7 +209,7 @@ macro_rules! fmt_replace { .map_or("", |m| m.as_str()); $crate::shared::fmt::maybe_html_escape( $html_escape, - $crate::shared::fmt::format(fmt_str, val, clipped_str), + $crate::shared::fmt::do_format(fmt_str, val, clipped_str), ) } )+ @@ -191,15 +220,17 @@ macro_rules! fmt_replace { }; } -pub(crate) use fmt_replace; +pub(crate) use format_placeholders; #[test] -fn foo() { - let foo = "{a}, {b}"; +fn test_format_placeholders() { + let foo = "{a}, {b} = {d}"; let html_escape = true; - let x: String = fmt_replace!(foo, html_escape, { + let x: String = format_placeholders!(foo, html_escape, { "a" => "1".to_string(), - "b" => "2".to_string(), + "b" | "d" => "2".to_string(), "c" => "3".to_owned(), }); + + assert_eq!("1, 2 = 2", x); } diff --git a/swayr/src/tree.rs b/swayr/src/tree.rs index 54ab39c..edbe3e0 100644 --- a/swayr/src/tree.rs +++ b/swayr/src/tree.rs @@ -16,7 +16,7 @@ //! Convenience data structures built from the IPC structs. use crate::config; -use crate::shared::fmt::fmt_replace; +use crate::shared::fmt::format_placeholders; use crate::shared::ipc; use crate::shared::ipc::NodeMethods; use crate::util; @@ -370,7 +370,7 @@ impl DisplayFormat for DisplayNode<'_> { .as_str(), ); - fmt_replace!(&fmt, html_escape, { + format_placeholders!(&fmt, html_escape, { "id" => self.node.id, "app_name" => self.node.get_app_name(), "layout" => format!("{:?}", self.node.layout), @@ -413,26 +413,3 @@ impl DisplayFormat for DisplayNode<'_> { } } } - -#[test] -fn test_placeholder_rx() { - let caps = PLACEHOLDER_RX.captures("Hello, {place}!").unwrap(); - assert_eq!(caps.name("name").unwrap().as_str(), "place"); - assert_eq!(caps.name("fmtstr"), None); - assert_eq!(caps.name("clipstr"), None); - - let caps = PLACEHOLDER_RX.captures("Hi, {place:{:>10.10}}!").unwrap(); - assert_eq!(caps.name("name").unwrap().as_str(), "place"); - assert_eq!(caps.name("fmtstr").unwrap().as_str(), "{:>10.10}"); - assert_eq!(caps.name("clipstr").unwrap().as_str(), ""); - - let caps = PLACEHOLDER_RX.captures("Hello, {place:{:.5}…}!").unwrap(); - assert_eq!(caps.name("name").unwrap().as_str(), "place"); - assert_eq!(caps.name("fmtstr").unwrap().as_str(), "{:.5}"); - assert_eq!(caps.name("clipstr").unwrap().as_str(), "…"); - - let caps = PLACEHOLDER_RX.captures("Hello, {place:{:.5}...}!").unwrap(); - assert_eq!(caps.name("name").unwrap().as_str(), "place"); - assert_eq!(caps.name("fmtstr").unwrap().as_str(), "{:.5}"); - assert_eq!(caps.name("clipstr").unwrap().as_str(), "..."); -} diff --git a/swayrbar/src/bar/module/battery.rs b/swayrbar/src/bar/module/battery.rs index 3e327f5..8d91d5a 100644 --- a/swayrbar/src/bar/module/battery.rs +++ b/swayrbar/src/bar/module/battery.rs @@ -17,7 +17,7 @@ use crate::bar::config; use crate::bar::module::BarModuleFn; -use crate::shared::fmt::fmt_replace; +use crate::shared::fmt::format_placeholders; use battery as bat; use std::collections::{HashMap, HashSet}; use swaybar_types as s; @@ -52,7 +52,7 @@ fn get_text(cfg: &config::ModuleConfig) -> String { if bats.is_empty() { return String::new(); } - fmt_replace!(&cfg.format, cfg.html_escape, { + format_placeholders!(&cfg.format, cfg.html_escape, { "state_of_charge" => bats.iter() .map(|b| b.state_of_charge().value) .sum::() diff --git a/swayrbar/src/bar/module/sysinfo.rs b/swayrbar/src/bar/module/sysinfo.rs index 07c61a7..b887077 100644 --- a/swayrbar/src/bar/module/sysinfo.rs +++ b/swayrbar/src/bar/module/sysinfo.rs @@ -17,7 +17,7 @@ use crate::bar::config; use crate::bar::module::BarModuleFn; -use crate::shared::fmt::fmt_replace; +use crate::shared::fmt::format_placeholders; use std::collections::HashMap; use std::sync::Mutex; use std::sync::Once; @@ -115,7 +115,7 @@ impl BarModuleFn for BarModuleSysInfo { instance: Some(self.config.instance.clone()), full_text: { let mut sys = self.system.lock().unwrap(); - fmt_replace!(&self.config.format, self.config.html_escape, { + format_placeholders!(&self.config.format, self.config.html_escape, { "cpu_usage" => get_cpu_usage(&mut sys, &updater), "mem_usage" => get_memory_usage(&mut sys, &updater), "load_avg_1" => get_load_average(&mut sys, diff --git a/swayrbar/src/bar/module/window.rs b/swayrbar/src/bar/module/window.rs index b56057f..e32c2e4 100644 --- a/swayrbar/src/bar/module/window.rs +++ b/swayrbar/src/bar/module/window.rs @@ -19,7 +19,7 @@ use std::collections::HashMap; use crate::bar::config; use crate::bar::module::BarModuleFn; -use crate::shared::fmt::fmt_replace; +use crate::shared::fmt::format_placeholders; use crate::shared::ipc; use crate::shared::ipc::NodeMethods; use swaybar_types as s; @@ -56,7 +56,7 @@ impl BarModuleFn for BarModuleWindow { .find(|n| n.focused && n.get_type() == ipc::Type::Window); let text = match focused_win { Some(win) => { - fmt_replace!(&self.config.format, self.config.html_escape, { + format_placeholders!(&self.config.format, self.config.html_escape, { "title" | "name" => win.get_name(), "app_name" => win.get_app_name(), })