Skip to content

Commit c8fe9a7

Browse files
committed
Use etcetera instead of directories
It provides more flexibility and it has a better default for cli tools. Fixes #1107.
1 parent 515e464 commit c8fe9a7

File tree

11 files changed

+59
-54
lines changed

11 files changed

+59
-54
lines changed

Cargo.lock

Lines changed: 13 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ chrono-humanize = "0.2.3"
5252
clap = { version = "4.2", features = ["derive"] }
5353
colors-transform = "0.2.11"
5454
criterion = "0.5"
55-
directories = "4.0"
55+
etcetera = "0.8.0"
5656
futures = "0.3"
5757
fuzzy-matcher = "0.3"
5858
grep-matcher = "0.1"

crates/dirs/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ version.workspace = true
44
edition.workspace = true
55

66
[dependencies]
7-
directories = { workspace = true }
7+
etcetera = { workspace = true }

crates/dirs/src/lib.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use directories::{BaseDirs, ProjectDirs};
2-
use std::path::PathBuf;
1+
use etcetera::app_strategy::Xdg;
2+
use etcetera::{choose_app_strategy, AppStrategy, AppStrategyArgs};
3+
use std::path::{Path, PathBuf};
34
use std::sync::OnceLock;
45

56
pub struct Dirs;
@@ -8,26 +9,43 @@ impl Dirs {
89
/// Project directory specifically for Vim Clap.
910
///
1011
/// All the files created by vim-clap are stored there.
11-
pub fn project() -> &'static ProjectDirs {
12-
static CELL: OnceLock<ProjectDirs> = OnceLock::new();
12+
pub fn project() -> &'static Xdg {
13+
static CELL: OnceLock<Xdg> = OnceLock::new();
1314

1415
CELL.get_or_init(|| {
15-
ProjectDirs::from("org", "vim", "Vim Clap")
16-
.expect("Couldn't create project directory for vim-clap")
16+
choose_app_strategy(AppStrategyArgs {
17+
top_level_domain: "org".to_string(),
18+
author: "vim".to_owned(),
19+
app_name: "Vim Clap".to_owned(),
20+
})
21+
.expect("Couldn't create project directory for vim-clap")
1722
})
1823
}
1924

20-
/// Provides access to the standard directories that the operating system uses.
21-
pub fn base() -> &'static BaseDirs {
22-
static CELL: OnceLock<BaseDirs> = OnceLock::new();
25+
/// Get the home directory
26+
pub fn home_dir() -> &'static Path {
27+
Self::project().home_dir()
28+
}
29+
30+
/// Get the config directory
31+
pub fn config_dir() -> PathBuf {
32+
Self::project().config_dir()
33+
}
34+
35+
/// Get the cache directory
36+
pub fn cache_dir() -> PathBuf {
37+
Self::project().cache_dir()
38+
}
2339

24-
CELL.get_or_init(|| BaseDirs::new().expect("Failed to construct BaseDirs"))
40+
/// Get the data directory
41+
pub fn data_dir() -> PathBuf {
42+
Self::project().data_dir()
2543
}
2644

2745
/// Cache directory for Vim Clap project.
2846
pub fn clap_cache_dir() -> std::io::Result<PathBuf> {
2947
let cache_dir = Self::project().cache_dir();
30-
std::fs::create_dir_all(cache_dir)?;
31-
Ok(cache_dir.to_path_buf())
48+
std::fs::create_dir_all(&cache_dir)?;
49+
Ok(cache_dir)
3250
}
3351
}

crates/maple_config/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ fn load_config(
1414
specified_config_file: Option<PathBuf>,
1515
) -> (Config, PathBuf, Option<toml::de::Error>) {
1616
let config_file = specified_config_file.unwrap_or_else(|| {
17-
// Linux: ~/.config/vimclap/config.toml
18-
// macOS: ~/Library/Application\ Support/org.vim.Vim-Clap/config.toml
17+
// Linuxi & macOS: ~/.config/vimclap/config.toml
1918
// Windows: ~\AppData\Roaming\Vim\Vim Clap\config\config.toml
20-
let config_file_path = Dirs::project().config_dir().join("config.toml");
19+
let config_file_path = Dirs::config_dir().join("config.toml");
2120

2221
if !config_file_path.exists() {
2322
std::fs::create_dir_all(&config_file_path).ok();

crates/maple_core/src/datastore/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ pub fn cache_metadata_path() -> Option<&'static PathBuf> {
6969

7070
/// Returns a `PathBuf` using given file name under the project data directory.
7171
pub fn generate_data_file_path(filename: &str) -> std::io::Result<PathBuf> {
72-
let data_dir = Dirs::project().data_dir();
73-
std::fs::create_dir_all(data_dir)?;
72+
let data_dir = Dirs::data_dir();
73+
std::fs::create_dir_all(&data_dir)?;
7474
Ok(data_dir.join(filename))
7575
}
7676

7777
/// Returns a `PathBuf` using given file name under the project cache directory.
7878
pub fn generate_cache_file_path(filename: impl AsRef<Path>) -> std::io::Result<PathBuf> {
79-
let cache_dir = Dirs::project().cache_dir();
80-
std::fs::create_dir_all(cache_dir)?;
79+
let cache_dir = Dirs::cache_dir();
80+
std::fs::create_dir_all(&cache_dir)?;
8181
Ok(cache_dir.join(filename))
8282
}
8383

crates/maple_core/src/searcher/tagfiles.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl TagItem {
3434
let mut home_path = PathBuf::new();
3535
let path = Path::new(&self.path);
3636
let path = path.strip_prefix(cwd).unwrap_or({
37-
path.strip_prefix(Dirs::base().home_dir())
37+
path.strip_prefix(Dirs::home_dir())
3838
.map(|path| {
3939
home_path.push("~");
4040
home_path = home_path.join(path);

crates/maple_core/src/stdio_server/winbar.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub async fn update_winbar(
6868
}
6969
None => winwidth,
7070
};
71-
let path = if let Some(home) = dirs::Dirs::base().home_dir().to_str() {
71+
let path = if let Some(home) = dirs::Dirs::home_dir().to_str() {
7272
path.replacen(home, "~", 1)
7373
} else {
7474
path

crates/maple_core/src/tools/ctags/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub static DEFAULT_EXCLUDE_OPT: Lazy<String> = Lazy::new(|| {
3333

3434
/// Directory for the `tags` files.
3535
pub static CTAGS_TAGS_DIR: Lazy<PathBuf> = Lazy::new(|| {
36-
let tags_dir = Dirs::project().data_dir().join("tags");
36+
let tags_dir = Dirs::data_dir().join("tags");
3737

3838
std::fs::create_dir_all(&tags_dir).expect("Couldn't create tags directory for vim-clap");
3939

crates/maple_core/src/tools/gtags/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub static GTAGS_EXISTS: Lazy<bool> = Lazy::new(|| gtags_executable_exists().unw
66

77
/// Directory for `GTAGS`/`GRTAGS`.
88
pub static GTAGS_DIR: Lazy<PathBuf> = Lazy::new(|| {
9-
let gtags_dir = Dirs::project().data_dir().join("gtags");
9+
let gtags_dir = Dirs::data_dir().join("gtags");
1010

1111
std::fs::create_dir_all(&gtags_dir).expect("Couldn't create gtags directory for vim-clap");
1212

0 commit comments

Comments
 (0)