parent
7035268413
commit
6e10aa4c24
20 changed files with 134 additions and 123 deletions
@ -1,74 +0,0 @@ |
||||
// Copyright (C) 2022 Tassilo Horn <tsdh@gnu.org>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify it
|
||||
// under the terms of the GNU General Public License as published by the Free
|
||||
// Software Foundation, either version 3 of the License, or (at your option)
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
// more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along with
|
||||
// this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use once_cell::sync::Lazy; |
||||
use regex::Regex; |
||||
|
||||
pub static PLACEHOLDER_RX: Lazy<Regex> = Lazy::new(|| { |
||||
Regex::new( |
||||
r"\{(?P<name>[^}:]+)(?::(?P<fmtstr>\{[^}]*\})(?P<clipstr>[^}]*))?\}", |
||||
) |
||||
.unwrap() |
||||
}); |
||||
|
||||
pub fn maybe_html_escape(do_it: bool, text: String) -> String { |
||||
if do_it { |
||||
text.replace('<', "<") |
||||
.replace('>', ">") |
||||
.replace('&', "&") |
||||
} else { |
||||
text |
||||
} |
||||
} |
||||
|
||||
macro_rules! fmt_replace { |
||||
( $fmt_str:expr, $html_escape:expr, |
||||
{ $( $($pat:pat_param)|+ => $exp:expr, )+ } |
||||
) => { |
||||
$crate::fmt_replace::PLACEHOLDER_RX |
||||
.replace_all($fmt_str, |caps: ®ex::Captures| { |
||||
let value: String = match &caps["name"] { |
||||
$( |
||||
$( $pat )|+ => { |
||||
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), |
||||
) |
||||
} |
||||
)+ |
||||
_ => caps[0].to_string(), |
||||
}; |
||||
value |
||||
}).into() |
||||
}; |
||||
} |
||||
|
||||
pub(crate) use fmt_replace; |
||||
|
||||
#[test] |
||||
fn foo() { |
||||
let foo = "{a}, {b}"; |
||||
let html_escape = true; |
||||
let x: String = fmt_replace!(foo, html_escape, { |
||||
"a" => "1".to_string(), |
||||
"b" => "2".to_string(), |
||||
"c" => "3".to_owned(), |
||||
}); |
||||
} |
@ -0,0 +1,18 @@ |
||||
// Copyright (C) 2021-2022 Tassilo Horn <tsdh@gnu.org>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify it
|
||||
// under the terms of the GNU General Public License as published by the Free
|
||||
// Software Foundation, either version 3 of the License, or (at your option)
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
// more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along with
|
||||
// this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
pub mod fmt; |
||||
pub mod ipc; |
||||
pub mod util; |
@ -0,0 +1,36 @@ |
||||
// Copyright (C) 2021-2022 Tassilo Horn <tsdh@gnu.org>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify it
|
||||
// under the terms of the GNU General Public License as published by the Free
|
||||
// Software Foundation, either version 3 of the License, or (at your option)
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
// more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along with
|
||||
// this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
/// Utility stuff shared by swayr and swayrbar.
|
||||
use directories::ProjectDirs; |
||||
use std::fs::DirBuilder; |
||||
use std::path::Path; |
||||
|
||||
pub fn get_config_file_path(project: &str) -> Box<Path> { |
||||
let proj_dirs = ProjectDirs::from("", "", project).expect(""); |
||||
let user_config_dir = proj_dirs.config_dir(); |
||||
if !user_config_dir.exists() { |
||||
let sys_path = format!("/etc/xdg/{}/config.toml", project); |
||||
let sys_config_file = Path::new(sys_path.as_str()); |
||||
if sys_config_file.exists() { |
||||
return sys_config_file.into(); |
||||
} |
||||
DirBuilder::new() |
||||
.recursive(true) |
||||
.create(user_config_dir) |
||||
.unwrap(); |
||||
} |
||||
user_config_dir.join("config.toml").into_boxed_path() |
||||
} |
@ -1 +0,0 @@ |
||||
../../swayr/src/fmt_replace.rs |
@ -1 +0,0 @@ |
||||
../../swayr/src/ipc.rs |
@ -1 +0,0 @@ |
||||
../../swayr/src/rtfmt.rs |
@ -0,0 +1 @@ |
||||
../../swayr/src/shared |
Loading…
Reference in new issue