Commit 5729b10e95

5729b10e95cb3b652a46a801b4b75401eeac0a16

parent: f1f21a7889

Unsigned

Azin Sharaf <azin.sharaf@icloud.com> · 2026-05-05 03:13 UTC

fix(paths): env vars now point directly to the app dir, not the parent

Layout: unified · split

src/global/functions/paths.rs +48 −54
@@ -1,85 +1,72 @@
11use std::path::PathBuf;
22
3/// Returns the base config directory, respecting XDG_CONFIG_HOME on all platforms.
3/// Returns the configuration directory for youtube-tui.
44///
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 sys_config_base() -> PathBuf {
5/// If `YOUTUBETUI_CONFIG_HOME` is set, it is used directly as the config directory.
6/// Otherwise falls back to the platform default with a `youtube-tui` subdirectory:
7/// - Linux: `~/.config/youtube-tui/`
8/// - Windows: `%APPDATA%\youtube-tui\`
9/// - macOS: `~/Library/Application Support/youtube-tui/`
10pub fn config_dir() -> PathBuf {
911 std::env::var("YOUTUBETUI_CONFIG_HOME")
1012 .map(PathBuf::from)
1113 .unwrap_or_else(|_| {
1214 dirs::config_dir()
13 .expect("no config dir, set the YOUTUBETUI_CONFIG_HOME variable to set a directory")
14 })
15}
16
17/// Returns the base data directory, respecting XDG_DATA_HOME on all platforms.
18fn sys_data_base() -> PathBuf {
19 std::env::var("YOUTUBETUI_DATA_HOME")
20 .map(PathBuf::from)
21 .unwrap_or_else(|_| {
22 dirs::data_local_dir()
23 .expect("no data dir, set the YOUTUBETUI_DATA_HOME variable to set a directory")
24 })
25}
26
27/// Returns the base cache directory, respecting XDG_CACHE_HOME on all platforms.
28fn sys_cache_base() -> PathBuf {
29 std::env::var("YOUTUBETUI_CACHE_HOME")
30 .map(PathBuf::from)
31 .unwrap_or_else(|_| {
32 dirs::cache_dir()
33 .expect("no cache dir, set the YOUTUBETUI_CACHE_HOME variable to set a directory")
15 .expect("no config dir, set YOUTUBETUI_CONFIG_HOME to override")
16 .join("youtube-tui")
3417 })
3518}
3619
37/// Returns the base cache directory, respecting XDG_CACHE_HOME on all platforms.
38fn sys_download_base() -> PathBuf {
39 std::env::var("YOUTUBETUI_DOWNLOAD_HOME")
40 .map(PathBuf::from)
41 .unwrap_or_else(|_| {
42 dirs::cache_dir().expect(
43 "no cache dir, set the YOUTUBETUI_DOWNLOAD_HOME variable to set a directory",
44 )
45 })
46}
47
48/// Returns the configuration directory for youtube-tui.
49///
50/// Respects `XDG_CONFIG_HOME` on all platforms. Platform defaults:
51/// - Linux: `~/.config/youtube-tui/`
52/// - Windows: `%APPDATA%\youtube-tui\` (or `~/.config/youtube-tui/` if XDG_CONFIG_HOME is set)
53/// - macOS: `~/Library/Application Support/youtube-tui/`
54pub fn config_dir() -> PathBuf {
55 sys_config_base().join("youtube-tui")
56}
57
5820/// Returns the data directory for youtube-tui.
5921///
60/// Respects `XDG_DATA_HOME` on all platforms. Platform defaults:
22/// If `YOUTUBETUI_DATA_HOME` is set, it is used directly as the data directory.
23/// Otherwise falls back to the platform default with a `youtube-tui` subdirectory:
6124/// - Linux: `~/.local/share/youtube-tui/`
6225/// - Windows: `%LOCALAPPDATA%\youtube-tui\`
6326/// - macOS: `~/Library/Application Support/youtube-tui/`
6427pub fn data_dir() -> PathBuf {
65 sys_data_base().join("youtube-tui")
28 std::env::var("YOUTUBETUI_DATA_HOME")
29 .map(PathBuf::from)
30 .unwrap_or_else(|_| {
31 dirs::data_local_dir()
32 .expect("no data dir, set YOUTUBETUI_DATA_HOME to override")
33 .join("youtube-tui")
34 })
6635}
6736
6837/// Returns the cache directory for youtube-tui.
6938///
70/// Respects `XDG_CACHE_HOME` on all platforms. Platform defaults:
39/// If `YOUTUBETUI_CACHE_HOME` is set, it is used directly as the cache directory.
40/// Otherwise falls back to the platform default with a `youtube-tui` subdirectory:
7141/// - Linux: `~/.cache/youtube-tui/`
7242/// - Windows: `%LOCALAPPDATA%\youtube-tui\cache\`
7343/// - macOS: `~/Library/Caches/youtube-tui/`
7444pub fn cache_dir() -> PathBuf {
75 sys_cache_base().join("youtube-tui")
45 std::env::var("YOUTUBETUI_CACHE_HOME")
46 .map(PathBuf::from)
47 .unwrap_or_else(|_| {
48 dirs::cache_dir()
49 .expect("no cache dir, set YOUTUBETUI_CACHE_HOME to override")
50 .join("youtube-tui")
51 })
7652}
7753
7854/// Returns the storage directory for rustypipe.
7955///
80/// Respects `XDG_DATA_HOME` on all platforms.
56/// Uses the parent of `YOUTUBETUI_DATA_HOME` (sibling to the youtube-tui data dir) if set,
57/// otherwise falls back to the platform data dir with a `rustypipe` subdirectory.
8158pub fn rustypipe_dir() -> PathBuf {
82 sys_data_base().join("rustypipe")
59 std::env::var("YOUTUBETUI_DATA_HOME")
60 .map(|p| {
61 let base = PathBuf::from(p);
62 let parent = base.parent().map(|p| p.to_path_buf()).unwrap_or(base);
63 parent.join("rustypipe")
64 })
65 .unwrap_or_else(|_| {
66 dirs::data_local_dir()
67 .expect("no data dir, set YOUTUBETUI_DATA_HOME to override")
68 .join("rustypipe")
69 })
8370}
8471
8572/// Returns the default save path string for the `save-path` env variable.
@@ -93,8 +80,15 @@ pub fn default_save_path() -> String {
9380}
9481
9582/// Returns the default download path string for the `download-path` env variable.
83///
84/// If `YOUTUBETUI_DOWNLOAD_HOME` is set, it is used directly as the download directory.
85/// Otherwise falls back to the platform downloads directory.
9686pub fn default_download_path() -> String {
97 let downloads = sys_download_base();
87 let downloads = std::env::var("YOUTUBETUI_DOWNLOAD_HOME")
88 .map(PathBuf::from)
89 .unwrap_or_else(|_| {
90 dirs::download_dir().expect("no download dir, set YOUTUBETUI_DOWNLOAD_HOME to override")
91 });
9892 let mut s = downloads.to_string_lossy().to_string();
9993 if !s.ends_with(std::path::MAIN_SEPARATOR) {
10094 s.push(std::path::MAIN_SEPARATOR);