Rename fmt_replace => format_placeholders; fix tests

main
Tassilo Horn 3 years ago
parent 6e10aa4c24
commit 6c6d237164
  1. 65
      swayr/src/shared/fmt.rs
  2. 27
      swayr/src/tree.rs
  3. 4
      swayrbar/src/bar/module/battery.rs
  4. 4
      swayrbar/src/bar/module/sysinfo.rs
  5. 4
      swayrbar/src/bar/module/window.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(); let arg_string = arg.to_string();
if let Ok(pf) = ParsedFormat::parse(fmt, &[arg], &NoNamedArguments) { if let Ok(pf) = ParsedFormat::parse(fmt, &[arg], &NoNamedArguments) {
@ -136,15 +136,21 @@ fn remove_last_n_chars(s: &mut String, n: usize) {
#[test] #[test]
fn test_format() { fn test_format() {
assert_eq!(format("{:.10}", "sway", ""), "sway"); assert_eq!(do_format("{:.10}", FmtArg::from("sway"), ""), "sway");
assert_eq!(format("{:.10}", "sway", "…"), "sway"); assert_eq!(do_format("{:.10}", FmtArg::from("sway"), "…"), "sway");
assert_eq!(format("{:.4}", "𝔰𝔴𝔞𝔶", "……"), "𝔰𝔴𝔞𝔶"); assert_eq!(do_format("{:.4}", FmtArg::from("𝔰𝔴𝔞𝔶"), "……"), "𝔰𝔴𝔞𝔶");
assert_eq!(format("{:.3}", "sway", ""), "swa"); assert_eq!(do_format("{:.3}", FmtArg::from("sway"), ""), "swa");
assert_eq!(format("{:.3}", "sway", "…"), "sw…"); assert_eq!(do_format("{:.3}", FmtArg::from("sway"), "…"), "sw…");
assert_eq!(format("{:.5}", "𝔰𝔴𝔞𝔶 𝔴𝔦𝔫𝔡𝔬𝔴", "…?"), "𝔰𝔴𝔞…?"); assert_eq!(
assert_eq!(format("{:.5}", "sway window", "..."), "sw..."); do_format("{:.5}", FmtArg::from("𝔰𝔴𝔞𝔶 𝔴𝔦𝔫𝔡𝔬𝔴"), "…?"),
assert_eq!(format("{:.2}", "sway", "..."), "..."); "𝔰𝔴𝔞…?"
);
assert_eq!(
do_format("{:.5}", FmtArg::from("sway window"), "..."),
"sw..."
);
assert_eq!(do_format("{:.2}", FmtArg::from("sway"), "..."), "...");
} }
pub static PLACEHOLDER_RX: Lazy<Regex> = Lazy::new(|| { pub static PLACEHOLDER_RX: Lazy<Regex> = Lazy::new(|| {
@ -154,6 +160,29 @@ pub static PLACEHOLDER_RX: Lazy<Regex> = Lazy::new(|| {
.unwrap() .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 { pub fn maybe_html_escape(do_it: bool, text: String) -> String {
if do_it { if do_it {
text.replace('<', "&lt;") text.replace('<', "&lt;")
@ -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, ( $fmt_str:expr, $html_escape:expr,
{ $( $($pat:pat_param)|+ => $exp:expr, )+ } { $( $($pat:pat_param)|+ => $exp:expr, )+ }
) => { ) => {
@ -180,7 +209,7 @@ macro_rules! fmt_replace {
.map_or("", |m| m.as_str()); .map_or("", |m| m.as_str());
$crate::shared::fmt::maybe_html_escape( $crate::shared::fmt::maybe_html_escape(
$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] #[test]
fn foo() { fn test_format_placeholders() {
let foo = "{a}, {b}"; let foo = "{a}, {b} = {d}";
let html_escape = true; let html_escape = true;
let x: String = fmt_replace!(foo, html_escape, { let x: String = format_placeholders!(foo, html_escape, {
"a" => "1".to_string(), "a" => "1".to_string(),
"b" => "2".to_string(), "b" | "d" => "2".to_string(),
"c" => "3".to_owned(), "c" => "3".to_owned(),
}); });
assert_eq!("1, 2 = 2", x);
} }

@ -16,7 +16,7 @@
//! Convenience data structures built from the IPC structs. //! Convenience data structures built from the IPC structs.
use crate::config; use crate::config;
use crate::shared::fmt::fmt_replace; use crate::shared::fmt::format_placeholders;
use crate::shared::ipc; use crate::shared::ipc;
use crate::shared::ipc::NodeMethods; use crate::shared::ipc::NodeMethods;
use crate::util; use crate::util;
@ -370,7 +370,7 @@ impl DisplayFormat for DisplayNode<'_> {
.as_str(), .as_str(),
); );
fmt_replace!(&fmt, html_escape, { format_placeholders!(&fmt, html_escape, {
"id" => self.node.id, "id" => self.node.id,
"app_name" => self.node.get_app_name(), "app_name" => self.node.get_app_name(),
"layout" => format!("{:?}", self.node.layout), "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(), "...");
}

@ -17,7 +17,7 @@
use crate::bar::config; use crate::bar::config;
use crate::bar::module::BarModuleFn; use crate::bar::module::BarModuleFn;
use crate::shared::fmt::fmt_replace; use crate::shared::fmt::format_placeholders;
use battery as bat; use battery as bat;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use swaybar_types as s; use swaybar_types as s;
@ -52,7 +52,7 @@ fn get_text(cfg: &config::ModuleConfig) -> String {
if bats.is_empty() { if bats.is_empty() {
return String::new(); return String::new();
} }
fmt_replace!(&cfg.format, cfg.html_escape, { format_placeholders!(&cfg.format, cfg.html_escape, {
"state_of_charge" => bats.iter() "state_of_charge" => bats.iter()
.map(|b| b.state_of_charge().value) .map(|b| b.state_of_charge().value)
.sum::<f32>() .sum::<f32>()

@ -17,7 +17,7 @@
use crate::bar::config; use crate::bar::config;
use crate::bar::module::BarModuleFn; use crate::bar::module::BarModuleFn;
use crate::shared::fmt::fmt_replace; use crate::shared::fmt::format_placeholders;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Mutex; use std::sync::Mutex;
use std::sync::Once; use std::sync::Once;
@ -115,7 +115,7 @@ impl BarModuleFn for BarModuleSysInfo {
instance: Some(self.config.instance.clone()), instance: Some(self.config.instance.clone()),
full_text: { full_text: {
let mut sys = self.system.lock().unwrap(); 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), "cpu_usage" => get_cpu_usage(&mut sys, &updater),
"mem_usage" => get_memory_usage(&mut sys, &updater), "mem_usage" => get_memory_usage(&mut sys, &updater),
"load_avg_1" => get_load_average(&mut sys, "load_avg_1" => get_load_average(&mut sys,

@ -19,7 +19,7 @@ use std::collections::HashMap;
use crate::bar::config; use crate::bar::config;
use crate::bar::module::BarModuleFn; 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;
use crate::shared::ipc::NodeMethods; use crate::shared::ipc::NodeMethods;
use swaybar_types as s; use swaybar_types as s;
@ -56,7 +56,7 @@ impl BarModuleFn for BarModuleWindow {
.find(|n| n.focused && n.get_type() == ipc::Type::Window); .find(|n| n.focused && n.get_type() == ipc::Type::Window);
let text = match focused_win { let text = match focused_win {
Some(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(), "title" | "name" => win.get_name(),
"app_name" => win.get_app_name(), "app_name" => win.get_app_name(),
}) })

Loading…
Cancel
Save