@@ -121,10 +121,13 @@ pub fn load_config() -> Result<Config> { |
| 121 | 121 | GarshotError::ConfigError(format!("Failed to read config file: {}", e)) |
| 122 | 122 | })?; |
| 123 | 123 | |
| 124 | | - let config: Config = toml::from_str(&content).map_err(|e| { |
| 124 | + let mut config: Config = toml::from_str(&content).map_err(|e| { |
| 125 | 125 | GarshotError::ConfigError(format!("Failed to parse config file: {}", e)) |
| 126 | 126 | })?; |
| 127 | 127 | |
| 128 | + // Expand tilde in save_dir |
| 129 | + config.general.save_dir = expand_tilde(&config.general.save_dir); |
| 130 | + |
| 128 | 131 | Ok(config) |
| 129 | 132 | } else { |
| 130 | 133 | tracing::debug!("No config file found, using defaults"); |
@@ -132,6 +135,20 @@ pub fn load_config() -> Result<Config> { |
| 132 | 135 | } |
| 133 | 136 | } |
| 134 | 137 | |
| 138 | +/// Expand leading `~` in a path to the user's home directory. |
| 139 | +fn expand_tilde(path: &PathBuf) -> PathBuf { |
| 140 | + let path_str = path.to_string_lossy(); |
| 141 | + if path_str == "~" { |
| 142 | + dirs::home_dir().unwrap_or_else(|| PathBuf::from(".")) |
| 143 | + } else if let Some(rest) = path_str.strip_prefix("~/") { |
| 144 | + dirs::home_dir() |
| 145 | + .unwrap_or_else(|| PathBuf::from(".")) |
| 146 | + .join(rest) |
| 147 | + } else { |
| 148 | + path.clone() |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 135 | 152 | /// Get the config file path. |
| 136 | 153 | pub fn config_path() -> PathBuf { |
| 137 | 154 | dirs::config_dir() |
@@ -194,4 +211,34 @@ mod tests { |
| 194 | 211 | assert_eq!(config.selection.line_color, "#00ff00"); |
| 195 | 212 | assert_eq!(config.naming.pattern, "shot-date"); |
| 196 | 213 | } |
| 214 | + |
| 215 | + #[test] |
| 216 | + fn test_expand_tilde() { |
| 217 | + let home = dirs::home_dir().unwrap(); |
| 218 | + |
| 219 | + // ~/foo/bar should expand |
| 220 | + let path = PathBuf::from("~/Pictures/Screenshots"); |
| 221 | + let expanded = expand_tilde(&path); |
| 222 | + assert_eq!(expanded, home.join("Pictures/Screenshots")); |
| 223 | + |
| 224 | + // Just ~ should expand to home |
| 225 | + let path = PathBuf::from("~"); |
| 226 | + let expanded = expand_tilde(&path); |
| 227 | + assert_eq!(expanded, home); |
| 228 | + |
| 229 | + // Absolute paths should not change |
| 230 | + let path = PathBuf::from("/tmp/screenshots"); |
| 231 | + let expanded = expand_tilde(&path); |
| 232 | + assert_eq!(expanded, PathBuf::from("/tmp/screenshots")); |
| 233 | + |
| 234 | + // Relative paths should not change |
| 235 | + let path = PathBuf::from("screenshots"); |
| 236 | + let expanded = expand_tilde(&path); |
| 237 | + assert_eq!(expanded, PathBuf::from("screenshots")); |
| 238 | + |
| 239 | + // Tilde in middle should not expand |
| 240 | + let path = PathBuf::from("/foo/~/bar"); |
| 241 | + let expanded = expand_tilde(&path); |
| 242 | + assert_eq!(expanded, PathBuf::from("/foo/~/bar")); |
| 243 | + } |
| 197 | 244 | } |