Commit 5729b10e95
Unsigned
Layout: unified · split
src/global/functions/paths.rs +48 −54
| @@ -1,85 +1,72 @@ | ||
| 1 | 1 | use std::path::PathBuf; |
| 2 | 2 | |
| 3 | /// Returns the base config directory, respecting XDG_CONFIG_HOME on all platforms. | |
| 3 | /// Returns the configuration directory for youtube-tui. | |
| 4 | 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`. | |
| 8 | fn 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/` | |
| 10 | pub fn config_dir() -> PathBuf { | |
| 9 | 11 | std::env::var("YOUTUBETUI_CONFIG_HOME") |
| 10 | 12 | .map(PathBuf::from) |
| 11 | 13 | .unwrap_or_else(|_| { |
| 12 | 14 | 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. | |
| 18 | fn 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. | |
| 28 | fn 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") | |
| 34 | 17 | }) |
| 35 | 18 | } |
| 36 | 19 | |
| 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 | ) | |
| 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/` | |
| 54 | pub fn config_dir() -> PathBuf { | |
| 55 | sys_config_base().join("youtube-tui") | |
| 56 | } | |
| 57 | ||
| 58 | 20 | /// Returns the data directory for youtube-tui. |
| 59 | 21 | /// |
| 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: | |
| 61 | 24 | /// - Linux: `~/.local/share/youtube-tui/` |
| 62 | 25 | /// - Windows: `%LOCALAPPDATA%\youtube-tui\` |
| 63 | 26 | /// - macOS: `~/Library/Application Support/youtube-tui/` |
| 64 | 27 | pub 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 | }) | |
| 66 | 35 | } |
| 67 | 36 | |
| 68 | 37 | /// Returns the cache directory for youtube-tui. |
| 69 | 38 | /// |
| 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: | |
| 71 | 41 | /// - Linux: `~/.cache/youtube-tui/` |
| 72 | 42 | /// - Windows: `%LOCALAPPDATA%\youtube-tui\cache\` |
| 73 | 43 | /// - macOS: `~/Library/Caches/youtube-tui/` |
| 74 | 44 | pub 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 | }) | |
| 76 | 52 | } |
| 77 | 53 | |
| 78 | 54 | /// Returns the storage directory for rustypipe. |
| 79 | 55 | /// |
| 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. | |
| 81 | 58 | pub 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 | }) | |
| 83 | 70 | } |
| 84 | 71 | |
| 85 | 72 | /// Returns the default save path string for the `save-path` env variable. |
| @@ -93,8 +80,15 @@ pub fn default_save_path() -> String { | ||
| 93 | 80 | } |
| 94 | 81 | |
| 95 | 82 | /// 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. | |
| 96 | 86 | pub 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 | }); | |
| 98 | 92 | let mut s = downloads.to_string_lossy().to_string(); |
| 99 | 93 | if !s.ends_with(std::path::MAIN_SEPARATOR) { |
| 100 | 94 | s.push(std::path::MAIN_SEPARATOR); |