From 4ccd9796c43c7e993baf033fb9a1538d306b08da Mon Sep 17 00:00:00 2001 From: Noah Mayr Date: Fri, 9 Jun 2023 18:58:28 +0200 Subject: [PATCH] Use xdg-rust crate instead of dirs crate (#631) * Use xdg-rust crate instead of dirs crate * Fix clippy warning --- Cargo.lock | 48 ++++++++++++++++++++---------------------------- Cargo.toml | 5 +++-- src/app.rs | 2 +- src/dirs.rs | 25 +++++++++++++++++++++++++ src/lib.rs | 1 + src/path.rs | 1 + 6 files changed, 51 insertions(+), 31 deletions(-) create mode 100644 src/dirs.rs diff --git a/Cargo.lock b/Cargo.lock index 48a08f8..89ceb85 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -440,15 +440,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" -[[package]] -name = "dirs" -version = "5.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" -dependencies = [ - "dirs-sys", -] - [[package]] name = "dirs-next" version = "2.0.0" @@ -459,18 +450,6 @@ dependencies = [ "dirs-sys-next", ] -[[package]] -name = "dirs-sys" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" -dependencies = [ - "libc", - "option-ext", - "redox_users", - "windows-sys 0.48.0", -] - [[package]] name = "dirs-sys-next" version = "0.1.2" @@ -572,6 +551,15 @@ dependencies = [ "libc", ] +[[package]] +name = "home" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5444c27eef6923071f7ebcc33e3444508466a76f7a2b93da00ed6e19f30c1ddb" +dependencies = [ + "windows-sys 0.48.0", +] + [[package]] name = "humansize" version = "2.1.3" @@ -895,12 +883,6 @@ version = "11.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" -[[package]] -name = "option-ext" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" - [[package]] name = "os_str_bytes" version = "6.5.0" @@ -1797,6 +1779,15 @@ version = "0.48.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" +[[package]] +name = "xdg" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "688597db5a750e9cad4511cb94729a078e274308099a0382b5b8203bbc767fee" +dependencies = [ + "home", +] + [[package]] name = "xplr" version = "0.21.2" @@ -1806,8 +1797,8 @@ dependencies = [ "assert_cmd", "criterion", "crossterm", - "dirs", "gethostname", + "home", "humansize", "indexmap", "jf", @@ -1830,4 +1821,5 @@ dependencies = [ "time 0.3.21", "tui-input", "which", + "xdg", ] diff --git a/Cargo.toml b/Cargo.toml index 336d25e..101f350 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,19 +28,20 @@ natord = "1.0.9" anyhow = "1.0.71" serde_yaml = "0.9.21" crossterm = { version = "0.26.1", features = [], default-features = false } -dirs = "5.0.1" ansi-to-tui = "3.0.0" regex = "1.8.1" gethostname = "0.4.3" serde_json = "1.0.96" path-absolutize = "3.1.0" which = "4.4.0" -nu-ansi-term = "0.47.0" +nu-ansi-term = "0.47.0" textwrap = "0.16" snailquote = "0.3.1" skim = { version = "0.10.4", default-features = false } time = { version = "0.3.21", features = ["serde", "local-offset", "formatting", "macros"] } jf = "0.3.1" +xdg = "2.5.0" +home = "0.5.5" [dependencies.lscolors] version = "0.14.0" diff --git a/src/app.rs b/src/app.rs index 1845edf..347abdb 100644 --- a/src/app.rs +++ b/src/app.rs @@ -2,6 +2,7 @@ use crate::config::Config; use crate::config::Hooks; use crate::config::Mode; pub use crate::directory_buffer::DirectoryBuffer; +use crate::dirs; use crate::explorer; use crate::input::{InputOperation, Key}; use crate::lua; @@ -320,7 +321,6 @@ impl App { let pid = std::process::id(); let mut session_path = dirs::runtime_dir() - .unwrap_or_else(env::temp_dir) .join("xplr") .join("session") .join(pid.to_string()) diff --git a/src/dirs.rs b/src/dirs.rs new file mode 100644 index 0000000..3f4425d --- /dev/null +++ b/src/dirs.rs @@ -0,0 +1,25 @@ +use std::{env, path::PathBuf}; + +use lazy_static::lazy_static; +use xdg::BaseDirectories; + +lazy_static! { + pub static ref BASE_DIRS: Option = BaseDirectories::new().ok(); +} + +pub fn home_dir() -> Option { + home::home_dir() +} + +pub fn config_dir() -> Option { + BASE_DIRS.as_ref().map(|base| base.get_config_home()) +} + +pub fn runtime_dir() -> PathBuf { + let Some(dir) = BASE_DIRS + .as_ref() + .and_then(|base| base.get_runtime_directory().ok()) else { + return env::temp_dir(); + }; + dir.to_owned() +} diff --git a/src/lib.rs b/src/lib.rs index eecb3c5..ff2e342 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,7 @@ pub mod cli; pub mod compat; pub mod config; pub mod directory_buffer; +pub mod dirs; pub mod event_reader; pub mod explorer; pub mod input; diff --git a/src/path.rs b/src/path.rs index 2582d73..1f18725 100644 --- a/src/path.rs +++ b/src/path.rs @@ -1,3 +1,4 @@ +use crate::dirs; use anyhow::{bail, Result}; use lazy_static::lazy_static; use serde::{Deserialize, Serialize};