elio 1.4.0

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
use super::{ImageProtocol, TerminalIdentity};
use std::{env, fs, path::Path};

pub(super) fn pdf_preview_tools_available() -> bool {
    command_exists("pdfinfo") && command_exists("pdftocairo")
}

pub(in crate::app) fn detect_terminal_identity() -> TerminalIdentity {
    let term = env::var("TERM").unwrap_or_default().to_ascii_lowercase();
    let term_program = env::var("TERM_PROGRAM")
        .unwrap_or_default()
        .to_ascii_lowercase();
    let kitty_window_id = env::var_os("KITTY_WINDOW_ID").is_some();
    let konsole_dbus = env::var_os("KONSOLE_DBUS_SESSION").is_some()
        || env::var_os("KONSOLE_DBUS_SERVICE").is_some()
        || env::var_os("KONSOLE_DBUS_WINDOW").is_some();

    if kitty_window_id || term.contains("xterm-kitty") || term_program == "kitty" {
        TerminalIdentity::Kitty
    } else if term.contains("ghostty") || term_program == "ghostty" {
        TerminalIdentity::Ghostty
    } else if term.contains("wezterm") || term_program == "wezterm" {
        TerminalIdentity::WezTerm
    } else if term_program.contains("warp") || env::var_os("WARP_SESSION_ID").is_some() {
        TerminalIdentity::Warp
    } else if term_program == "iterm.app" {
        TerminalIdentity::ITerm2
    } else if term.contains("alacritty")
        || term_program.contains("alacritty")
        || env::var_os("ALACRITTY_SOCKET").is_some()
    {
        TerminalIdentity::Alacritty
    } else if konsole_dbus {
        // Konsole exports D-Bus identifiers into child shells so scripts can
        // address the current window/session.
        TerminalIdentity::Konsole
    } else if term == "foot" || term == "foot-extra" {
        // Foot sets TERM=foot or TERM=foot-extra and supports Sixel natively.
        TerminalIdentity::Foot
    } else if env::var_os("WT_SESSION").is_some() {
        // WT_SESSION is a GUID set by Windows Terminal in every shell it hosts.
        // WT_PROFILE_ID is also available but WT_SESSION is the canonical marker.
        TerminalIdentity::WindowsTerminal
    } else {
        TerminalIdentity::Other
    }
}

pub(in crate::app) fn select_image_protocol(
    identity: TerminalIdentity,
    image_previews_override: bool,
) -> ImageProtocol {
    match identity {
        TerminalIdentity::Kitty => ImageProtocol::KittyGraphics,
        TerminalIdentity::Ghostty => ImageProtocol::KittyGraphics,
        // Warp implements the basic Kitty Graphics transmit-and-place protocol
        // but not the Unicode placeholder extension that the `KittyGraphics`
        // path emits (`U=1`). Route Warp through `KittyDirectGraphics`, which uses
        // direct CSI cursor placement and matches what Warp actually renders.
        TerminalIdentity::Warp => ImageProtocol::KittyDirectGraphics,
        TerminalIdentity::Konsole => ImageProtocol::KittyDirectGraphics,
        TerminalIdentity::WezTerm | TerminalIdentity::ITerm2 => ImageProtocol::ItermInline,
        // Foot and Windows Terminal ≥ 1.22 both support Sixel graphics.
        TerminalIdentity::Foot | TerminalIdentity::WindowsTerminal => ImageProtocol::Sixel,
        // ELIO_IMAGE_PREVIEWS=1 force-enables KittyGraphics on unrecognised terminals
        // for testing. Alacritty is excluded — it does not support image protocols.
        TerminalIdentity::Other if image_previews_override => ImageProtocol::KittyGraphics,
        TerminalIdentity::Alacritty | TerminalIdentity::Other => ImageProtocol::None,
    }
}

pub(in crate::app) fn command_exists(program: &str) -> bool {
    if program.is_empty() {
        return false;
    }

    let program_path = Path::new(program);
    if program_path.components().count() > 1 {
        return executable_file_exists(program_path)
            || cfg!(windows) && executable_file_exists(&program_path.with_extension("exe"));
    }

    env::var_os("PATH").is_some_and(|paths| {
        env::split_paths(&paths).any(|dir| {
            let candidate = dir.join(program);
            executable_file_exists(&candidate)
                || cfg!(windows) && executable_file_exists(&candidate.with_extension("exe"))
        })
    })
}

