gardesk/garshot / f44f052

Browse files

Fix tilde expansion in config save_dir path

Authored by espadonne
SHA
f44f052cbc28bcb5c3e1c83746bedc59beef259d
Parents
6187125
Tree
05ee53e

1 changed file

StatusFile+-
M garshot/src/config/toml_config.rs 48 1
garshot/src/config/toml_config.rsmodified
@@ -121,10 +121,13 @@ pub fn load_config() -> Result<Config> {
121121
             GarshotError::ConfigError(format!("Failed to read config file: {}", e))
122122
         })?;
123123
 
124
-        let config: Config = toml::from_str(&content).map_err(|e| {
124
+        let mut config: Config = toml::from_str(&content).map_err(|e| {
125125
             GarshotError::ConfigError(format!("Failed to parse config file: {}", e))
126126
         })?;
127127
 
128
+        // Expand tilde in save_dir
129
+        config.general.save_dir = expand_tilde(&config.general.save_dir);
130
+
128131
         Ok(config)
129132
     } else {
130133
         tracing::debug!("No config file found, using defaults");
@@ -132,6 +135,20 @@ pub fn load_config() -> Result<Config> {
132135
     }
133136
 }
134137
 
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
+
135152
 /// Get the config file path.
136153
 pub fn config_path() -> PathBuf {
137154
     dirs::config_dir()
@@ -194,4 +211,34 @@ mod tests {
194211
         assert_eq!(config.selection.line_color, "#00ff00");
195212
         assert_eq!(config.naming.pattern, "shot-date");
196213
     }
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
+    }
197244
 }