Enhance rtfmt

main
Tassilo Horn 3 years ago
parent c330a35624
commit 89959253cf
  1. 4
      src/fmt_replace.rs
  2. 45
      src/rtfmt.rs
  3. 17
      src/tree.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),
)
}
)+

@ -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<i64> 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<String> 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);
}

@ -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("<no_output>", |w| w.get_name())
.to_string(),
.map_or("<no_output>", |w| w.get_name()),
"workspace_name" => self
.tree
.get_parent_node_of_type(self.node.id, Type::Workspace)
.map_or("<no_workspace>", |w| w.get_name())
.to_string(),
.map_or("<no_workspace>", |w| w.get_name()),
"marks" => format_marks(&self.node.marks),
})
}

Loading…
Cancel
Save