feat(image): implement w3m image backend

Swadhin Biswas committed 7 months ago
73b92b0
1 parent 982fffd
commit 73b92b0
1 file changed
+60 -0
Showing 1 changed file+60-0
▼src/image_backend/mod.rs
+60-0
258
258
image_path,
259
259
])
260
260
.output()
261
+ .map_err(|e| format!("Failed to run chafa: {}", e))?;
262
+
263
+ if !output.status.success() {
264
+ return Err(format!(
265
+ "chafa failed: {}",
266
+ String::from_utf8_lossy(&output.stderr)
267
+ ));
268
+ }
269
+
270
+ let text = String::from_utf8_lossy(&output.stdout);
271
+ Ok(text.lines().map(|l| l.to_string()).collect())
272
+}
273
+
274
+/// Display an image using sixel (via img2sixel).
275
+pub fn display_sixel(
276
+ image_path: &str,
277
+ max_cols: usize,
278
+ _max_rows: usize,
279
+) -> Result<(), String> {
280
+ let path = Path::new(image_path);
281
+ if !path.exists() {
282
+ return Err(format!("Image not found: {}", image_path));
283
+ }
284
+
285
+ // Calculate pixel width (approx 8px per col)
286
+ let px_width = max_cols * 8;
287
+
288
+ let output = Command::new("img2sixel")
289
+ .args(["-w", &px_width.to_string(), image_path])
290
+ .output()
291
+ .map_err(|e| format!("Failed to run img2sixel: {}", e))?;
292
+
293
+ if !output.status.success() {
294
+ return Err(format!(
295
+ "img2sixel failed: {}",
296
+ String::from_utf8_lossy(&output.stderr)
297
+ ));
298
+ }
299
+
300
+ let stdout = io::stdout();
301
+ let mut out = stdout.lock();
302
+ out.write_all(&output.stdout).ok();
303
+ out.flush().ok();
304
+
305
+ Ok(())
306
+}
307
+
308
+/// Render an image with the specified backend. Returns lines for side-by-side layout.
309
+/// For kitty/sixel, the image is printed directly and empty placeholder lines are returned.
310
+#[allow(dead_code)]
311
+pub fn render_image(
312
+ backend: &Backend,
313
+ image_path: &str,
314
+ max_cols: usize,
315
+ max_rows: usize,
316
+) -> ImageResult {
317
+ match backend {
318
+ Backend::Kitty => match display_kitty(image_path, max_cols, max_rows) {
319
+ Ok((w, h)) => ImageResult::InlineRendered {
320
+ height_rows: h,
261
321