feat: create image_backend module with Backend enum
Swadhin Biswas committed 7 months ago
790e1e8
1 parent 24a4879
commit 790e1e8
1 file changed
+50 -0
Showing 1 changed file+50-0
▼src/image_backend/mod.rs
+50-0
| @@ -0,0 +1,50 @@ | ||
1 | +//! Image backend module for FetchX | |
2 | +//! | |
3 | +//! Supports: kitty graphics protocol, sixel (via external tool), chafa, and "off". | |
4 | + | |
5 | +use base64::Engine; | |
6 | +use std::io::{self, Write}; | |
7 | +use std::path::Path; | |
8 | +use std::process::Command; | |
9 | + | |
10 | +/// Image backend types | |
11 | +#[derive(Debug, Clone, PartialEq)] | |
12 | +pub enum Backend { | |
13 | + Ascii, | |
14 | + Kitty, | |
15 | + Sixel, | |
16 | + Chafa, | |
17 | + W3m, | |
18 | + Iterm2, | |
19 | + Off, | |
20 | +} | |
21 | + | |
22 | +impl Backend { | |
23 | + pub fn from_str(s: &str) -> Self { | |
24 | + match s.to_lowercase().as_str() { | |
25 | + "kitty" => Backend::Kitty, | |
26 | + "sixel" => Backend::Sixel, | |
27 | + "chafa" => Backend::Chafa, | |
28 | + "w3m" | "w3mimgdisplay" => Backend::W3m, | |
29 | + "iterm2" | "iterm" => Backend::Iterm2, | |
30 | + "off" => Backend::Off, | |
31 | + "auto" => Backend::Ascii, // Default: auto-detection is handled in main.rs | |
32 | + _ => Backend::Ascii, | |
33 | + } | |
34 | + } | |
35 | + | |
36 | + /// Check if the terminal supports this backend. | |
37 | + #[allow(dead_code)] | |
38 | + pub fn is_supported(&self) -> bool { | |
39 | + match self { | |
40 | + Backend::Ascii | Backend::Off => true, | |
41 | + Backend::Kitty => { | |
42 | + // Kitty is supported if TERM contains "kitty" or KITTY_PID is set | |
43 | + std::env::var("KITTY_PID").is_ok() | |
44 | + || std::env::var("TERM") | |
45 | + .unwrap_or_default() | |
46 | + .contains("kitty") | |
47 | + || std::env::var("TERM_PROGRAM") | |
48 | + .unwrap_or_default() | |
49 | + .contains("kitty") | |
50 | + } | |
0 | 51 | |