Commit 07f019f64d
Unsigned
Layout: unified · split
src/global/functions/paths.rs +49 −26
| @@ -1,55 +1,78 @@ | |||
| 1 | use std::path::PathBuf; | 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 | /// Returns the configuration directory for youtube-tui. | 35 | /// Returns the configuration directory for youtube-tui. |
| 4 | /// | 36 | /// |
| 5 | /// - Linux: `~/.config/youtube-tui/` | 37 | /// Respects `XDG_CONFIG_HOME` on all platforms. Platform defaults: |
| 6 | /// - Windows: `%APPDATA%\youtube-tui\` | 38 | /// - Linux: `~/.config/youtube-tui/` |
| 7 | /// - macOS: `~/Library/Application Support/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 | pub fn config_dir() -> PathBuf { | 41 | pub fn config_dir() -> PathBuf { |
| 9 | dirs::config_dir() | 42 | xdg_config_base().join("youtube-tui") |
| 10 | .unwrap_or_else(|| home::home_dir().unwrap().join(".config")) | ||
| 11 | .join("youtube-tui") | ||
| 12 | } | 43 | } |
| 13 | 44 | ||
| 14 | /// Returns the data directory for youtube-tui. | 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 | /// - Windows: `%LOCALAPPDATA%\youtube-tui\` | 49 | /// - Windows: `%LOCALAPPDATA%\youtube-tui\` |
| 18 | /// - macOS: `~/Library/Application Support/youtube-tui/` | 50 | /// - macOS: `~/Library/Application Support/youtube-tui/` |
| 19 | pub fn data_dir() -> PathBuf { | 51 | pub fn data_dir() -> PathBuf { |
| 20 | dirs::data_local_dir() | 52 | xdg_data_base().join("youtube-tui") |
| 21 | .unwrap_or_else(|| home::home_dir().unwrap().join(".local").join("share")) | ||
| 22 | .join("youtube-tui") | ||
| 23 | } | 53 | } |
| 24 | 54 | ||
| 25 | /// Returns the cache directory for youtube-tui. | 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 | /// - Windows: `%LOCALAPPDATA%\youtube-tui\cache\` | 59 | /// - Windows: `%LOCALAPPDATA%\youtube-tui\cache\` |
| 29 | /// - macOS: `~/Library/Caches/youtube-tui/` | 60 | /// - macOS: `~/Library/Caches/youtube-tui/` |
| 30 | pub fn cache_dir() -> PathBuf { | 61 | pub fn cache_dir() -> PathBuf { |
| 31 | dirs::cache_dir() | 62 | xdg_cache_base().join("youtube-tui") |
| 32 | .unwrap_or_else(|| home::home_dir().unwrap().join(".cache")) | ||
| 33 | .join("youtube-tui") | ||
| 34 | } | 63 | } |
| 35 | 64 | ||
| 36 | /// Returns the storage directory for rustypipe. | 65 | /// Returns the storage directory for rustypipe. |
| 37 | /// | 66 | /// |
| 38 | /// - Linux: `~/.local/share/rustypipe/` | 67 | /// Respects `XDG_DATA_HOME` on all platforms. |
| 39 | /// - Windows: `%LOCALAPPDATA%\rustypipe\` | ||
| 40 | /// - macOS: `~/Library/Application Support/rustypipe/` | ||
| 41 | pub fn rustypipe_dir() -> PathBuf { | 68 | pub fn rustypipe_dir() -> PathBuf { |
| 42 | dirs::data_local_dir() | 69 | xdg_data_base().join("rustypipe") |
| 43 | .unwrap_or_else(|| home::home_dir().unwrap().join(".local").join("share")) | ||
| 44 | .join("rustypipe") | ||
| 45 | } | 70 | } |
| 46 | 71 | ||
| 47 | /// Returns the default save path string for the `save-path` env variable. | 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 | pub fn default_save_path() -> String { | 73 | pub fn default_save_path() -> String { |
| 50 | let p = data_dir().join("saved"); | 74 | let p = data_dir().join("saved"); |
| 51 | let mut s = p.to_string_lossy().to_string(); | 75 | let mut s = p.to_string_lossy().to_string(); |
| 52 | // Ensure trailing path separator for command substitution | ||
| 53 | if !s.ends_with(std::path::MAIN_SEPARATOR) { | 76 | if !s.ends_with(std::path::MAIN_SEPARATOR) { |
| 54 | s.push(std::path::MAIN_SEPARATOR); | 77 | s.push(std::path::MAIN_SEPARATOR); |
| 55 | } | 78 | } |
| @@ -58,8 +81,8 @@ pub fn default_save_path() -> String { | |||
| 58 | 81 | ||
| 59 | /// Returns the default download path string for the `download-path` env variable. | 82 | /// Returns the default download path string for the `download-path` env variable. |
| 60 | pub fn default_download_path() -> String { | 83 | pub fn default_download_path() -> String { |
| 61 | let downloads = dirs::download_dir() | 84 | let downloads = |
| 62 | .unwrap_or_else(|| home::home_dir().unwrap().join("Downloads")); | 85 | dirs::download_dir().unwrap_or_else(|| home::home_dir().unwrap().join("Downloads")); |
| 63 | let mut s = downloads.to_string_lossy().to_string(); | 86 | let mut s = downloads.to_string_lossy().to_string(); |
| 64 | if !s.ends_with(std::path::MAIN_SEPARATOR) { | 87 | if !s.ends_with(std::path::MAIN_SEPARATOR) { |
| 65 | s.push(std::path::MAIN_SEPARATOR); | 88 | s.push(std::path::MAIN_SEPARATOR); |