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
use anyhow::Result;
use std::path::Path;
/// Terminal image display capabilities
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TerminalImageSupport {
Kitty, // Kitty graphics protocol
ITerm2, // iTerm2 graphics protocol
Sixel, // Sixel graphics
HalfBlocks, // Unicode half-block fallback
None, // Text description only
}
/// Handles display of images in the terminal using various protocols
#[derive(Debug)]
pub struct TerminalImageRenderer {
support: TerminalImageSupport,
max_width: u32,
max_height: u32,
}
impl TerminalImageRenderer {
/// Create a new terminal image renderer with auto-detected capabilities
pub fn new() -> Self {
let support = Self::detect_capabilities();
let (max_width, max_height) = Self::get_terminal_size();
Self {
support,
max_width,
max_height,
}
}
/// Create a new terminal image renderer with custom size limits
pub fn with_size_limits(max_width: Option<u32>, max_height: Option<u32>) -> Self {
let support = Self::detect_capabilities();
let (default_width, default_height) = Self::get_terminal_size();
Self {
support,
max_width: max_width.unwrap_or(default_width),
max_height: max_height.unwrap_or(default_height),
}
}
/// Create a new terminal image renderer with custom size limits and scaling
pub fn with_options(
max_width: Option<u32>,
max_height: Option<u32>,
scale: Option<f32>,
) -> Self {
let support = Self::detect_capabilities();
let (default_width, default_height) = Self::get_terminal_size();
let scale_factor = scale.unwrap_or(1.0).clamp(0.1, 2.0); // Clamp between 0.1 and 2.0
let scaled_width = max_width.unwrap_or(default_width);
let scaled_height = max_height.unwrap_or(default_height);
Self {
support,
max_width: ((scaled_width as f32) * scale_factor) as u32,
max_height: ((scaled_height as f32) * scale_factor) as u32,
}
}
/// Create a renderer with specific capabilities (for testing)
pub fn with_support(support: TerminalImageSupport) -> Self {
let (max_width, max_height) = Self::get_terminal_size();
Self {
support,
max_width,
max_height,
}
}
/// Detect terminal image display capabilities
pub fn detect_capabilities() -> TerminalImageSupport {
// Check for WezTerm FIRST - it supports Kitty protocol
if let Ok(term_program) = std::env::var("TERM_PROGRAM") {
if term_program == "WezTerm" {
return TerminalImageSupport::Kitty;
}
}
// Check for iTerm2 (this function exists)
if viuer::is_iterm_supported() {
return TerminalImageSupport::ITerm2;
}
// Sixel support disabled for now to avoid linking issues
// Will re-enable after fixing dependencies
// Check terminal type for Kitty support
if let Ok(term) = std::env::var("TERM") {
match term.as_str() {
"xterm-kitty" => TerminalImageSupport::Kitty,
"wezterm" => TerminalImageSupport::Kitty,
"screen" | "screen-256color" => {
// Screen/tmux might support passthrough
TerminalImageSupport::HalfBlocks
}
_ => TerminalImageSupport::HalfBlocks,
}
} else {
TerminalImageSupport::HalfBlocks
}
}
/// Get the current support level
pub fn support(&self) -> TerminalImageSupport {
self.support
}
/// Check if we can display images inline
pub fn can_display_images(&self) -> bool {
!matches!(self.support, TerminalImageSupport::None)
}
/// Render an image from a file path
pub fn render_image_from_path(&self, image_path: &Path, description: &str) -> Result<()> {
match self.support {
TerminalImageSupport::None => {
println!("📷 Image: {description}");
Ok(())
}
_ => {
let display_path = image_path.to_path_buf();
// Use viuer to display the image with appropriate protocol
let mut conf = viuer::Config {
transparent: true,
absolute_offset: false,
width: Some(self.max_width.min(80)), // Limit width to 80 columns
height: Some(self.max_height.min(24)), // Limit height to 24 rows
..Default::default()
};
// Set protocol based on terminal capability
match self.support {
TerminalImageSupport::Kitty => {
conf.use_kitty = true;
conf.use_iterm = false;
}
TerminalImageSupport::ITerm2 => {
conf.use_kitty = false;
conf.use_iterm = true;
}
_ => {}
}
match viuer::print_from_file(&display_path, &conf) {
Ok(_) => {
// Print description after the image
if !description.is_empty() {
println!("📷 {description}");
}
Ok(())
}
Err(e) => {
// Fallback to text description if image display fails
println!("📷 Image: {description} (display failed: {e})");
Ok(())
}
}
}
}
}
/// Render an image from raw bytes
pub fn render_image_from_bytes(&self, image_data: &[u8], description: &str) -> Result<()> {
match self.support {
TerminalImageSupport::None => {
println!("📷 Image: {description}");
Ok(())
}
_ => {
let mut conf = viuer::Config {
transparent: true,
absolute_offset: false,
width: Some(self.max_width.min(80)),
height: Some(self.max_height.min(24)),
..Default::default()
};
// Set protocol based on terminal capability
match self.support {
TerminalImageSupport::Kitty => {
conf.use_kitty = true;
conf.use_iterm = false;
}
TerminalImageSupport::ITerm2 => {
conf.use_kitty = false;
conf.use_iterm = true;
}
_ => {}
}
// Create a temporary file for viuer (it needs a file path)
let temp_path = std::env::temp_dir().join("doxx_temp_image.png");
std::fs::write(&temp_path, image_data)?;
match viuer::print_from_file(&temp_path, &conf) {
Ok(_) => {
// Clean up temp file
let _ = std::fs::remove_file(&temp_path);
if !description.is_empty() {
println!("📷 {description}");
}
Ok(())
}
Err(e) => {
println!("📷 Image: {description} (display failed: {e})");
Ok(())
}
}
}
}
}
/// Get terminal size for image scaling
fn get_terminal_size() -> (u32, u32) {
// Try to get terminal size from crossterm
if let Ok((width, height)) = crossterm::terminal::size() {
(width as u32, height as u32)
} else {
// Fallback to reasonable defaults
(80, 24)
}
}
/// Print capabilities information for debugging
pub fn print_capabilities(&self) {
println!("=== Terminal Image Debug Information ===");
println!("Detected support: {:?}", self.support);
println!("Max dimensions: {}x{}", self.max_width, self.max_height);
println!("Can display images: {}", self.can_display_images());
// Environment variables
if let Ok(term) = std::env::var("TERM") {
println!("TERM: {term}");
} else {
println!("TERM: not set");
}
if let Ok(term_program) = std::env::var("TERM_PROGRAM") {
println!("TERM_PROGRAM: {term_program}");
} else {
println!("TERM_PROGRAM: not set");
}
// Viuer capabilities
println!(
"viuer::is_iterm_supported(): {}",
viuer::is_iterm_supported()
);
// Additional debug info
if let Ok(colorterm) = std::env::var("COLORTERM") {
println!("COLORTERM: {colorterm}");
}
println!("========================================");
}
/// Debug method to test image rendering
pub fn debug_render(&self) {
println!(
"DEBUG: Attempting to render test image with support: {:?}",
self.support
);
}
}
impl Default for TerminalImageRenderer {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_capability_detection() {
// This will vary by environment, but should not panic
let support = TerminalImageRenderer::detect_capabilities();
println!("Detected support: {support:?}");
}
#[test]
fn test_renderer_creation() {
let renderer = TerminalImageRenderer::new();
assert!(renderer.max_width > 0);
assert!(renderer.max_height > 0);
}
#[test]
fn test_can_display_images() {
let renderer = TerminalImageRenderer::with_support(TerminalImageSupport::Kitty);
assert!(renderer.can_display_images());
let renderer = TerminalImageRenderer::with_support(TerminalImageSupport::None);
assert!(!renderer.can_display_images());
}
}