Commit 07f019f64d
Unsigned
Layout: unified · split
src/global/functions/paths.rs +49 −26
| @@ -1,55 +1,78 @@ | ||
| 1 | 1 | use std::path::PathBuf; |
| 2 | 2 | |
| 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`. | |
| 8 | fn 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. | |
| 17 | fn 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. | |
| 27 | fn 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 | ||
| 3 | 35 | /// Returns the configuration directory for youtube-tui. |
| 4 | 36 | /// |
| 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/` | |
| 8 | 41 | pub 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") | |
| 12 | 43 | } |
| 13 | 44 | |
| 14 | 45 | /// Returns the data directory for youtube-tui. |
| 15 | 46 | /// |
| 16 | /// - Linux: `~/.local/share/youtube-tui/` | |
| 47 | /// Respects `XDG_DATA_HOME` on all platforms. Platform defaults: | |
| 48 | /// - Linux: `~/.local/share/youtube-tui/` | |
| 17 | 49 | /// - Windows: `%LOCALAPPDATA%\youtube-tui\` |
| 18 | /// - macOS: `~/Library/Application Support/youtube-tui/` | |
| 50 | /// - macOS: `~/Library/Application Support/youtube-tui/` | |
| 19 | 51 | pub 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") | |
| 23 | 53 | } |
| 24 | 54 | |
| 25 | 55 | /// Returns the cache directory for youtube-tui. |
| 26 | 56 | /// |
| 27 | /// - Linux: `~/.cache/youtube-tui/` | |
| 57 | /// Respects `XDG_CACHE_HOME` on all platforms. Platform defaults: | |
| 58 | /// - Linux: `~/.cache/youtube-tui/` | |
| 28 | 59 | /// - Windows: `%LOCALAPPDATA%\youtube-tui\cache\` |
| 29 | /// - macOS: `~/Library/Caches/youtube-tui/` | |
| 60 | /// - macOS: `~/Library/Caches/youtube-tui/` | |
| 30 | 61 | pub 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") | |
| 34 | 63 | } |
| 35 | 64 | |
| 36 | 65 | /// Returns the storage directory for rustypipe. |
| 37 | 66 | /// |
| 38 | /// - Linux: `~/.local/share/rustypipe/` | |
| 39 | /// - Windows: `%LOCALAPPDATA%\rustypipe\` | |
| 40 | /// - macOS: `~/Library/Application Support/rustypipe/` | |
| 67 | /// Respects `XDG_DATA_HOME` on all platforms. | |
| 41 | 68 | pub 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") | |
| 45 | 70 | } |
| 46 | 71 | |
| 47 | 72 | /// 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. | |
| 49 | 73 | pub fn default_save_path() -> String { |
| 50 | 74 | let p = data_dir().join("saved"); |
| 51 | 75 | let mut s = p.to_string_lossy().to_string(); |
| 52 | // Ensure trailing path separator for command substitution | |
| 53 | 76 | if !s.ends_with(std::path::MAIN_SEPARATOR) { |
| 54 | 77 | s.push(std::path::MAIN_SEPARATOR); |
| 55 | 78 | } |
| @@ -58,8 +81,8 @@ pub fn default_save_path() -> String { | ||
| 58 | 81 | |
| 59 | 82 | /// Returns the default download path string for the `download-path` env variable. |
| 60 | 83 | pub 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")); | |
| 63 | 86 | let mut s = downloads.to_string_lossy().to_string(); |
| 64 | 87 | if !s.ends_with(std::path::MAIN_SEPARATOR) { |
| 65 | 88 | s.push(std::path::MAIN_SEPARATOR); |