elio 1.5.1

Snappy, batteries-included terminal file manager with rich previews, inline images, bulk actions, and trash support.
Documentation
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
mod geometry;
mod iterm;
mod kitty;
mod konsole;
mod protocol;
mod sixel;
mod tmux;
mod window;

use anyhow::{Context, Result};
use ratatui::layout::Rect;
use std::{env, io::Write as _, path::Path};

use crate::app::App;

pub(in crate::app) use self::geometry::{
    area_pixel_size, fit_image_area, fit_image_pixels, read_png_dimensions,
};
pub(in crate::app) use self::iterm::encode_iterm_inline_payload;
pub(in crate::app) use self::protocol::{command_exists, select_image_protocol};
use self::protocol::{detect_terminal_identity, pdf_preview_tools_available};
pub(in crate::app) use self::sixel::{encode_sixel_dcs, place_sixel_from_dcs};
use self::window::query_terminal_window_size;

/// Write a line to `<temp>/elio-preview.log` when `ELIO_DEBUG_PREVIEW` is set.
/// Does nothing (and compiles to nothing meaningful) when the env var is absent.
pub(in crate::app) fn preview_log(msg: impl std::fmt::Display) {
    if env::var_os("ELIO_DEBUG_PREVIEW").is_none() {
        return;
    }
    let _ = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(std::env::temp_dir().join("elio-preview.log"))
        .and_then(|mut f| writeln!(f, "{msg}"));
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub(in crate::app) struct TerminalImageState {
    pub(super) protocol: ImageProtocol,
    pub(super) identity: TerminalIdentity,
    pub(super) window: Option<TerminalWindowSize>,
    pending_iterm_erase: Vec<Rect>,
    pending_resize_clear: bool,
    pending_iterm_popup_restore: bool,
    pending_sixel_repaint: bool,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(in crate::app) enum OverlayPresentState {
    NotRequested,
    Waiting,
    Displayed,
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(in crate::app) enum TerminalIdentity {
    Kitty,
    Ghostty,
    Warp,
    WezTerm,
    ITerm2,
    Konsole,
    Alacritty,
    Foot,
    WindowsTerminal,
    #[default]
    Other,
}

/// The wire protocol used to render images in the terminal preview pane.
/// Kept separate from `TerminalIdentity` so that multiple terminals can share
/// the same protocol without coupling detection logic to rendering logic.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub(in crate::app) enum ImageProtocol {
    /// Kitty Graphics Protocol (APC `\x1b_G…\x1b\\`) using the Unicode
    /// placeholder extension. Used by Kitty and Ghostty.
    KittyGraphics,
    /// Direct-placement variant of the Kitty Graphics Protocol: same APC wire
    /// format, but without the Unicode placeholder extension. Images are
    /// positioned with an explicit CSI cursor move and need explicit delete
    /// commands. Used by Konsole and Warp.
    KittyDirectGraphics,
    /// iTerm2 inline image protocol (OSC 1337). Used by WezTerm and iTerm2.
    ItermInline,
    /// Sixel graphics protocol (DCS). Used by Windows Terminal (≥ 1.22).
    Sixel,
    #[default]
    None,
}

impl ImageProtocol {
    /// Returns `true` for pixel-buffer protocols that write directly into the
    /// terminal framebuffer and have no dedicated clear command (iTerm2 and
    /// Sixel).  These protocols require a pre-draw cell-erase pass before
    /// ratatui can safely overpaint stale image content.
    pub(in crate::app) fn is_raster(self) -> bool {
        matches!(self, ImageProtocol::ItermInline | ImageProtocol::Sixel)
    }
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub(in crate::app) struct TerminalWindowSize {
    pub(super) cells_width: u16,
    pub(super) cells_height: u16,
    pub(super) pixels_width: u32,
    pub(super) pixels_height: u32,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(in crate::app) struct RenderedImageDimensions {
    pub(super) width_px: u32,
    pub(super) height_px: u32,
}

impl App {
    pub(crate) fn enable_terminal_image_previews(&mut self) {
        let identity = detect_terminal_identity();
        let image_previews_override = env::var_os("ELIO_IMAGE_PREVIEWS").is_some();
        let protocol = select_image_protocol(identity, image_previews_override);
        preview_log(format_args!(
            "enable_terminal_image_previews:\n  TERM={}\n  TERM_PROGRAM={}\n  KITTY_WINDOW_ID={}\n  WARP_SESSION_ID={}\n  WT_SESSION={}\n  KONSOLE_DBUS_SESSION={}\n  KONSOLE_DBUS_SERVICE={}\n  KONSOLE_DBUS_WINDOW={}\n  identity={identity:?}\n  override={image_previews_override}\n  protocol={protocol:?}",
            env::var("TERM").unwrap_or_default(),
            env::var("TERM_PROGRAM").unwrap_or_default(),
            env::var_os("KITTY_WINDOW_ID").is_some(),
            env::var_os("WARP_SESSION_ID").is_some(),
            env::var_os("WT_SESSION").is_some(),
            env::var_os("KONSOLE_DBUS_SESSION").is_some(),
            env::var_os("KONSOLE_DBUS_SERVICE").is_some(),
            env::var_os("KONSOLE_DBUS_WINDOW").is_some(),
        ));
        self.preview.terminal_images.identity = identity;
        self.preview.terminal_images.protocol = protocol;
        if matches!(
            protocol,
            ImageProtocol::KittyGraphics
                | ImageProtocol::KittyDirectGraphics
                | ImageProtocol::ItermInline
                | ImageProtocol::Sixel
        ) {
            tmux::enable_allow_passthrough();
        }
        self.preview.pdf.pdf_tools_available = pdf_preview_tools_available();
        self.refresh_terminal_image_window_size();
        preview_log(format_args!(
            "  window={:?}",
            self.preview.terminal_images.window
        ));
        self.sync_pdf_preview_selection();
    }

    pub(crate) fn handle_terminal_image_resize(&mut self) {
        self.refresh_terminal_image_window_size();
        if matches!(
            self.preview.terminal_images.protocol,
            ImageProtocol::KittyGraphics | ImageProtocol::Sixel
        ) && (self.static_image_overlay_displayed() || self.pdf_overlay_displayed())
        {
            // Kitty unicode placeholders can reflow on resize, while Sixel can
            // leave stale framebuffer pixels outside the new bounds in Foot.
            // Force a full-screen clear on the next draw so ratatui repaints
            // the entire alt screen before the image is re-rendered.
            self.preview.terminal_images.pending_resize_clear = true;
        }
        self.handle_pdf_overlay_resize();
    }

    pub(crate) fn take_pending_resize_clear(&mut self) -> bool {
        if !self.preview.terminal_images.pending_resize_clear {
            return false;
        }

        self.preview.terminal_images.pending_resize_clear = false;
        self.clear_displayed_static_image();
        self.clear_displayed_pdf_overlay();
        true
    }

    pub(in crate::app) fn terminal_image_overlay_available(&self) -> bool {
        self.preview.terminal_images.protocol != ImageProtocol::None
    }

    pub(in crate::app) fn uses_sixel_image_protocol(&self) -> bool {
        self.preview.terminal_images.protocol == ImageProtocol::Sixel
    }

    pub(in crate::app) fn uses_iterm_inline_protocol_inside_tmux(&self) -> bool {
        self.preview.terminal_images.protocol == ImageProtocol::ItermInline && tmux::inside_tmux()
    }

    pub(crate) fn is_windows_terminal(&self) -> bool {
        self.preview.terminal_images.identity == TerminalIdentity::WindowsTerminal
    }

    pub(in crate::app) fn needs_sixel_repaint_workaround(&self) -> bool {
        self.preview.terminal_images.protocol == ImageProtocol::Sixel
            && self.preview.terminal_images.identity == TerminalIdentity::Foot
    }

    pub(in crate::app) fn needs_slow_sixel_navigation_workaround(&self) -> bool {
        self.preview.terminal_images.protocol == ImageProtocol::Sixel
            && matches!(
                self.preview.terminal_images.identity,
                TerminalIdentity::Foot | TerminalIdentity::WindowsTerminal
            )
    }

    #[cfg(test)]
    pub(in crate::app) fn set_terminal_image_protocol_for_tests(
        &mut self,
        protocol: ImageProtocol,
        identity: TerminalIdentity,
    ) {
        self.preview.terminal_images.protocol = protocol;
        self.preview.terminal_images.identity = identity;
    }

    pub(in crate::app) fn cached_terminal_window(&self) -> Option<TerminalWindowSize> {
        self.preview.terminal_images.window
    }

    /// Returns Kitty erase bytes that must be written to the terminal **before**
    /// `terminal.draw()` when a unicode-placeholder image is about to be replaced
    /// or cleared.
    ///
    /// Unlike standard Kitty placement, unicode placeholder cells are regular
    /// terminal characters. ratatui's differential renderer skips cells it
    /// considers "unchanged", leaving stale placeholder chars visible even after
    /// the image is no longer active. Emitting spaces to those cells before the
    /// draw forces the terminal to show blank content, which ratatui then
    /// overpaints correctly.
    pub(crate) fn kitty_pre_draw_erase(&self) -> Vec<u8> {
        if self.preview.terminal_images.protocol != ImageProtocol::KittyGraphics {
            return Vec::new();
        }
        let keep_stale = self.keep_displayed_static_image_overlay_while_pending();
        let needs_clear = (self.static_image_overlay_displayed()
            && !self.displayed_static_image_matches_active()
            && !keep_stale)
            || (self.pdf_overlay_displayed() && !self.displayed_pdf_overlay_matches_active());
        if !needs_clear {
            return Vec::new();
        }
        self.displayed_static_image_clear_area()
            .or_else(|| self.displayed_pdf_overlay_area())
            .map(iterm::erase_cells)
            .unwrap_or_default()
    }

    /// Returns iTerm2 erase bytes that must be written to the terminal **before**
    /// `terminal.draw()` when an image is about to be replaced or cleared.
    ///
    /// Emitting the erase before the draw lets ratatui naturally overpaint the
    /// erased cells with the correct panel background in the same render pass,
    /// avoiding the black-background artifact that occurs when erasing after draw.
    pub(crate) fn iterm_pre_draw_erase(&mut self) -> Vec<u8> {
        if !self.preview.terminal_images.protocol.is_raster() {
            return Vec::new();
        }
        let mut areas = std::mem::take(&mut self.preview.terminal_images.pending_iterm_erase);
        let keep_stale = self.keep_displayed_static_image_overlay_while_pending();
        if self.static_image_overlay_displayed()
            && !self.displayed_static_image_matches_active()
            && !keep_stale
            && let Some(area) = self.displayed_static_image_clear_area()
        {
            geometry::push_unique_rect(&mut areas, area);
        }
        if self.pdf_overlay_displayed()
            && !self.displayed_pdf_overlay_matches_active()
            && let Some(area) = self.displayed_pdf_overlay_area()
        {
            geometry::push_unique_rect(&mut areas, area);
        }
        if areas.is_empty() {
            return Vec::new();
        }
        let mut expanded_areas = Vec::with_capacity(areas.len());
        for area in areas {
            geometry::push_unique_rect(
                &mut expanded_areas,
                if self.preview.terminal_images.identity == TerminalIdentity::WindowsTerminal
                    && self.preview.terminal_images.protocol == ImageProtocol::Sixel
                {
                    iterm::expand_raster_erase_area(&self.input.frame_state, area, 1, 1)
                } else {
                    iterm::expand_raster_erase_area(&self.input.frame_state, area, 0, 2)
                },
            );
        }
        expanded_areas
            .into_iter()
            .flat_map(iterm::erase_cells)
            .collect()
    }

    pub(crate) fn present_preview_overlay(&mut self) -> Result<Vec<u8>> {
        if self.browser_wheel_burst_active() || self.preview.state.deferred_refresh_at.is_some() {
            return Ok(Vec::new());
        }

        let protocol = self.preview.terminal_images.protocol;
        if protocol == ImageProtocol::None {
            preview_log("present_preview_overlay: no protocol -> clear");
            return self.clear_preview_overlay();
        }

        let popup_open = self.any_modal_overlay_open();
        if protocol == ImageProtocol::KittyDirectGraphics && popup_open {
            if self.static_image_overlay_displayed() || self.pdf_overlay_displayed() {
                return self.clear_preview_overlay();
            }
            return Ok(Vec::new());
        }

        if protocol.is_raster()
            && popup_open
            && (self.static_image_overlay_displayed() || self.pdf_overlay_displayed())
        {
            self.preview.terminal_images.pending_iterm_popup_restore = true;
        }
        let force_sixel_repaint = protocol == ImageProtocol::Sixel
            && std::mem::take(&mut self.preview.terminal_images.pending_sixel_repaint);
        let force_iterm_popup_repaint = protocol.is_raster()
            && self.preview.terminal_images.pending_iterm_popup_restore
            && !popup_open;
        let force_protocol_repaint = force_iterm_popup_repaint || force_sixel_repaint;

        // For Kitty, collect rects occupied by open popups so the image can be
        // rendered only in cells not covered by any popup.
        let excluded: Vec<Rect> = if protocol == ImageProtocol::KittyGraphics {
            self.collect_popup_rects()
        } else {
            Vec::new()
        };

        let keep_stale_page_preview_overlay =
            self.keep_displayed_static_image_overlay_while_pending();
        let mut out = Vec::new();
        if (self.static_image_overlay_displayed()
            && !self.displayed_static_image_matches_active()
            && !keep_stale_page_preview_overlay)
            || self.pdf_overlay_displayed() && !self.displayed_pdf_overlay_matches_active()
        {
            out.extend(self.clear_preview_overlay()?);
        }

        let static_state = self.present_static_image_overlay(
            protocol,
            &excluded,
            force_protocol_repaint,
            &mut out,
        )?;
        preview_log(format_args!(
            "present_preview_overlay: protocol={protocol:?} static={static_state:?} out_len={}",
            out.len()
        ));
        match static_state {
            OverlayPresentState::Displayed | OverlayPresentState::Waiting => return Ok(out),
            OverlayPresentState::NotRequested => {}
        }

        let pdf_state =
            self.present_pdf_overlay(protocol, &excluded, force_protocol_repaint, &mut out)?;
        preview_log(format_args!(
            "present_preview_overlay: pdf={pdf_state:?} out_len={}",
            out.len()
        ));
        match pdf_state {
            OverlayPresentState::Displayed | OverlayPresentState::Waiting => return Ok(out),
            OverlayPresentState::NotRequested => {}
        }

        let visual_state = self.present_preview_visual_overlay(
            protocol,
            &excluded,
            force_protocol_repaint,
            &mut out,
        )?;
        preview_log(format_args!(
            "present_preview_overlay: visual={visual_state:?} out_len={}",
            out.len()
        ));
        match visual_state {
            OverlayPresentState::Displayed | OverlayPresentState::Waiting => Ok(out),
            OverlayPresentState::NotRequested if keep_stale_page_preview_overlay => Ok(out),
            OverlayPresentState::NotRequested => {
                self.preview.terminal_images.pending_iterm_popup_restore = false;
                out.extend(self.clear_preview_overlay()?);
                Ok(out)
            }
        }
    }

    fn collect_popup_rects(&self) -> Vec<Rect> {
        let mut rects = Vec::new();
        if let Some(r) = self.input.frame_state.trash_panel {
            rects.push(r);
        }
        if let Some(r) = self.input.frame_state.restore_panel {
            rects.push(r);
        }
        if let Some(r) = self.input.frame_state.create_panel {
            rects.push(r);
        }
        if let Some(r) = self.input.frame_state.rename_panel {
            rects.push(r);
        }
        if let Some(r) = self.input.frame_state.goto_panel {
            rects.push(r);
        }
        if let Some(r) = self.input.frame_state.copy_panel {
            rects.push(r);
        }
        if let Some(r) = self.input.frame_state.open_with_panel {
            rects.push(r);
        }
        if let Some(r) = self.input.frame_state.search_panel {
            rects.push(r);
        }
        if let Some(r) = self.input.frame_state.help_panel {
            rects.push(r);
        }
        rects
    }

    fn any_modal_overlay_open(&self) -> bool {
        self.overlays.trash.is_some()
            || self.overlays.restore.is_some()
            || self.overlays.create.is_some()
            || self.overlays.rename.is_some()
            || self.overlays.bulk_rename.is_some()
            || self.overlays.goto.is_some()
            || self.overlays.copy.is_some()
            || self.overlays.open_with.is_some()
            || self.overlays.search.is_some()
            || self.overlays.help
    }

    pub(in crate::app) fn clear_pending_iterm_popup_restore(&mut self) {
        self.preview.terminal_images.pending_iterm_popup_restore = false;
    }

    pub(in crate::app) fn queue_sixel_repaint(&mut self) {
        if self.needs_sixel_repaint_workaround() {
            self.preview.terminal_images.pending_sixel_repaint = true;
        }
    }

    pub(in crate::app) fn queue_windows_terminal_pdf_sixel_repaint(&mut self) {
        if self.preview.terminal_images.protocol == ImageProtocol::Sixel
            && self.preview.terminal_images.identity == TerminalIdentity::WindowsTerminal
        {
            self.preview.terminal_images.pending_sixel_repaint = true;
        }
    }

    pub(crate) fn clear_preview_overlay(&mut self) -> Result<Vec<u8>> {
        if !self.static_image_overlay_displayed() && !self.pdf_overlay_displayed() {
            return Ok(Vec::new());
        }
        let bytes = clear_terminal_images(self.preview.terminal_images.protocol)
            .context("failed to clear preview overlay")?;
        // iTerm2 erase is emitted by iterm_pre_draw_erase() *before* terminal.draw(),
        // so ratatui naturally overpaints with the correct panel background. Nothing
        // extra needed here.
        self.clear_pending_iterm_popup_restore();
        self.clear_displayed_static_image();
        self.clear_displayed_pdf_overlay();
        Ok(bytes)
    }

    pub(crate) fn queue_forced_iterm_preview_erase(&mut self) {
        if !self.preview.terminal_images.protocol.is_raster() {
            return;
        }
        if let Some(area) = self.displayed_static_image_clear_area() {
            geometry::push_unique_rect(&mut self.preview.terminal_images.pending_iterm_erase, area);
        }
        if let Some(area) = self.displayed_pdf_overlay_area() {
            geometry::push_unique_rect(&mut self.preview.terminal_images.pending_iterm_erase, area);
        }
    }

    pub(crate) fn preview_uses_image_overlay(&self) -> bool {
        self.displayed_static_image_replaces_preview()
            || self.displayed_pdf_overlay_matches_active()
    }

    pub(crate) fn preview_prefers_image_surface(&self) -> bool {
        self.preview_prefers_static_image_surface() || self.preview_prefers_pdf_surface()
    }

    fn refresh_terminal_image_window_size(&mut self) {
        self.preview.terminal_images.window = (self.preview.terminal_images.protocol
            != ImageProtocol::None)
            .then(query_terminal_window_size)
            .flatten();
    }
}

pub(in crate::app) fn place_terminal_image(
    protocol: ImageProtocol,
    path: &Path,
    area: Rect,
    excluded: &[Rect],
    inline_payload: Option<&str>,
    window_size: Option<TerminalWindowSize>,
) -> Result<Vec<u8>> {
    match protocol {
        ImageProtocol::KittyGraphics => {
            kitty::place_terminal_image_with_kitty_protocol(path, area, excluded)
        }
        ImageProtocol::KittyDirectGraphics => {
            konsole::place_terminal_image_with_konsole_protocol(path, area)
        }
        ImageProtocol::ItermInline => {
            iterm::place_terminal_image_with_iterm_protocol(path, area, inline_payload)
        }
        ImageProtocol::Sixel => {
            let ws = window_size.ok_or_else(|| {
                anyhow::anyhow!("sixel protocol requires terminal window size, but none available")
            })?;
            sixel::place_terminal_image_with_sixel_protocol(path, area, ws)
        }
        ImageProtocol::None => Ok(Vec::new()),
    }
}

pub(in crate::app) fn clear_terminal_images(protocol: ImageProtocol) -> Result<Vec<u8>> {
    match protocol {
        ImageProtocol::KittyGraphics => kitty::clear_terminal_images_with_kitty_protocol(),
        ImageProtocol::KittyDirectGraphics => {
            konsole::clear_terminal_images_with_konsole_protocol()
        }
        // iTerm2 has no clear primitive — the overlay is erased by the next
        // ratatui draw call overwriting the cell region.
        ImageProtocol::ItermInline | ImageProtocol::None => Ok(Vec::new()),
        // Sixel also has no clear primitive (same as iTerm2).
        ImageProtocol::Sixel => sixel::clear_terminal_images_with_sixel_protocol(),
    }
}