| @@ -5,30 +5,43 @@ use std::path::PathBuf; |
| 5 | /// On Linux this is handled by `dirs` already. On Windows and macOS, `dirs` |
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 |
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`. |
7 | /// keep their config in `~/.config` by setting `XDG_CONFIG_HOME`. |
| 8 | fn xdg_config_base() -> PathBuf { |
8 | fn sys_config_base() -> PathBuf { |
| 9 | std::env::var("XDG_CONFIG_HOME") |
9 | std::env::var("YOUTUBETUI_CONFIG_HOME") |
| 10 | .map(PathBuf::from) |
10 | .map(PathBuf::from) |
| 11 | .unwrap_or_else(|_| { |
11 | .unwrap_or_else(|_| { |
| 12 | dirs::config_dir().unwrap_or_else(|| home::home_dir().unwrap().join(".config")) |
12 | dirs::config_dir() |
| |
13 | .expect("no config dir, set the YOUTUBETUI_CONFIG_HOME variable to set a directory") |
| 13 | }) |
14 | }) |
| 14 | } |
15 | } |
| 15 | |
16 | |
| 16 | /// Returns the base data directory, respecting XDG_DATA_HOME on all platforms. |
17 | /// Returns the base data directory, respecting XDG_DATA_HOME on all platforms. |
| 17 | fn xdg_data_base() -> PathBuf { |
18 | fn sys_data_base() -> PathBuf { |
| 18 | std::env::var("XDG_DATA_HOME") |
19 | std::env::var("YOUTUBETUI_DATA_HOME") |
| 19 | .map(PathBuf::from) |
20 | .map(PathBuf::from) |
| 20 | .unwrap_or_else(|_| { |
21 | .unwrap_or_else(|_| { |
| 21 | dirs::data_local_dir() |
22 | dirs::data_local_dir() |
| 22 | .unwrap_or_else(|| home::home_dir().unwrap().join(".local").join("share")) |
23 | .expect("no data dir, set the YOUTUBETUI_DATA_HOME variable to set a directory") |
| 23 | }) |
24 | }) |
| 24 | } |
25 | } |
| 25 | |
26 | |
| 26 | /// Returns the base cache directory, respecting XDG_CACHE_HOME on all platforms. |
27 | /// Returns the base cache directory, respecting XDG_CACHE_HOME on all platforms. |
| 27 | fn xdg_cache_base() -> PathBuf { |
28 | fn sys_cache_base() -> PathBuf { |
| 28 | std::env::var("XDG_CACHE_HOME") |
29 | std::env::var("YOUTUBETUI_CACHE_HOME") |
| 29 | .map(PathBuf::from) |
30 | .map(PathBuf::from) |
| 30 | .unwrap_or_else(|_| { |
31 | .unwrap_or_else(|_| { |
| 31 | dirs::cache_dir().unwrap_or_else(|| home::home_dir().unwrap().join(".cache")) |
32 | dirs::cache_dir() |
| |
33 | .expect("no cache dir, set the YOUTUBETUI_CACHE_HOME variable to set a directory") |
| |
34 | }) |
| |
35 | } |
| |
36 | |
| |
37 | /// Returns the base cache directory, respecting XDG_CACHE_HOME on all platforms. |
| |
38 | fn 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 | ) |
| 32 | }) |
45 | }) |
| 33 | } |
46 | } |
| 34 | |
47 | |
| @@ -39,7 +52,7 @@ fn xdg_cache_base() -> PathBuf { |
| 39 | /// - Windows: `%APPDATA%\youtube-tui\` (or `~/.config/youtube-tui/` if XDG_CONFIG_HOME is set) |
52 | /// - Windows: `%APPDATA%\youtube-tui\` (or `~/.config/youtube-tui/` if XDG_CONFIG_HOME is set) |
| 40 | /// - macOS: `~/Library/Application Support/youtube-tui/` |
53 | /// - macOS: `~/Library/Application Support/youtube-tui/` |
| 41 | pub fn config_dir() -> PathBuf { |
54 | pub fn config_dir() -> PathBuf { |
| 42 | xdg_config_base().join("youtube-tui") |
55 | sys_config_base().join("youtube-tui") |
| 43 | } |
56 | } |
| 44 | |
57 | |
| 45 | /// Returns the data directory for youtube-tui. |
58 | /// Returns the data directory for youtube-tui. |
| @@ -49,7 +62,7 @@ pub fn config_dir() -> PathBuf { |
| 49 | /// - Windows: `%LOCALAPPDATA%\youtube-tui\` |
62 | /// - Windows: `%LOCALAPPDATA%\youtube-tui\` |
| 50 | /// - macOS: `~/Library/Application Support/youtube-tui/` |
63 | /// - macOS: `~/Library/Application Support/youtube-tui/` |
| 51 | pub fn data_dir() -> PathBuf { |
64 | pub fn data_dir() -> PathBuf { |
| 52 | xdg_data_base().join("youtube-tui") |
65 | sys_data_base().join("youtube-tui") |
| 53 | } |
66 | } |
| 54 | |
67 | |
| 55 | /// Returns the cache directory for youtube-tui. |
68 | /// Returns the cache directory for youtube-tui. |
| @@ -59,14 +72,14 @@ pub fn data_dir() -> PathBuf { |
| 59 | /// - Windows: `%LOCALAPPDATA%\youtube-tui\cache\` |
72 | /// - Windows: `%LOCALAPPDATA%\youtube-tui\cache\` |
| 60 | /// - macOS: `~/Library/Caches/youtube-tui/` |
73 | /// - macOS: `~/Library/Caches/youtube-tui/` |
| 61 | pub fn cache_dir() -> PathBuf { |
74 | pub fn cache_dir() -> PathBuf { |
| 62 | xdg_cache_base().join("youtube-tui") |
75 | sys_cache_base().join("youtube-tui") |
| 63 | } |
76 | } |
| 64 | |
77 | |
| 65 | /// Returns the storage directory for rustypipe. |
78 | /// Returns the storage directory for rustypipe. |
| 66 | /// |
79 | /// |
| 67 | /// Respects `XDG_DATA_HOME` on all platforms. |
80 | /// Respects `XDG_DATA_HOME` on all platforms. |
| 68 | pub fn rustypipe_dir() -> PathBuf { |
81 | pub fn rustypipe_dir() -> PathBuf { |
| 69 | xdg_data_base().join("rustypipe") |
82 | sys_data_base().join("rustypipe") |
| 70 | } |
83 | } |
| 71 | |
84 | |
| 72 | /// Returns the default save path string for the `save-path` env variable. |
85 | /// Returns the default save path string for the `save-path` env variable. |
| @@ -81,8 +94,7 @@ pub fn default_save_path() -> String { |
| 81 | |
94 | |
| 82 | /// Returns the default download path string for the `download-path` env variable. |
95 | /// Returns the default download path string for the `download-path` env variable. |
| 83 | pub fn default_download_path() -> String { |
96 | pub fn default_download_path() -> String { |
| 84 | let downloads = |
97 | let downloads = sys_download_base(); |
| 85 | dirs::download_dir().unwrap_or_else(|| home::home_dir().unwrap().join("Downloads")); |
| |
| 86 | let mut s = downloads.to_string_lossy().to_string(); |
98 | let mut s = downloads.to_string_lossy().to_string(); |
| 87 | if !s.ends_with(std::path::MAIN_SEPARATOR) { |
99 | if !s.ends_with(std::path::MAIN_SEPARATOR) { |
| 88 | s.push(std::path::MAIN_SEPARATOR); |
100 | s.push(std::path::MAIN_SEPARATOR); |