fn executable_file_exists(path: &Path) -> bool {
    let Ok(metadata) = fs::metadata(path) else {
        return false;
    };
    if !metadata.is_file() {
        return false;
    }

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        metadata.permissions().mode() & 0o111 != 0
    }

    #[cfg(not(unix))]
    {
        true
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::{
        ffi::OsString,
        fs,
        path::PathBuf,
        sync::{Mutex, OnceLock},
        time::{SystemTime, UNIX_EPOCH},
    };

    fn temp_root(label: &str) -> PathBuf {
        let unique = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("system time should be after unix epoch")
            .as_nanos();
        std::env::temp_dir().join(format!("elio-inline-image-{label}-{unique}"))
    }

    fn terminal_env_lock() -> std::sync::MutexGuard<'static, ()> {
        static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
        LOCK.get_or_init(|| Mutex::new(()))
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner())
    }

    struct TerminalEnvGuard {
        saved: Vec<(&'static str, Option<OsString>)>,
    }

    impl TerminalEnvGuard {
        fn isolate() -> Self {
            const VARS: &[&str] = &[
                "TERM",
                "TERM_PROGRAM",
                "KITTY_WINDOW_ID",
                "WARP_SESSION_ID",
                "ALACRITTY_SOCKET",
                "WT_SESSION",
                "KONSOLE_DBUS_SESSION",
                "KONSOLE_DBUS_SERVICE",
                "KONSOLE_DBUS_WINDOW",
            ];

            let saved = VARS
                .iter()
                .map(|name| (*name, env::var_os(name)))
                .collect::<Vec<_>>();
            for name in VARS {
                unsafe {
                    env::remove_var(name);
                }
            }

            Self { saved }
        }
    }

    impl Drop for TerminalEnvGuard {
        fn drop(&mut self) {
            for (name, value) in &self.saved {
                if let Some(value) = value {
                    unsafe {
                        env::set_var(name, value);
                    }
                } else {
                    unsafe {
                        env::remove_var(name);
                    }
                }
            }
        }
    }

    #[test]
    fn detect_terminal_identity_recognizes_iterm2_term_program() {
        let _lock = terminal_env_lock();
        let _guard = TerminalEnvGuard::isolate();

        unsafe {
            env::set_var("TERM_PROGRAM", "iTerm.app");
        }

        assert_eq!(detect_terminal_identity(), TerminalIdentity::ITerm2);
    }

    #[test]
    fn select_image_protocol_kitty_always_enabled() {
        assert_eq!(
            select_image_protocol(TerminalIdentity::Kitty, false),
            ImageProtocol::KittyGraphics
        );
        assert_eq!(
            select_image_protocol(TerminalIdentity::Kitty, true),
            ImageProtocol::KittyGraphics
        );
    }

    #[test]
    fn select_image_protocol_ghostty_always_enabled() {
        assert_eq!(
            select_image_protocol(TerminalIdentity::Ghostty, false),
            ImageProtocol::KittyGraphics
        );
        assert_eq!(
            select_image_protocol(TerminalIdentity::Ghostty, true),
            ImageProtocol::KittyGraphics
        );
    }

    #[test]
    fn select_image_protocol_wezterm_always_enabled() {
        assert_eq!(
            select_image_protocol(TerminalIdentity::WezTerm, false),
            ImageProtocol::ItermInline
        );
        assert_eq!(
            select_image_protocol(TerminalIdentity::WezTerm, true),
            ImageProtocol::ItermInline
        );
    }

    #[test]
    fn select_image_protocol_iterm2_always_enabled() {
        assert_eq!(
            select_image_protocol(TerminalIdentity::ITerm2, false),
            ImageProtocol::ItermInline
        );
        assert_eq!(
            select_image_protocol(TerminalIdentity::ITerm2, true),
            ImageProtocol::ItermInline
        );
    }

    #[test]
    fn detect_terminal_identity_recognizes_konsole_dbus_session() {
        let _lock = terminal_env_lock();
        let _guard = TerminalEnvGuard::isolate();

        unsafe {
            env::set_var("KONSOLE_DBUS_SESSION", "/Sessions/7");
        }

        assert_eq!(detect_terminal_identity(), TerminalIdentity::Konsole);
    }

    #[test]
    fn detect_terminal_identity_recognizes_konsole_dbus_service() {
        let _lock = terminal_env_lock();
        let _guard = TerminalEnvGuard::isolate();

        unsafe {
            env::set_var("KONSOLE_DBUS_SERVICE", "org.kde.konsole-12345");
        }

        assert_eq!(detect_terminal_identity(), TerminalIdentity::Konsole);
    }

    #[test]
    fn select_image_protocol_konsole_always_enabled() {
        assert_eq!(
            select_image_protocol(TerminalIdentity::Konsole, false),
            ImageProtocol::KittyDirectGraphics
        );
        assert_eq!(
            select_image_protocol(TerminalIdentity::Konsole, true),
            ImageProtocol::KittyDirectGraphics
        );
    }

    #[test]
    fn wezterm_takes_precedence_over_inherited_konsole_markers() {
        let _lock = terminal_env_lock();
        let _guard = TerminalEnvGuard::isolate();

        unsafe {
            env::set_var("TERM_PROGRAM", "WezTerm");
            env::set_var("KONSOLE_DBUS_SESSION", "/Sessions/9");
        }

        assert_eq!(detect_terminal_identity(), TerminalIdentity::WezTerm);
    }

    #[test]
    fn select_image_protocol_warp_always_enabled() {
        assert_eq!(
            select_image_protocol(TerminalIdentity::Warp, false),
            ImageProtocol::KittyDirectGraphics
        );
        assert_eq!(
            select_image_protocol(TerminalIdentity::Warp, true),
            ImageProtocol::KittyDirectGraphics
        );
    }

    #[test]
    fn select_image_protocol_alacritty_disabled_and_other_override_enabled() {
        assert_eq!(
            select_image_protocol(TerminalIdentity::Alacritty, true),
            ImageProtocol::None
        );
        assert_eq!(
            select_image_protocol(TerminalIdentity::Other, false),
            ImageProtocol::None
        );
        assert_eq!(
            select_image_protocol(TerminalIdentity::Other, true),
            ImageProtocol::KittyGraphics
        );
    }

    #[test]
    fn detect_terminal_identity_recognizes_foot_term() {
        let _lock = terminal_env_lock();
        let _guard = TerminalEnvGuard::isolate();

        unsafe {
            env::set_var("TERM", "foot");
        }

        assert_eq!(detect_terminal_identity(), TerminalIdentity::Foot);
    }

    #[test]
    fn detect_terminal_identity_recognizes_foot_extra_term() {
        let _lock = terminal_env_lock();
        let _guard = TerminalEnvGuard::isolate();

        unsafe {
            env::set_var("TERM", "foot-extra");
        }

        assert_eq!(detect_terminal_identity(), TerminalIdentity::Foot);
    }

    #[test]
    fn select_image_protocol_foot_uses_sixel() {
        assert_eq!(
            select_image_protocol(TerminalIdentity::Foot, false),
            ImageProtocol::Sixel
        );
        assert_eq!(
            select_image_protocol(TerminalIdentity::Foot, true),
            ImageProtocol::Sixel
        );
    }

    #[test]
    fn detect_terminal_identity_recognizes_windows_terminal_wt_session() {
        let _lock = terminal_env_lock();
        let _guard = TerminalEnvGuard::isolate();

        unsafe {
            env::set_var("WT_SESSION", "00000000-0000-0000-0000-000000000001");
        }

        assert_eq!(
            detect_terminal_identity(),
            TerminalIdentity::WindowsTerminal
        );
    }

    #[test]
    fn select_image_protocol_windows_terminal_uses_sixel() {
        assert_eq!(
            select_image_protocol(TerminalIdentity::WindowsTerminal, false),
            ImageProtocol::Sixel
        );
        assert_eq!(
            select_image_protocol(TerminalIdentity::WindowsTerminal, true),
            ImageProtocol::Sixel
        );
    }

    #[test]
    fn windows_terminal_takes_precedence_over_other_fallback() {
        let _lock = terminal_env_lock();
        let _guard = TerminalEnvGuard::isolate();

        // WT_SESSION present, no other terminal markers → WindowsTerminal
        unsafe {
            env::set_var("WT_SESSION", "some-guid");
        }

        assert_eq!(
            detect_terminal_identity(),
            TerminalIdentity::WindowsTerminal
        );
    }

    #[cfg(unix)]
    #[test]
    fn command_exists_checks_direct_executable_paths_without_shelling_out() {
        use std::os::unix::fs::PermissionsExt;

        let root = temp_root("command-exists-direct-path");
        fs::create_dir_all(&root).expect("failed to create temp root");

        let executable = root.join("demo-tool");
        fs::write(&executable, b"#!/bin/sh\nexit 0\n").expect("failed to write test executable");

        let mut permissions = fs::metadata(&executable)
            .expect("test executable metadata should exist")
            .permissions();
        permissions.set_mode(0o755);
        fs::set_permissions(&executable, permissions).expect("failed to mark test executable");

        assert!(command_exists(
            executable.to_str().expect("path should be valid utf-8")
        ));

        let not_executable = root.join("demo-data");
        fs::write(&not_executable, b"plain data").expect("failed to write plain file");
        assert!(!command_exists(
            not_executable.to_str().expect("path should be valid utf-8")
        ));
    }
}