From bbc59370e77f66956106fc2bd8a4a29bae23ec30 Mon Sep 17 00:00:00 2001 From: Wei Zhang Date: Fri, 1 Mar 2024 23:36:05 +0800 Subject: [PATCH] :sparkles: add xdg dir back Signed-off-by: Wei Zhang --- Cargo.lock | 127 +++++++++++++++++++++++++++----------- Cargo.toml | 1 + src/config_file.rs | 9 ++- src/core.rs | 9 ++- src/meta/windows_utils.rs | 14 +---- src/theme.rs | 56 ++++++++--------- 6 files changed, 135 insertions(+), 81 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c3321bfb6..0a662a32e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -257,6 +257,12 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "errno" version = "0.3.5" @@ -424,9 +430,9 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.11.2" +version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" [[package]] name = "heck" @@ -486,11 +492,11 @@ dependencies = [ [[package]] name = "indexmap" -version = "1.8.0" +version = "2.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223" +checksum = "233cf39063f058ea2caae4091bf4a3ef70a653afbc026f5c4a4135d114e3c177" dependencies = [ - "autocfg", + "equivalent", "hashbrown", ] @@ -521,7 +527,7 @@ checksum = "28dfb6c8100ccc63462345b67d1bbc3679177c75ee4bf59bf29c8b1d110b8189" dependencies = [ "hermit-abi 0.2.6", "io-lifetimes", - "rustix", + "rustix 0.36.16", "windows-sys 0.42.0", ] @@ -543,6 +549,12 @@ dependencies = [ "either", ] +[[package]] +name = "itoa" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" + [[package]] name = "jobserver" version = "0.1.26" @@ -600,6 +612,12 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" +[[package]] +name = "linux-raw-sys" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" + [[package]] name = "lock_api" version = "0.4.6" @@ -620,9 +638,12 @@ dependencies = [ [[package]] name = "lscolors" -version = "0.15.0" +version = "0.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf7015a04103ad78abb77e4b79ed151e767922d1cfde5f62640471c629a2320d" +checksum = "ab0b209ec3976527806024406fe765474b9a1750a0ed4b8f0372364741f50e7b" +dependencies = [ + "nu-ansi-term", +] [[package]] name = "lsd" @@ -649,7 +670,7 @@ dependencies = [ "sys-locale", "tempfile", "term_grid", - "terminal_size 0.1.17", + "terminal_size 0.3.0", "thiserror", "unicode-width", "url", @@ -659,6 +680,7 @@ dependencies = [ "wild", "windows", "xattr", + "xdg", "yaml-rust", ] @@ -692,6 +714,15 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" +[[package]] +name = "nu-ansi-term" +version = "0.49.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c073d3c1930d0751774acf49e66653acecb416c3a54c6ec095a9b11caddb5a68" +dependencies = [ + "windows-sys 0.48.0", +] + [[package]] name = "num-integer" version = "0.1.44" @@ -854,9 +885,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.69" +version = "1.0.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" dependencies = [ "unicode-ident", ] @@ -869,9 +900,9 @@ checksum = "b45c49fc4f91f35bae654f85ebb3a44d60ac64f11b3166ffa609def390c732d8" [[package]] name = "quote" -version = "1.0.33" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] @@ -938,10 +969,23 @@ dependencies = [ "errno", "io-lifetimes", "libc", - "linux-raw-sys", + "linux-raw-sys 0.1.4", "windows-sys 0.45.0", ] +[[package]] +name = "rustix" +version = "0.38.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b426b0506e5d50a7d8dafcf2e81471400deb602392c7dd110815afb4eaf02a3" +dependencies = [ + "bitflags 2.4.0", + "errno", + "libc", + "linux-raw-sys 0.4.13", + "windows-sys 0.48.0", +] + [[package]] name = "ryu" version = "1.0.9" @@ -965,34 +1009,35 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "serde" -version = "1.0.136" +version = "1.0.197" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789" +checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.136" +version = "1.0.197" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" +checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" dependencies = [ "proc-macro2", "quote", - "syn 1.0.89", + "syn 2.0.51", ] [[package]] name = "serde_yaml" -version = "0.8.23" +version = "0.9.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4a521f2940385c165a24ee286aa8599633d162077a54bdcae2a6fd5a7bfa7a0" +checksum = "8fd075d994154d4a774f95b51fb96bdc2832b0ea48425c92546073816cda1f2f" dependencies = [ "indexmap", + "itoa", "ryu", "serde", - "yaml-rust", + "unsafe-libyaml", ] [[package]] @@ -1017,7 +1062,7 @@ checksum = "91d129178576168c589c9ec973feedf7d3126c01ac2bf08795109aa35b69fb8f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.38", + "syn 2.0.51", ] [[package]] @@ -1084,9 +1129,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.38" +version = "2.0.51" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b" +checksum = "6ab617d94515e94ae53b8406c628598680aa0c9587474ecbe58188f7b345d66c" dependencies = [ "proc-macro2", "quote", @@ -1118,9 +1163,9 @@ dependencies = [ [[package]] name = "term_grid" -version = "0.1.7" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "230d3e804faaed5a39b08319efb797783df2fd9671b39b7596490cb486d702cf" +checksum = "a7c9eb7705cb3f0fd71d3955b23db6d372142ac139e8c473952c93bf3c3dc4b7" dependencies = [ "unicode-width", ] @@ -1136,22 +1181,22 @@ dependencies = [ [[package]] name = "terminal_size" -version = "0.1.17" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "633c1a546cee861a1a6d0dc69ebeca693bf4296661ba7852b9d21d159e0506df" +checksum = "cb20089a8ba2b69debd491f8d2d023761cbf196e999218c591fa1e7e15a21907" dependencies = [ - "libc", - "winapi", + "rustix 0.36.16", + "windows-sys 0.42.0", ] [[package]] name = "terminal_size" -version = "0.2.3" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb20089a8ba2b69debd491f8d2d023761cbf196e999218c591fa1e7e15a21907" +checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7" dependencies = [ - "rustix", - "windows-sys 0.42.0", + "rustix 0.38.21", + "windows-sys 0.48.0", ] [[package]] @@ -1247,6 +1292,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" +[[package]] +name = "unsafe-libyaml" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab4c90930b95a82d00dc9e9ac071b4991924390d46cbd0dfe566148667605e4b" + [[package]] name = "url" version = "2.1.1" @@ -1572,6 +1623,12 @@ dependencies = [ "libc", ] +[[package]] +name = "xdg" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "213b7324336b53d2414b2db8537e56544d981803139155afa84f76eeebb7a546" + [[package]] name = "yaml-rust" version = "0.4.5" diff --git a/Cargo.toml b/Cargo.toml index c3d77d0dc..dc2086eec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,6 +42,7 @@ serde = { version = "1.0", features = ["derive"] } serde_yaml = "0.9" url = "2.1" vsort = "0.2" +xdg = "2.5" [target."cfg(not(all(windows, target_arch = \"x86\", target_env = \"gnu\")))".dependencies] # if ssl feature is enabled compilation will fail on arm-unknown-linux-gnueabihf and i686-pc-windows-gnu diff --git a/src/config_file.rs b/src/config_file.rs index dee21edd7..80f40ba89 100644 --- a/src/config_file.rs +++ b/src/config_file.rs @@ -173,9 +173,16 @@ impl Config { /// `%APPDATA%\lsd` or `%USERPROFILE%\.config\lsd` in that order. /// This will apply both to the config file and the theme file. pub fn config_paths() -> impl Iterator { + #[cfg(not(windows))] + use xdg::BaseDirectories; + [ - dirs::config_dir(), dirs::home_dir().map(|h| h.join(".config")), + dirs::config_dir(), + #[cfg(not(windows))] + BaseDirectories::with_prefix("") + .ok() + .map(|p| p.get_config_home()), ] .iter() .filter_map(|p| p.as_ref().map(|p| p.join("lsd"))) diff --git a/src/core.rs b/src/core.rs index 7e90770f6..cc526a1ff 100644 --- a/src/core.rs +++ b/src/core.rs @@ -18,8 +18,6 @@ use std::os::unix::io::AsRawFd; use crate::flags::blocks::Block; use crate::git_theme::GitTheme; #[cfg(target_os = "windows")] -use crate::meta::windows_utils; -#[cfg(target_os = "windows")] use terminal_size::terminal_size; pub struct Core { @@ -107,7 +105,12 @@ impl Core { }; #[cfg(target_os = "windows")] - let paths: Vec = paths.into_iter().map(windows_utils::expand_home).collect(); + use crate::config_file; + #[cfg(target_os = "windows")] + let paths: Vec = paths + .into_iter() + .filter_map(config_file::expand_home) + .collect(); for path in paths { let mut meta = diff --git a/src/meta/windows_utils.rs b/src/meta/windows_utils.rs index a9db17251..e647c26a5 100644 --- a/src/meta/windows_utils.rs +++ b/src/meta/windows_utils.rs @@ -2,7 +2,7 @@ use std::ffi::{OsStr, OsString}; use std::io; use std::mem::MaybeUninit; use std::os::windows::ffi::{OsStrExt, OsStringExt}; -use std::path::{Path, PathBuf}; +use std::path::Path; use windows::Win32::Foundation::PSID; use windows::Win32::Security::{self, Authorization::TRUSTEE_W, ACL}; @@ -343,18 +343,6 @@ pub fn is_path_system(path: &Path) -> bool { ) } -/// Expands the `~` in a path to the current user's home directory -pub fn expand_home(path: PathBuf) -> PathBuf { - if path.starts_with("~") { - if let Some(home) = dirs::home_dir() { - let mut expanded = home.to_path_buf(); - expanded.push(path.strip_prefix("~").unwrap()); - return expanded; - } - } - path -} - #[cfg(test)] mod test { use super::*; diff --git a/src/theme.rs b/src/theme.rs index 82daeb488..0b54b65bc 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -27,14 +27,12 @@ pub struct Theme { #[derive(Error, Debug)] pub enum Error { - #[error("Theme file not existed")] - NotExisted(#[from] io::Error), + #[error("Can not read the theme file")] + ReadFailed(#[from] io::Error), #[error("Theme file format invalid")] InvalidFormat(#[from] serde_yaml::Error), #[error("Theme file path invalid {0}")] InvalidPath(String), - #[error("Unknown Theme error")] - Unknown(), } impl Theme { @@ -51,37 +49,37 @@ impl Theme { print_error!("Not a valid theme file path: {}.", &file); return Err(Error::InvalidPath(file.to_string())); }; - let path = if Path::new(&real).is_absolute() { - real + + let mut paths = if Path::new(&real).is_absolute() { + vec![real].into_iter() } else { - let path = config_file::Config::config_paths() + config_file::Config::config_paths() .map(|p| p.join(real.clone())) - .find(|p| p.is_file()); - match path { - Some(p) => p, - None => { - return Err(Error::InvalidPath( - "Did not find theme file in config folders".into(), - )) - } - } + .collect::>() + .into_iter() }; - // try `yml` if `yaml` extension file not found or error - let mut err: Error = Error::Unknown(); - for ext in ["yaml", "yml"] { - match fs::read(&path.with_extension(ext)) { - Ok(f) => match Self::with_yaml(&String::from_utf8_lossy(&f)) { - Ok(t) => return Ok(t), - Err(e) => { - err = Error::from(e); - } - }, - Err(e) => err = Error::from(e), + let Some(valid) = paths.find_map(|p| { + let yaml = p.with_extension("yaml"); + let yml = p.with_extension("yml"); + if yaml.is_file() { + Some(yaml) + } else if yml.is_file() { + Some(yml) + } else { + None } - } + }) else { + return Err(Error::InvalidPath("No valid theme file found".to_string())); + }; - Err(err) + match fs::read_to_string(valid) { + Ok(yaml) => match Self::with_yaml(&yaml) { + Ok(t) => Ok(t), + Err(e) => Err(Error::InvalidFormat(e)), + }, + Err(e) => Err(Error::ReadFailed(e)), + } } /// This constructs a Theme struct with a passed [Yaml] str.