From 6e10aa4c24455f4583ada9aa27015d396a692781 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Fri, 8 Apr 2022 16:45:08 +0200 Subject: [PATCH] Restructure the shared parts of the lib --- Cargo.lock | 1 + swayr/src/cmds.rs | 4 +- swayr/src/config.rs | 24 ++------- swayr/src/fmt_replace.rs | 74 --------------------------- swayr/src/layout.rs | 4 +- swayr/src/lib.rs | 4 +- swayr/src/{rtfmt.rs => shared/fmt.rs} | 63 +++++++++++++++++++++-- swayr/src/{ => shared}/ipc.rs | 0 swayr/src/shared/mod.rs | 18 +++++++ swayr/src/shared/util.rs | 36 +++++++++++++ swayr/src/tree.rs | 8 +-- swayrbar/Cargo.toml | 3 +- swayrbar/src/bar/module/battery.rs | 2 +- swayrbar/src/bar/module/sysinfo.rs | 2 +- swayrbar/src/bar/module/window.rs | 6 +-- swayrbar/src/fmt_replace.rs | 1 - swayrbar/src/ipc.rs | 1 - swayrbar/src/lib.rs | 4 +- swayrbar/src/rtfmt.rs | 1 - swayrbar/src/shared | 1 + 20 files changed, 134 insertions(+), 123 deletions(-) delete mode 100644 swayr/src/fmt_replace.rs rename swayr/src/{rtfmt.rs => shared/fmt.rs} (69%) rename swayr/src/{ => shared}/ipc.rs (100%) create mode 100644 swayr/src/shared/mod.rs create mode 100644 swayr/src/shared/util.rs delete mode 120000 swayrbar/src/fmt_replace.rs delete mode 120000 swayrbar/src/ipc.rs delete mode 120000 swayrbar/src/rtfmt.rs create mode 120000 swayrbar/src/shared diff --git a/Cargo.lock b/Cargo.lock index 9405ab7..abd27b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -636,6 +636,7 @@ version = "0.0.1" dependencies = [ "battery", "chrono", + "directories", "env_logger", "log", "once_cell", diff --git a/swayr/src/cmds.rs b/swayr/src/cmds.rs index 4d2528a..89662d4 100644 --- a/swayr/src/cmds.rs +++ b/swayr/src/cmds.rs @@ -16,9 +16,9 @@ //! Functions and data structures of the swayr client. use crate::config as cfg; -use crate::ipc; -use crate::ipc::NodeMethods; use crate::layout; +use crate::shared::ipc; +use crate::shared::ipc::NodeMethods; use crate::tree as t; use crate::util; use crate::util::DisplayFormat; diff --git a/swayr/src/config.rs b/swayr/src/config.rs index f446aa7..f4afd4f 100644 --- a/swayr/src/config.rs +++ b/swayr/src/config.rs @@ -15,13 +15,11 @@ //! TOML configuration for swayr. -use directories::ProjectDirs; +use crate::shared::util as shared_util; use serde::{Deserialize, Serialize}; use std::collections::HashMap; -use std::fs::DirBuilder; use std::fs::OpenOptions; use std::io::{Read, Write}; -use std::path::Path; #[derive(Debug, Serialize, Deserialize)] pub struct Config { @@ -304,24 +302,8 @@ impl Default for Config { } } -fn get_config_file_path() -> Box { - let proj_dirs = ProjectDirs::from("", "", "swayr").expect(""); - let user_config_dir = proj_dirs.config_dir(); - if !user_config_dir.exists() { - let sys_config_file = Path::new("/etc/xdg/swayr/config.toml"); - 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() -} - pub fn save_config(cfg: Config) { - let path = get_config_file_path(); + let path = shared_util::get_config_file_path("swayr"); let content = toml::to_string_pretty(&cfg).expect("Cannot serialize config."); let mut file = OpenOptions::new() @@ -334,7 +316,7 @@ pub fn save_config(cfg: Config) { } pub fn load_config() -> Config { - let path = get_config_file_path(); + let path = shared_util::get_config_file_path("swayr"); if !path.exists() { save_config(Config::default()); // Tell the user that a fresh default config has been created. diff --git a/swayr/src/fmt_replace.rs b/swayr/src/fmt_replace.rs deleted file mode 100644 index dd612fb..0000000 --- a/swayr/src/fmt_replace.rs +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright (C) 2022 Tassilo Horn -// -// 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 . - -use once_cell::sync::Lazy; -use regex::Regex; - -pub static PLACEHOLDER_RX: Lazy = Lazy::new(|| { - Regex::new( - r"\{(?P[^}:]+)(?::(?P\{[^}]*\})(?P[^}]*))?\}", - ) - .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(), - }); -} diff --git a/swayr/src/layout.rs b/swayr/src/layout.rs index e2c08d6..d295fc3 100644 --- a/swayr/src/layout.rs +++ b/swayr/src/layout.rs @@ -16,8 +16,8 @@ //! Functions and data structures of the swayrd demon. use crate::config; -use crate::ipc; -use crate::ipc::NodeMethods; +use crate::shared::ipc; +use crate::shared::ipc::NodeMethods; use std::collections::HashMap; use swayipc as s; diff --git a/swayr/src/lib.rs b/swayr/src/lib.rs index 2cd4e15..6f046a0 100644 --- a/swayr/src/lib.rs +++ b/swayr/src/lib.rs @@ -22,9 +22,7 @@ pub mod client; pub mod cmds; pub mod config; pub mod demon; -pub mod fmt_replace; -pub mod ipc; pub mod layout; -pub mod rtfmt; +pub mod shared; pub mod tree; pub mod util; diff --git a/swayr/src/rtfmt.rs b/swayr/src/shared/fmt.rs similarity index 69% rename from swayr/src/rtfmt.rs rename to swayr/src/shared/fmt.rs index 60e6134..f6f5575 100644 --- a/swayr/src/rtfmt.rs +++ b/swayr/src/shared/fmt.rs @@ -13,10 +13,8 @@ // You should have received a copy of the GNU General Public License along with // this program. If not, see . -//! Provides runtime formatting of strings since our format strings are read -//! from the swayr config, not specified as literals, e.g., `{name:{:.30}}` in -//! `format.window_format`. - +use once_cell::sync::Lazy; +use regex::Regex; use rt_format::{ Format, FormatArgument, NoNamedArguments, ParsedFormat, Specifier, }; @@ -148,3 +146,60 @@ fn test_format() { assert_eq!(format("{:.5}", "sway window", "..."), "sw..."); assert_eq!(format("{:.2}", "sway", "..."), "..."); } + +pub static PLACEHOLDER_RX: Lazy = Lazy::new(|| { + Regex::new( + r"\{(?P[^}:]+)(?::(?P\{[^}]*\})(?P[^}]*))?\}", + ) + .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::shared::fmt::PLACEHOLDER_RX + .replace_all($fmt_str, |caps: ®ex::Captures| { + let value: String = match &caps["name"] { + $( + $( $pat )|+ => { + let val = $crate::shared::fmt::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::shared::fmt::maybe_html_escape( + $html_escape, + $crate::shared::fmt::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(), + }); +} diff --git a/swayr/src/ipc.rs b/swayr/src/shared/ipc.rs similarity index 100% rename from swayr/src/ipc.rs rename to swayr/src/shared/ipc.rs diff --git a/swayr/src/shared/mod.rs b/swayr/src/shared/mod.rs new file mode 100644 index 0000000..6be39e1 --- /dev/null +++ b/swayr/src/shared/mod.rs @@ -0,0 +1,18 @@ +// Copyright (C) 2021-2022 Tassilo Horn +// +// 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 . + +pub mod fmt; +pub mod ipc; +pub mod util; diff --git a/swayr/src/shared/util.rs b/swayr/src/shared/util.rs new file mode 100644 index 0000000..f32011e --- /dev/null +++ b/swayr/src/shared/util.rs @@ -0,0 +1,36 @@ +// Copyright (C) 2021-2022 Tassilo Horn +// +// 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 . + +/// 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 { + 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() +} diff --git a/swayr/src/tree.rs b/swayr/src/tree.rs index 99dd7b9..54ab39c 100644 --- a/swayr/src/tree.rs +++ b/swayr/src/tree.rs @@ -16,9 +16,9 @@ //! Convenience data structures built from the IPC structs. use crate::config; -use crate::fmt_replace::fmt_replace; -use crate::ipc; -use crate::ipc::NodeMethods; +use crate::shared::fmt::fmt_replace; +use crate::shared::ipc; +use crate::shared::ipc::NodeMethods; use crate::util; use crate::util::DisplayFormat; use once_cell::sync::Lazy; @@ -186,7 +186,7 @@ impl<'a> Tree<'a> { self.as_display_nodes(&v, IndentLevel::WorkspacesZeroWindowsOne) } - fn sort_by_urgency_and_lru_time_1(&self, v: &mut Vec<&s::Node>) { + fn sort_by_urgency_and_lru_time_1(&self, v: &mut [&s::Node]) { v.sort_by(|a, b| { if a.urgent && !b.urgent { cmp::Ordering::Less diff --git a/swayrbar/Cargo.toml b/swayrbar/Cargo.toml index dfd31bd..0879966 100644 --- a/swayrbar/Cargo.toml +++ b/swayrbar/Cargo.toml @@ -7,11 +7,10 @@ repository = "https://git.sr.ht/~tsdh/swayr" authors = ["Tassilo Horn "] license = "GPL-3.0+" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] battery = "0.7.8" chrono = "0.4" +directories = "4.0" env_logger = { version = "0.9.0", default-features = false, features = ["termcolor", "atty", "humantime"] } # without regex log = "0.4" once_cell = "1.10.0" diff --git a/swayrbar/src/bar/module/battery.rs b/swayrbar/src/bar/module/battery.rs index 0ded74d..3e327f5 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::fmt_replace::fmt_replace; +use crate::shared::fmt::fmt_replace; use battery as bat; use std::collections::{HashMap, HashSet}; use swaybar_types as s; diff --git a/swayrbar/src/bar/module/sysinfo.rs b/swayrbar/src/bar/module/sysinfo.rs index 25007df..07c61a7 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::fmt_replace::fmt_replace; +use crate::shared::fmt::fmt_replace; use std::collections::HashMap; use std::sync::Mutex; use std::sync::Once; diff --git a/swayrbar/src/bar/module/window.rs b/swayrbar/src/bar/module/window.rs index 7598505..b56057f 100644 --- a/swayrbar/src/bar/module/window.rs +++ b/swayrbar/src/bar/module/window.rs @@ -19,9 +19,9 @@ use std::collections::HashMap; use crate::bar::config; use crate::bar::module::BarModuleFn; -use crate::fmt_replace::fmt_replace; -use crate::ipc; -use crate::ipc::NodeMethods; +use crate::shared::fmt::fmt_replace; +use crate::shared::ipc; +use crate::shared::ipc::NodeMethods; use swaybar_types as s; const NAME: &str = "window"; diff --git a/swayrbar/src/fmt_replace.rs b/swayrbar/src/fmt_replace.rs deleted file mode 120000 index 1b48b5f..0000000 --- a/swayrbar/src/fmt_replace.rs +++ /dev/null @@ -1 +0,0 @@ -../../swayr/src/fmt_replace.rs \ No newline at end of file diff --git a/swayrbar/src/ipc.rs b/swayrbar/src/ipc.rs deleted file mode 120000 index 088362c..0000000 --- a/swayrbar/src/ipc.rs +++ /dev/null @@ -1 +0,0 @@ -../../swayr/src/ipc.rs \ No newline at end of file diff --git a/swayrbar/src/lib.rs b/swayrbar/src/lib.rs index 45716e3..4cba831 100644 --- a/swayrbar/src/lib.rs +++ b/swayrbar/src/lib.rs @@ -14,6 +14,4 @@ // this program. If not, see . pub mod bar; -pub mod fmt_replace; -pub mod ipc; -pub mod rtfmt; +pub mod shared; diff --git a/swayrbar/src/rtfmt.rs b/swayrbar/src/rtfmt.rs deleted file mode 120000 index 0d9eac8..0000000 --- a/swayrbar/src/rtfmt.rs +++ /dev/null @@ -1 +0,0 @@ -../../swayr/src/rtfmt.rs \ No newline at end of file diff --git a/swayrbar/src/shared b/swayrbar/src/shared new file mode 120000 index 0000000..15bef5f --- /dev/null +++ b/swayrbar/src/shared @@ -0,0 +1 @@ +../../swayr/src/shared \ No newline at end of file