feat(cache): implement download_image_background with detached process
Swadhin Biswas committed 7 months ago
c6471f0
1 parent acb6611
commit c6471f0
1 file changed
+50 -0
Showing 1 changed file+50-0
▼src/image_cache.rs
+50-0
| @@ -198,3 +198,53 @@ fn copy_image_to_cache(source: &PathBuf) -> Result<(), Box<dyn std::error::Error | ||
198 | 198 | |
199 | 199 | Ok(()) |
200 | 200 | } |
201 | + | |
202 | +/// Check if a file is likely an image | |
203 | +fn is_image_file(path: &PathBuf) -> bool { | |
204 | + let extensions = ["png", "jpg", "jpeg", "webp", "gif", "bmp"]; | |
205 | + if let Some(ext) = path | |
206 | + .extension() | |
207 | + .and_then(|e| e.to_str()) | |
208 | + .map(|s| s.to_lowercase()) | |
209 | + { | |
210 | + extensions.contains(&ext.as_str()) | |
211 | + } else { | |
212 | + false | |
213 | + } | |
214 | +} | |
215 | + | |
216 | +/// Expand ~ and environment variables in paths | |
217 | +fn expand_path(path: &str) -> PathBuf { | |
218 | + if path.starts_with("~/") { | |
219 | + if let Ok(home) = std::env::var("HOME") { | |
220 | + PathBuf::from(path.replace("~", &home)) | |
221 | + } else { | |
222 | + PathBuf::from(path) | |
223 | + } | |
224 | + } else { | |
225 | + PathBuf::from(path) | |
226 | + } | |
227 | +} | |
228 | + | |
229 | + | |
230 | +pub fn get_available_image() -> Option<PathBuf> { | |
231 | + let cache = cached_image_path(); | |
232 | + if cache.exists() { | |
233 | + Some(cache) | |
234 | + } else { | |
235 | + None | |
236 | + } | |
237 | +} | |
238 | + | |
239 | +/// Detect terminal capabilities | |
240 | +pub fn detect_terminal_capabilities() -> TerminalCapabilities { | |
241 | + let term = std::env::var("TERM").unwrap_or_default(); | |
242 | + let colorterm = std::env::var("COLORTERM").unwrap_or_default(); | |
243 | + | |
244 | + // Check for kitty | |
245 | + if std::env::var("KITTY_WINDOW_ID").is_ok() { | |
246 | + return TerminalCapabilities { | |
247 | + supports_graphics: true, | |
248 | + backend: "kitty".to_string(), | |
249 | + }; | |
250 | + } | |
201 | 251 | |