From 89959253cffe397bfdbae73ce196615eba0dc1e4 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Fri, 1 Apr 2022 23:21:36 +0200 Subject: [PATCH] Enhance rtfmt --- src/fmt_replace.rs | 4 ++-- src/rtfmt.rs | 45 +++++++++++++++++++++++++++++++++++++-------- src/tree.rs | 17 +++++++++-------- 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/src/fmt_replace.rs b/src/fmt_replace.rs index 58d1870..38f4e67 100644 --- a/src/fmt_replace.rs +++ b/src/fmt_replace.rs @@ -41,14 +41,14 @@ macro_rules! fmt_replace { let value: String = match &caps["name"] { $( $( | $pat )+ => { - let val = $exp; + let val = $crate::rtfmt::FmtArg::from($exp); let fmt_str = caps.name("fmtstr") .map_or("{}", |m| m.as_str()); let clipped_str = caps.name("clipstr") .map_or("", |m| m.as_str()); $crate::fmt_replace::maybe_html_escape( $html_escape, - crate::rtfmt::format(fmt_str, &val, clipped_str), + $crate::rtfmt::format(fmt_str, val, clipped_str), ) } )+ diff --git a/src/rtfmt.rs b/src/rtfmt.rs index cc5c5e6..989bf1b 100644 --- a/src/rtfmt.rs +++ b/src/rtfmt.rs @@ -22,18 +22,47 @@ use rt_format::{ }; use std::fmt; -enum FmtArg<'a> { - Str(&'a str), +pub enum FmtArg { + I64(i64), + String(String), } -impl<'a> FormatArgument for FmtArg<'a> { +impl From for FmtArg { + fn from(x: i64) -> FmtArg { + FmtArg::I64(x) + } +} + +impl From<&str> for FmtArg { + fn from(x: &str) -> FmtArg { + FmtArg::String(x.to_string()) + } +} + +impl From for FmtArg { + fn from(x: String) -> FmtArg { + FmtArg::String(x) + } +} + +impl ToString for FmtArg { + fn to_string(&self) -> String { + match self { + FmtArg::String(x) => x.clone(), + FmtArg::I64(x) => x.to_string(), + } + } +} + +impl FormatArgument for FmtArg { fn supports_format(&self, spec: &Specifier) -> bool { spec.format == Format::Display } fn fmt_display(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - Self::Str(val) => fmt::Display::fmt(&val, f), + Self::String(val) => fmt::Display::fmt(&val, f), + Self::I64(val) => fmt::Display::fmt(&val, f), } } @@ -66,13 +95,13 @@ impl<'a> FormatArgument for FmtArg<'a> { } } -pub fn format(fmt: &str, arg: &str, clipped_str: &str) -> String { - let args = &[FmtArg::Str(arg)]; +pub fn format(fmt: &str, arg: FmtArg, clipped_str: &str) -> String { + let arg_string = arg.to_string(); - if let Ok(pf) = ParsedFormat::parse(fmt, args, &NoNamedArguments) { + if let Ok(pf) = ParsedFormat::parse(fmt, &[arg], &NoNamedArguments) { let mut s = format!("{}", pf); - if !clipped_str.is_empty() && !s.contains(arg) { + if !clipped_str.is_empty() && !s.contains(arg_string.as_str()) { remove_last_n_chars(&mut s, clipped_str.chars().count()); s.push_str(clipped_str); } diff --git a/src/tree.rs b/src/tree.rs index 7b9b181..bdb352e 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -465,7 +465,10 @@ impl DisplayFormat for DisplayNode<'_> { Type::Window => cfg.get_format_window_format(), }; let fmt = fmt - .replace("{indent}", &indent.repeat(self.get_indent_level())) + .replace( + "{indent}", + indent.repeat(self.get_indent_level()).as_str(), + ) .replace( "{urgency_start}", if self.node.urgent { @@ -501,20 +504,18 @@ impl DisplayFormat for DisplayNode<'_> { ); fmt_replace!(&fmt, html_escape, { - "id" => self.node.id.to_string(), - "app_name" => self.node.get_app_name().to_string(), + "id" => self.node.id, + "app_name" => self.node.get_app_name(), "layout" => format!("{:?}", self.node.layout), - "name" | "title" => self.node.get_name().to_string(), + "name" | "title" => self.node.get_name(), "output_name" => self .tree .get_parent_node_of_type(self.node.id, Type::Output) - .map_or("", |w| w.get_name()) - .to_string(), + .map_or("", |w| w.get_name()), "workspace_name" => self .tree .get_parent_node_of_type(self.node.id, Type::Workspace) - .map_or("", |w| w.get_name()) - .to_string(), + .map_or("", |w| w.get_name()), "marks" => format_marks(&self.node.marks), }) }