Commit 07f019f64d

07f019f64ddcc7f55c279b7e9717f2863de7aadd

parent: 3d8737b9f5

Unsigned

Azin Sharaf <azin.sharaf@icloud.com> · 2026-05-04 00:07 UTC

feat(paths): respect XDG_CONFIG_HOME, XDG_DATA_HOME, XDG_CACHE_HOME on all platforms

The dirs crate honours XDG env vars on Linux but ignores them on
Windows and macOS. This change checks the XDG vars manually before
falling back to dirs, so users on any platform can override the
default locations by setting the standard XDG environment variables.

Practical use on Windows: set XDG_CONFIG_HOME=~\.config to keep
config files in the same location as a Linux install.

Layout: unified · split

src/global/functions/paths.rs +49 −26
@@ -1,55 +1,78 @@
11use std::path::PathBuf;
22
3/// Returns the base config directory, respecting XDG_CONFIG_HOME on all platforms.
4///
5/// On Linux this is handled by `dirs` already. On Windows and macOS, `dirs`
6/// ignores XDG env vars, so we check manually first. This lets Windows users
7/// keep their config in `~/.config` by setting `XDG_CONFIG_HOME`.
8fn xdg_config_base() -> PathBuf {
9 std::env::var("XDG_CONFIG_HOME")
10 .map(PathBuf::from)
11 .unwrap_or_else(|_| {
12 dirs::config_dir().unwrap_or_else(|| home::home_dir().unwrap().join(".config"))
13 })
14}
15
16/// Returns the base data directory, respecting XDG_DATA_HOME on all platforms.
17fn xdg_data_base() -> PathBuf {
18 std::env::var("XDG_DATA_HOME")
19 .map(PathBuf::from)
20 .unwrap_or_else(|_| {
21 dirs::data_local_dir()
22 .unwrap_or_else(|| home::home_dir().unwrap().join(".local").join("share"))
23 })
24}
25
26/// Returns the base cache directory, respecting XDG_CACHE_HOME on all platforms.
27fn xdg_cache_base() -> PathBuf {
28 std::env::var("XDG_CACHE_HOME")
29 .map(PathBuf::from)
30 .unwrap_or_else(|_| {
31 dirs::cache_dir().unwrap_or_else(|| home::home_dir().unwrap().join(".cache"))
32 })
33}
34
335/// Returns the configuration directory for youtube-tui.
436///
5/// - Linux: `~/.config/youtube-tui/`
6/// - Windows: `%APPDATA%\youtube-tui\`
7/// - macOS: `~/Library/Application Support/youtube-tui/`
37/// Respects `XDG_CONFIG_HOME` on all platforms. Platform defaults:
38/// - Linux: `~/.config/youtube-tui/`
39/// - Windows: `%APPDATA%\youtube-tui\` (or `~/.config/youtube-tui/` if XDG_CONFIG_HOME is set)
40/// - macOS: `~/Library/Application Support/youtube-tui/`
841pub fn config_dir() -> PathBuf {
9 dirs::config_dir()
10 .unwrap_or_else(|| home::home_dir().unwrap().join(".config"))
11 .join("youtube-tui")
42 xdg_config_base().join("youtube-tui")
1243}
1344
1445/// Returns the data directory for youtube-tui.
1546///
16/// - Linux: `~/.local/share/youtube-tui/`
47/// Respects `XDG_DATA_HOME` on all platforms. Platform defaults:
48/// - Linux: `~/.local/share/youtube-tui/`
1749/// - Windows: `%LOCALAPPDATA%\youtube-tui\`
18/// - macOS: `~/Library/Application Support/youtube-tui/`
50/// - macOS: `~/Library/Application Support/youtube-tui/`
1951pub fn data_dir() -> PathBuf {
20 dirs::data_local_dir()
21 .unwrap_or_else(|| home::home_dir().unwrap().join(".local").join("share"))
22 .join("youtube-tui")
52 xdg_data_base().join("youtube-tui")
2353}
2454
2555/// Returns the cache directory for youtube-tui.
2656///
27/// - Linux: `~/.cache/youtube-tui/`
57/// Respects `XDG_CACHE_HOME` on all platforms. Platform defaults:
58/// - Linux: `~/.cache/youtube-tui/`
2859/// - Windows: `%LOCALAPPDATA%\youtube-tui\cache\`
29/// - macOS: `~/Library/Caches/youtube-tui/`
60/// - macOS: `~/Library/Caches/youtube-tui/`
3061pub fn cache_dir() -> PathBuf {
31 dirs::cache_dir()
32 .unwrap_or_else(|| home::home_dir().unwrap().join(".cache"))
33 .join("youtube-tui")
62 xdg_cache_base().join("youtube-tui")
3463}
3564
3665/// Returns the storage directory for rustypipe.
3766///
38/// - Linux: `~/.local/share/rustypipe/`
39/// - Windows: `%LOCALAPPDATA%\rustypipe\`
40/// - macOS: `~/Library/Application Support/rustypipe/`
67/// Respects `XDG_DATA_HOME` on all platforms.
4168pub fn rustypipe_dir() -> PathBuf {
42 dirs::data_local_dir()
43 .unwrap_or_else(|| home::home_dir().unwrap().join(".local").join("share"))
44 .join("rustypipe")
69 xdg_data_base().join("rustypipe")
4570}
4671
4772/// Returns the default save path string for the `save-path` env variable.
48/// This is used in command strings, so it returns a String with trailing separator.
4973pub fn default_save_path() -> String {
5074 let p = data_dir().join("saved");
5175 let mut s = p.to_string_lossy().to_string();
52 // Ensure trailing path separator for command substitution
5376 if !s.ends_with(std::path::MAIN_SEPARATOR) {
5477 s.push(std::path::MAIN_SEPARATOR);
5578 }
@@ -58,8 +81,8 @@ pub fn default_save_path() -> String {
5881
5982/// Returns the default download path string for the `download-path` env variable.
6083pub fn default_download_path() -> String {
61 let downloads = dirs::download_dir()
62 .unwrap_or_else(|| home::home_dir().unwrap().join("Downloads"));
84 let downloads =
85 dirs::download_dir().unwrap_or_else(|| home::home_dir().unwrap().join("Downloads"));
6386 let mut s = downloads.to_string_lossy().to_string();
6487 if !s.ends_with(std::path::MAIN_SEPARATOR) {
6588 s.push(std::path::MAIN_SEPARATOR);