Swadhin Biswas fix: resolve all 25 clippy warnings · 3/4/2026
60ab211
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//! Image caching and background downloading from API
//!
//! Implements smart caching where the previous image is shown while a new one
//! is downloaded in the background for the next run.
use std::fs;
use std::path::PathBuf;
use std::process::Command;
/// Get the cache directory for images
pub fn cache_dir() -> PathBuf {
let cache = dirs::cache_dir().unwrap_or_else(|| PathBuf::from("."));
cache.join("fetchx")
}
/// Get the cached image path
pub fn cached_image_path() -> PathBuf {
cache_dir().join("current_image.png")
}
/// Download a new image in background every run (non-blocking)
/// Spawns a detached child process (fetchx --bg-download) that survives
/// after the main process exits, ensuring the download always completes.
pub fn download_image_background() {
// Find our own executable path
if let Ok(exe) = std::env::current_exe() {
let _ = Command::new(exe)
.arg("--bg-download")
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn(); // Detached child process — survives after parent exits
}
}
/// Download image from API and save to cache directory
pub fn download_image_from_api() -> Result<(), Box<dyn std::error::Error>> {
let cache = cache_dir();
fs::create_dir_all(&cache)?;
// /file endpoint returns raw image bytes directly — no JSON parsing needed
let client = reqwest::blocking::Client::builder()
.timeout(std::time::Duration::from_secs(30))
.build()?;
let response = client
.get("https://api.nekosapi.com/v4/images/random/file")
.send()?;
response.error_for_status_ref()?;
let bytes = response.bytes()?;
if bytes.is_empty() {
return Err("Empty response from API".into());
}
// Decode whatever format API sends (webp/jpeg/png) and save as PNG
let img =
image::load_from_memory(&bytes).map_err(|e| format!("Failed to decode image: {}", e))?;
// Write to temp file first, then rename — so display never sees a half-written file
let cache = cache_dir();
let tmp_path = cache.join("downloading.tmp.png");
img.save_with_format(&tmp_path, image::ImageFormat::Png)
.map_err(|e| format!("Failed to save image: {}", e))?;
let current = cached_image_path();
fs::rename(&tmp_path, ¤t)?;
Ok(())
}
/// Detect and use wallpaper as image (Hyprland, GNOME, KDE, etc.)
#[allow(dead_code)]
pub fn detect_and_use_wallpaper() -> Option<PathBuf> {
// Try Hyprland wallpaper detection
if let Some(wallpaper) = get_hyprland_wallpaper() {
if copy_image_to_cache(&wallpaper).is_ok() {
return Some(cached_image_path());
}
}
// Try GNOME wallpaper
if let Some(wallpaper) = get_gnome_wallpaper() {
if copy_image_to_cache(&wallpaper).is_ok() {
return Some(cached_image_path());
}
}
// Try KDE wallpaper
if let Some(wallpaper) = get_kde_wallpaper() {
if copy_image_to_cache(&wallpaper).is_ok() {
return Some(cached_image_path());
}
}
// Try from environment variable
if let Ok(wallpaper_path) = std::env::var("WALLPAPER") {
let path = PathBuf::from(wallpaper_path);
if path.exists() && copy_image_to_cache(&path).is_ok() {
return Some(cached_image_path());
}
}
None
}
/// Get Hyprland wallpaper (check hyprctl output and config files)
fn get_hyprland_wallpaper() -> Option<PathBuf> {
// Try hyprctl command first
if let Ok(output) = Command::new("hyprctl")
.args(["hyprpaper", "wallpapers"])
.output()
{
if let Ok(stdout) = String::from_utf8(output.stdout) {
for line in stdout.lines() {
let path = line.trim();
let file_path = PathBuf::from(path);
if file_path.exists() && is_image_file(&file_path) {
return Some(file_path);
}
}
}
}
// Try hyprpaper config
let hyprpaper_config = dirs::config_dir().map(|d| d.join("hypr/hyprpaper.conf"))?;
if let Ok(content) = fs::read_to_string(&hyprpaper_config) {
for line in content.lines() {
if line.starts_with("preload") || line.starts_with("wallpaper") {
if let Some(path_str) = line.split('=').nth(1) {
let path_str = path_str.trim().trim_matches('"').trim_matches('\'');
let path = expand_path(path_str);
if path.exists() && is_image_file(&path) {
return Some(path);
}
}
}
}
}
None
}
/// Get GNOME wallpaper from dconf
fn get_gnome_wallpaper() -> Option<PathBuf> {
let output = Command::new("dconf")
.args(["read", "/org/gnome/desktop/background/picture-uri-dark"])
.output()
.ok()?;
let path_str = String::from_utf8(output.stdout).ok()?;
let path_str = path_str
.trim()
.trim_matches('\'')
.trim_start_matches("file://");
let path = PathBuf::from(path_str);
if path.exists() && is_image_file(&path) {
return Some(path);
}
None
}
/// Get KDE wallpaper from config
fn get_kde_wallpaper() -> Option<PathBuf> {
let kde_config = dirs::config_dir().map(|d| d.join("plasmarc"))?;
if let Ok(content) = fs::read_to_string(&kde_config) {
for line in content.lines() {
if line.contains("Image=") || line.contains("File=") {
if let Some(value) = line.split('=').nth(1) {
let path = expand_path(value.trim());
if path.exists() && is_image_file(&path) {
return Some(path);
}
}
}
}
}
None
}
/// Copy wallpaper image to cache directory
fn copy_image_to_cache(source: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {
let cache = cache_dir();
fs::create_dir_all(&cache)?;
let cached = cached_image_path();
fs::copy(source, &cached)?;
Ok(())
}
/// Check if a file is likely an image
fn is_image_file(path: &std::path::Path) -> bool {
let extensions = ["png", "jpg", "jpeg", "webp", "gif", "bmp"];
if let Some(ext) = path
.extension()
.and_then(|e| e.to_str())
.map(|s| s.to_lowercase())
{
extensions.contains(&ext.as_str())
} else {
false
}
}
/// Expand ~ and environment variables in paths
fn expand_path(path: &str) -> PathBuf {
if path.starts_with("~/") {
if let Ok(home) = std::env::var("HOME") {
PathBuf::from(path.replace("~", &home))
} else {
PathBuf::from(path)
}
} else {
PathBuf::from(path)
}
}
pub fn get_available_image() -> Option<PathBuf> {
let cache = cached_image_path();
if cache.exists() {
Some(cache)
} else {
None
}
}
/// Detect terminal capabilities
pub fn detect_terminal_capabilities() -> TerminalCapabilities {
let term = std::env::var("TERM").unwrap_or_default();
let colorterm = std::env::var("COLORTERM").unwrap_or_default();
// Check for kitty
if std::env::var("KITTY_WINDOW_ID").is_ok() {
return TerminalCapabilities {
supports_graphics: true,
backend: "kitty".to_string(),
};
}
// Check for iTerm2
if std::env::var("ITERM_SESSION_ID").is_ok() {
return TerminalCapabilities {
supports_graphics: true,
backend: "iterm2".to_string(),
};
}
// Check for sixel support
if (term.contains("xterm-kitty") || colorterm.contains("truecolor")) && check_sixel_support() {
return TerminalCapabilities {
supports_graphics: true,
backend: "sixel".to_string(),
};
}
// Check for w3m (X11)
if std::env::var("DISPLAY").is_ok() {
return TerminalCapabilities {
supports_graphics: true,
backend: "w3m".to_string(),
};
}
// Fallback: no graphics support, use ASCII
TerminalCapabilities {
supports_graphics: false,
backend: "ascii".to_string(),
}
}
pub struct TerminalCapabilities {
#[allow(dead_code)]
pub supports_graphics: bool,
pub backend: String,
}
/// Check if terminal supports sixel (heuristic)
fn check_sixel_support() -> bool {
std::env::var("TERM")
.map(|t| t.contains("256color") || t.contains("truecolor"))
.unwrap_or(false)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cache_dir_exists() {
let dir = cache_dir();
// cache_dir() returns either <system_cache>/fetchx or ./fetchx
assert!(
dir.to_string_lossy().contains("fetchx"),
"Cache dir should contain 'fetchx': {:?}",
dir
);
}
#[test]
fn test_get_available_image() {
let result = get_available_image();
// Should return None if no cached image, or Some(path) if cached
assert!(result.is_none() || result.is_some());
}
}