Skip to content

Commit

Permalink
Use XDG conventions on macOS too
Browse files Browse the repository at this point in the history
  • Loading branch information
utkarshgupta137 committed Apr 24, 2023
1 parent 6a223af commit 21f93c8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
21 changes: 20 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -23,7 +23,7 @@ regex = { version = "1.7.3", default-features = false, features = ["std", "unico
clap = { version = "4.2.1", features = ["derive", "cargo"] }
crossterm = "0.26.1"
lazy_static = "1.4.0"
directories-next = "2.0.0"
etcetera = "0.7.1"
walkdir = "2.3.3"
shellwords = "1.1.0"
anyhow = "1.0.70"
Expand Down
49 changes: 34 additions & 15 deletions src/filesystem.rs
Expand Up @@ -4,7 +4,7 @@ use crate::parser::Parser;
use crate::prelude::*;

use crate::structures::fetcher;
use directories_next::BaseDirs;
use etcetera::BaseStrategy;
use regex::Regex;

use std::cell::RefCell;
Expand Down Expand Up @@ -46,8 +46,20 @@ fn compiled_default_path(path: Option<&str>) -> Option<PathBuf> {
}

pub fn default_cheat_pathbuf() -> Result<PathBuf> {
let base_dirs = BaseDirs::new().ok_or_else(|| anyhow!("Unable to get base dirs"))?;
let mut pathbuf = PathBuf::from(base_dirs.data_dir());
if cfg!(target_os = "macos") {
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("cheats");
if !pathbuf.exists() {
Expand All @@ -59,9 +71,20 @@ pub fn default_cheat_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 = 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 = PathBuf::from(base_dirs.config_dir());
let mut pathbuf = base_dirs.config_dir();
pathbuf.push("navi");
pathbuf.push("config.yaml");
if !pathbuf.exists() {
Expand Down Expand Up @@ -132,13 +155,13 @@ impl fetcher::Fetcher for Fetcher {
let folders = paths_from_path_param(&interpolated_paths);

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()));

for folder in folders {
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(),
};
let folder_pathbuf = PathBuf::from(interpolated_folder);
Expand Down Expand Up @@ -228,12 +251,10 @@ mod tests {

#[test]
fn test_default_config_pathbuf() {
let base_dirs = BaseDirs::new()
.ok_or_else(|| anyhow!("bad"))
.expect("could not determine base directories");
let base_dirs = etcetera::choose_base_strategy().expect("could not determine base directories");

let expected = {
let mut e = base_dirs.config_dir().to_path_buf();
let mut e = base_dirs.config_dir();
e.push("navi");
e.push("config.yaml");
e.to_string_lossy().to_string()
Expand All @@ -246,12 +267,10 @@ mod tests {

#[test]
fn test_default_cheat_pathbuf() {
let base_dirs = BaseDirs::new()
.ok_or_else(|| anyhow!("bad"))
.expect("could not determine base directories");
let base_dirs = etcetera::choose_base_strategy().expect("could not determine base directories");

let expected = {
let mut e = base_dirs.data_dir().to_path_buf();
let mut e = base_dirs.data_dir();
e.push("navi");
e.push("cheats");
e.to_string_lossy().to_string()
Expand Down

0 comments on commit 21f93c8

Please sign in to comment.