Use XDG conventions on macOS too

This commit is contained in:
Utkarsh Gupta 2023-04-22 17:21:14 +05:30
parent 6a223afae3
commit 21f93c84b1
No known key found for this signature in database
GPG key ID: 428BFDF197596BDC
3 changed files with 55 additions and 17 deletions

21
Cargo.lock generated
View file

@ -281,6 +281,16 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "etcetera"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51822eedc6129d8c4d96cec86d56b785e983f943c9ce9fb892e0c2a99a7f47a0"
dependencies = [
"cfg-if",
"home",
]
[[package]] [[package]]
name = "fs_at" name = "fs_at"
version = "0.1.4" version = "0.1.4"
@ -325,6 +335,15 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286" checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286"
[[package]]
name = "home"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "747309b4b440c06d57b0b25f2aee03ee9b5e5397d288c60e21fc709bb98a7408"
dependencies = [
"winapi",
]
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "1.9.3" version = "1.9.3"
@ -434,10 +453,10 @@ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
"crossterm", "crossterm",
"directories-next",
"dns_common", "dns_common",
"dns_common_derive", "dns_common_derive",
"edit", "edit",
"etcetera",
"lazy_static", "lazy_static",
"regex", "regex",
"remove_dir_all 0.8.2", "remove_dir_all 0.8.2",

View file

@ -23,7 +23,7 @@ regex = { version = "1.7.3", default-features = false, features = ["std", "unico
clap = { version = "4.2.1", features = ["derive", "cargo"] } clap = { version = "4.2.1", features = ["derive", "cargo"] }
crossterm = "0.26.1" crossterm = "0.26.1"
lazy_static = "1.4.0" lazy_static = "1.4.0"
directories-next = "2.0.0" etcetera = "0.7.1"
walkdir = "2.3.3" walkdir = "2.3.3"
shellwords = "1.1.0" shellwords = "1.1.0"
anyhow = "1.0.70" anyhow = "1.0.70"

View file

@ -4,7 +4,7 @@ use crate::parser::Parser;
use crate::prelude::*; use crate::prelude::*;
use crate::structures::fetcher; use crate::structures::fetcher;
use directories_next::BaseDirs; use etcetera::BaseStrategy;
use regex::Regex; use regex::Regex;
use std::cell::RefCell; use std::cell::RefCell;
@ -46,8 +46,20 @@ fn compiled_default_path(path: Option<&str>) -> Option<PathBuf> {
} }
pub fn default_cheat_pathbuf() -> Result<PathBuf> { pub fn default_cheat_pathbuf() -> Result<PathBuf> {
let base_dirs = BaseDirs::new().ok_or_else(|| anyhow!("Unable to get base dirs"))?; if cfg!(target_os = "macos") {
let mut pathbuf = PathBuf::from(base_dirs.data_dir()); let base_dirs = etcetera::base_strategy::Apple::new()?;
let mut pathbuf = base_dirs.data_dir();
pathbuf.push("navi");
pathbuf.push("cheats");
if pathbuf.exists() {
return Ok(pathbuf);
}
}
let base_dirs = etcetera::choose_base_strategy()?;
let mut pathbuf = base_dirs.data_dir();
pathbuf.push("navi"); pathbuf.push("navi");
pathbuf.push("cheats"); pathbuf.push("cheats");
if !pathbuf.exists() { if !pathbuf.exists() {
@ -59,9 +71,20 @@ pub fn default_cheat_pathbuf() -> Result<PathBuf> {
} }
pub fn default_config_pathbuf() -> Result<PathBuf> { pub fn default_config_pathbuf() -> Result<PathBuf> {
let base_dirs = BaseDirs::new().ok_or_else(|| anyhow!("Unable to get base dirs"))?; if cfg!(target_os = "macos") {
let base_dirs = etcetera::base_strategy::Apple::new()?;
let mut pathbuf = PathBuf::from(base_dirs.config_dir()); let mut pathbuf = base_dirs.config_dir();
pathbuf.push("navi");
pathbuf.push("config.yaml");
if pathbuf.exists() {
return Ok(pathbuf);
}
}
let base_dirs = etcetera::choose_base_strategy()?;
let mut pathbuf = base_dirs.config_dir();
pathbuf.push("navi"); pathbuf.push("navi");
pathbuf.push("config.yaml"); pathbuf.push("config.yaml");
if !pathbuf.exists() { if !pathbuf.exists() {
@ -132,13 +155,13 @@ impl fetcher::Fetcher for Fetcher {
let folders = paths_from_path_param(&interpolated_paths); let folders = paths_from_path_param(&interpolated_paths);
let home_regex = Regex::new(r"^~").unwrap(); let home_regex = Regex::new(r"^~").unwrap();
let home = BaseDirs::new().map(|b| b.home_dir().to_string()); let home = etcetera::home_dir().ok();
// parser.filter = self.tag_rules.as_ref().map(|r| gen_lists(r.as_str())); // parser.filter = self.tag_rules.as_ref().map(|r| gen_lists(r.as_str()));
for folder in folders { for folder in folders {
let interpolated_folder = match &home { let interpolated_folder = match &home {
Some(h) => home_regex.replace(folder, h).to_string(), Some(h) => home_regex.replace(folder, h.to_string_lossy()).to_string(),
None => folder.to_string(), None => folder.to_string(),
}; };
let folder_pathbuf = PathBuf::from(interpolated_folder); let folder_pathbuf = PathBuf::from(interpolated_folder);
@ -228,12 +251,10 @@ mod tests {
#[test] #[test]
fn test_default_config_pathbuf() { fn test_default_config_pathbuf() {
let base_dirs = BaseDirs::new() let base_dirs = etcetera::choose_base_strategy().expect("could not determine base directories");
.ok_or_else(|| anyhow!("bad"))
.expect("could not determine base directories");
let expected = { let expected = {
let mut e = base_dirs.config_dir().to_path_buf(); let mut e = base_dirs.config_dir();
e.push("navi"); e.push("navi");
e.push("config.yaml"); e.push("config.yaml");
e.to_string_lossy().to_string() e.to_string_lossy().to_string()
@ -246,12 +267,10 @@ mod tests {
#[test] #[test]
fn test_default_cheat_pathbuf() { fn test_default_cheat_pathbuf() {
let base_dirs = BaseDirs::new() let base_dirs = etcetera::choose_base_strategy().expect("could not determine base directories");
.ok_or_else(|| anyhow!("bad"))
.expect("could not determine base directories");
let expected = { let expected = {
let mut e = base_dirs.data_dir().to_path_buf(); let mut e = base_dirs.data_dir();
e.push("navi"); e.push("navi");
e.push("cheats"); e.push("cheats");
e.to_string_lossy().to_string() e.to_string_lossy().to_string()