Skip to content

Commit da696df

Browse files
authored
Replace dirs crate with xdg and home (#68)
`dirs` refuses to support XDG env vars on macos
1 parent 1f9b545 commit da696df

File tree

8 files changed

+63
-32
lines changed

8 files changed

+63
-32
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Changed
6+
7+
- BREAKING: MacOS breaking change only since Tinty now uses `XDG` paths
8+
for config and data directories while falling back to `XDG` defaults
9+
for these dirs if the environment variables aren't set. This is how
10+
Tinty functions on Linux already
11+
312
## [0.20.1] - 2024-09-25
413

514
### Fixed

Cargo.lock

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ categories = ["command-line-utilities"]
1515
anyhow = "1.0.89"
1616
clap = { version = "4.5.4", features = ["derive"] }
1717
clap_complete = { version = "4.5.29" }
18-
dirs = "5.0.1"
1918
hex_color = "3.0.0"
2019
serde = { version = "1.0.210", features = ["derive"] }
2120
serde_yaml = "0.9.31"
@@ -26,3 +25,5 @@ tinted-builder= "0.7.0"
2625
tinted-scheme-extractor = "0.4.0"
2726
toml = "0.8.19"
2827
url = "2.5.2"
28+
xdg = "2.5.2"
29+
home = "0.5.9"

src/config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::constants::REPO_NAME;
22
use anyhow::{anyhow, Context, Result};
3+
use home::home_dir;
34
use serde::de::{self, Deserializer, Unexpected, Visitor};
45
use serde::Deserialize;
56
use std::collections::HashSet;
@@ -10,6 +11,7 @@ use url::Url;
1011

1112
pub const DEFAULT_CONFIG_SHELL: &str = "sh -c '{}'";
1213
pub const CONFIG_FILE_NAME: &str = "config.toml";
14+
pub const ORG_NAME: &str = "tinted-theming";
1315
pub const BASE16_SHELL_REPO_URL: &str = "https://github.com/tinted-theming/tinted-shell";
1416
pub const BASE16_SHELL_REPO_NAME: &str = "tinted-shell";
1517
pub const BASE16_SHELL_THEMES_DIR: &str = "scripts";
@@ -196,7 +198,7 @@ impl Config {
196198
// Replace `~/` with absolute home path
197199
let trimmed_path = item.path.trim();
198200
if trimmed_path.starts_with("~/") {
199-
match dirs::home_dir() {
201+
match home_dir() {
200202
Some(home_dir) => {
201203
item.path = trimmed_path.replacen(
202204
"~/",

src/main.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,38 @@ use crate::cli::{build_cli, get_matches};
2020
use anyhow::{anyhow, Context, Result};
2121
use clap::Command;
2222
use clap_complete::{generate, Generator, Shell};
23-
use config::CONFIG_FILE_NAME;
23+
use config::{CONFIG_FILE_NAME, ORG_NAME};
2424
use constants::{CUSTOM_SCHEMES_DIR_NAME, REPO_DIR, REPO_NAME, SCHEMES_REPO_NAME};
2525
use operations::generate_scheme;
2626
use std::path::PathBuf;
2727
use tinted_builder::{SchemeSystem, SchemeVariant};
2828
use utils::{ensure_directory_exists, replace_tilde_slash_with_home};
29+
use xdg::BaseDirectories;
2930

3031
/// Entry point of the application.
3132
fn main() -> Result<()> {
3233
// Parse the command line arguments
3334
let matches = get_matches();
35+
let xdg_dirs = BaseDirectories::with_prefix(format!("{}/{}", ORG_NAME, REPO_NAME)).unwrap();
3436

3537
// Other configuration paths
3638
let config_path_result: Result<PathBuf> =
3739
if let Some(config_file_path) = matches.get_one::<String>("config") {
3840
replace_tilde_slash_with_home(config_file_path)
3941
} else {
40-
Ok(dirs::config_dir()
41-
.ok_or_else(|| anyhow!("Error getting config directory"))?
42-
.join(format!("tinted-theming/{}/{}", REPO_NAME, CONFIG_FILE_NAME)))
42+
xdg_dirs
43+
.place_config_file(CONFIG_FILE_NAME)
44+
.context(format!(
45+
"Unable to create XDG_HOME/{}/{}/{}",
46+
ORG_NAME, REPO_NAME, CONFIG_FILE_NAME
47+
))
4348
};
4449
let config_path = config_path_result?;
45-
// Determine the data-dir path
46-
let data_path_result: Result<PathBuf> =
47-
if let Some(data_file_path) = matches.get_one::<String>("data-dir") {
48-
replace_tilde_slash_with_home(data_file_path)
49-
} else {
50-
Ok(dirs::data_dir()
51-
.ok_or_else(|| anyhow!("Error getting data directory"))?
52-
.join(format!("tinted-theming/{}", REPO_NAME)))
53-
};
54-
let data_path = data_path_result?;
50+
let data_path: PathBuf = if let Some(data_file_path) = matches.get_one::<String>("data-dir") {
51+
replace_tilde_slash_with_home(data_file_path)?
52+
} else {
53+
xdg_dirs.get_data_home()
54+
};
5555
let data_repo_path = data_path.join(REPO_DIR);
5656

5757
// Ensure config dirs exist

src/utils.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::config::{Config, ConfigItem, SupportedSchemeSystems, DEFAULT_CONFIG_SHELL};
22
use crate::constants::{REPO_NAME, SCHEME_EXTENSION};
33
use anyhow::{anyhow, Context, Result};
4+
use home::home_dir;
45
use std::fs::{self, File};
56
use std::io::Write;
67
use std::path::{Path, PathBuf};
@@ -171,14 +172,14 @@ pub fn get_all_scheme_names(
171172
pub fn replace_tilde_slash_with_home(path_str: &str) -> Result<PathBuf> {
172173
let trimmed_path_str = path_str.trim();
173174
if trimmed_path_str.starts_with("~/") {
174-
match dirs::home_dir() {
175-
Some(home_dir) => Ok(PathBuf::from(trimmed_path_str.replacen(
176-
"~/",
177-
format!("{}/", home_dir.display()).as_str(),
178-
1,
179-
))),
180-
None => Err(anyhow!("Unable to determine a home directory for \"{}\", please use an absolute path instead", trimmed_path_str))
181-
}
175+
match home_dir() {
176+
Some(home_dir) => Ok(PathBuf::from(trimmed_path_str.replacen(
177+
"~/",
178+
format!("{}/", home_dir.display()).as_str(),
179+
1,
180+
))),
181+
None => Err(anyhow!("Unable to determine a home directory for \"{}\", please use an absolute path instead", trimmed_path_str))
182+
}
182183
} else {
183184
Ok(PathBuf::from(trimmed_path_str))
184185
}

tests/cli_general_tests.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ mod utils;
22

33
use crate::utils::{
44
cleanup, read_file_to_string, setup, write_to_file, COMMAND_NAME, CURRENT_SCHEME_FILE_NAME,
5-
REPO_NAME,
5+
ORG_NAME, REPO_NAME,
66
};
7-
use anyhow::{anyhow, Result};
7+
use anyhow::Result;
88
use std::{fs, path::PathBuf};
99

1010
#[test]
@@ -36,7 +36,7 @@ fn test_cli_config_path_tilde_as_home() -> Result<()> {
3636
// -------
3737
let name = "test_cli_config_path_tilde_as_home";
3838
let config_path_name = format!("config_path_{}", name);
39-
let home_path = dirs::home_dir().unwrap();
39+
let home_path = home::home_dir().unwrap();
4040
let config_path = home_path.join(&config_path_name);
4141
let provided_config_path = format!("~/{}", config_path_name);
4242
let data_path = PathBuf::from(format!("data_path_{}", name));
@@ -87,9 +87,9 @@ fn test_cli_default_data_path() -> Result<()> {
8787
let config_path = PathBuf::from(format!("{}.toml", "test_cli_default_data_path"));
8888
let scheme_name = "base16-uwunicorn";
8989
let init_scheme_name = "base16-mocha";
90-
let data_path = dirs::data_dir()
91-
.ok_or_else(|| anyhow!("Error getting data directory"))?
92-
.join(format!("tinted-theming/{}", REPO_NAME));
90+
let xdg_dirs =
91+
xdg::BaseDirectories::with_prefix(format!("{}/{}", ORG_NAME, REPO_NAME)).unwrap();
92+
let data_path = xdg_dirs.get_data_home();
9393
let init_command = format!(
9494
"{} --config=\"{}\" init",
9595
COMMAND_NAME,
@@ -159,7 +159,7 @@ fn test_cli_data_path_tilde_as_home() -> Result<()> {
159159
// Arrange
160160
// -------
161161
let data_path_name = "test_cli_data_path_tilde_as_home";
162-
let home_path = dirs::home_dir().unwrap();
162+
let home_path = home::home_dir().unwrap();
163163
let config_path = PathBuf::from(format!("{}.toml", data_path_name));
164164
let data_path = home_path.join(data_path_name);
165165
let provided_data_path = format!("~/{}", data_path_name);

tests/utils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use std::str;
88

99
#[allow(dead_code)]
1010
pub const REPO_NAME: &str = env!("CARGO_PKG_NAME");
11+
#[allow(dead_code)]
12+
pub const ORG_NAME: &str = "tinted-theming";
1113
pub const COMMAND_NAME: &str = "./target/release/tinty";
1214
#[allow(dead_code)]
1315
pub const CURRENT_SCHEME_FILE_NAME: &str = "current_scheme";

0 commit comments

Comments
 (0)