oxi-tui 0.42.0

Terminal UI widgets and theme system for oxi, built on ratatui
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
//! Terminal capability detection.
//!
//! Identifies the host terminal from its environment-variable signature and
//! derives a capability set from a built-in knowledge table. This is the safe,
//! fully unit-testable baseline.
//!
//! A *live* device-attributes probe (DA1/XTGETTCAP/XTVERSION queries that read
//! the terminal's reply from stdin) can refine these heuristics, but it must
//! run before the event loop begins reading input and needs careful stdin
//! isolation to avoid polluting the key stream. That probe is a follow-up; for
//! now every capability is derived from env vars via [`TerminalCapabilities::detect`].
//!
//! ## Safe defaults
//!
//! Each capability carries a *safe default* for unknown terminals:
//! - [`TerminalCapabilities::synchronized_output`] defaults to **true** — the
//!   CSI 2026 sequence is simply ignored by terminals that don't support it, so
//!   emitting it is harmless. Disable with `OXI_NO_SYNC_OUTPUT=1`.
//! - [`TerminalCapabilities::deccara`] and [`TerminalCapabilities::sixel`]
//!   default to **false** — emitting those to an unsupported terminal corrupts
//!   the display or renders garbage, so they are only enabled for terminals
//!   known to support them.

use std::env;

/// Supported image protocols for inline terminal images.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ImageProtocol {
    /// Kitty image protocol (supported by Kitty, Ghostty, WezTerm).
    Kitty,
    /// iTerm2 inline image protocol (supported by iTerm2, WezTerm).
    ITerm2,
}

/// The host terminal family, derived from env-var signatures.
///
/// Drives the capability table in [`TerminalCapabilities::detect`]. The detection
/// logic itself is split into a pure classifier ([`TerminalKind::classify`]) so
/// it can be unit-tested without touching the real process environment.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TerminalKind {
    Kitty,
    Ghostty,
    WezTerm,
    ITerm2,
    Foot,
    Contour,
    Alacritty,
    Konsole,
    Blackbox,
    Tabby,
    AppleTerminal,
    Xterm,
    Tmux,
    Screen,
    /// Anything we don't recognize — gets conservative defaults.
    Unknown,
}

impl TerminalKind {
    /// Human-readable family name surfaced via [`TerminalCapabilities::terminal_name`].
    fn display_name(self) -> &'static str {
        match self {
            Self::Kitty => "kitty",
            Self::Ghostty => "ghostty",
            Self::WezTerm => "wezterm",
            Self::ITerm2 => "iTerm2",
            Self::Foot => "foot",
            Self::Contour => "contour",
            Self::Alacritty => "alacritty",
            Self::Konsole => "konsole",
            Self::Blackbox => "blackbox",
            Self::Tabby => "tabby",
            Self::AppleTerminal => "Apple Terminal",
            Self::Xterm => "xterm",
            Self::Tmux => "tmux",
            Self::Screen => "screen",
            Self::Unknown => "unknown",
        }
    }

    /// Collect the relevant env-var signals into a value object.
    ///
    /// Kept separate from [`Self::classify`] so the classifier is pure and
    /// testable.
    #[allow(clippy::too_many_arguments)]
    fn signals(
        term_program: &str,
        term: &str,
        kitty_window_id: bool,
        ghostty_resources: bool,
        wezterm_pane: bool,
        iterm_session: bool,
        tmux: bool,
    ) -> Self {
        // Most-specific signals first.
        if kitty_window_id || term_program == "kitty" {
            return Self::Kitty;
        }
        if term_program == "ghostty" || ghostty_resources {
            return Self::Ghostty;
        }
        if wezterm_pane || term_program == "WezTerm" {
            return Self::WezTerm;
        }
        if iterm_session || term_program == "iTerm.app" {
            return Self::ITerm2;
        }
        if term_program == "contour" || term.starts_with("contour") {
            return Self::Contour;
        }
        if term_program == "alacritty" {
            return Self::Alacritty;
        }
        if term_program == "konsole" {
            return Self::Konsole;
        }
        if term_program == "blackbox" {
            return Self::Blackbox;
        }
        if term_program == "Tabby" {
            return Self::Tabby;
        }
        if term_program == "Apple_Terminal" {
            return Self::AppleTerminal;
        }
        if term == "foot" || term.starts_with("foot-") {
            return Self::Foot;
        }
        if tmux {
            return Self::Tmux;
        }
        if term.starts_with("screen") {
            return Self::Screen;
        }
        if term.starts_with("xterm") {
            return Self::Xterm;
        }
        Self::Unknown
    }
}

/// Detected terminal capabilities.
#[derive(Debug, Clone)]
pub struct TerminalCapabilities {
    /// Supported image protocol, if any.
    pub image_protocol: Option<ImageProtocol>,
    /// Whether the terminal supports 24-bit true color.
    pub true_color: bool,
    /// Whether the terminal supports OSC 8 hyperlinks.
    pub hyperlinks: bool,
    /// Whether the Kitty keyboard protocol is active.
    pub kitty_protocol: bool,
    /// Cell size in pixels (width, height), if detectable.
    pub cell_size: Option<(u16, u16)>,
    /// Identified terminal family name, if recognized.
    pub terminal_name: Option<String>,
    /// Supports synchronized output (CSI 2026 BSU/ESU). Defaults to **true** —
    /// the sequence is harmless on unsupported terminals (they ignore it).
    /// Disable with `OXI_NO_SYNC_OUTPUT=1`.
    pub synchronized_output: bool,
    /// Supports Kitty's DECCARA rectangular background-fill extension. Defaults
    /// to **false** — emitting DECCARA to an unsupported terminal corrupts the
    /// display, so it is only enabled for terminals known to support it
    /// (Kitty, Ghostty).
    pub deccara: bool,
    /// Supports Sixel inline images. Defaults to **false**.
    pub sixel: bool,
}

impl Default for TerminalCapabilities {
    fn default() -> Self {
        // Safe defaults: synchronized output on (harmless if ignored),
        // deccara/sixel off (harmful if unsupported).
        Self {
            image_protocol: None,
            true_color: false,
            hyperlinks: false,
            kitty_protocol: false,
            cell_size: None,
            terminal_name: None,
            synchronized_output: true,
            deccara: false,
            sixel: false,
        }
    }
}

impl TerminalCapabilities {
    /// Detect terminal capabilities from environment variables.
    ///
    /// Checks `TERM`, `TERM_PROGRAM`, `COLORTERM`, `KITTY_WINDOW_ID`,
    /// `GHOSTTY_RESOURCES_DIR`, `WEZTERM_PANE`, `ITERM_SESSION_ID`, `TMUX`,
    /// and `OXI_NO_SYNC_OUTPUT`.
    pub fn detect() -> Self {
        let term_program = env::var("TERM_PROGRAM").unwrap_or_default();
        let term = env::var("TERM").unwrap_or_default();
        let colorterm = env::var("COLORTERM").unwrap_or_default();
        let kind = TerminalKind::signals(
            &term_program,
            &term,
            env::var_os("KITTY_WINDOW_ID").is_some(),
            env::var_os("GHOSTTY_RESOURCES_DIR").is_some(),
            env::var_os("WEZTERM_PANE").is_some(),
            env::var_os("ITERM_SESSION_ID").is_some(),
            env::var_os("TMUX").is_some(),
        );

        // All field writes go through `apply_kind` (a method body, not the
        // `let x = Default::default(); x.f = ..` pattern clippy flags).
        let no_sync = env::var_os("OXI_NO_SYNC_OUTPUT").is_some();
        let mut caps = Self::default();
        caps.apply_kind(kind, &colorterm, &term, no_sync);
        caps
    }

    /// Apply the per-kind capability table onto `self` (kept separate from
    /// [`Self::detect`] so it is unit-testable without touching the process
    /// env). `no_sync` forces synchronized output off regardless of kind.
    fn apply_kind(&mut self, kind: TerminalKind, colorterm: &str, term: &str, no_sync: bool) {
        self.terminal_name = Some(kind.display_name().to_string());
        match kind {
            TerminalKind::Kitty | TerminalKind::Ghostty => {
                self.image_protocol = Some(ImageProtocol::Kitty);
                self.true_color = true;
                self.hyperlinks = true;
                self.kitty_protocol = true;
                self.synchronized_output = true;
                // Kitty/Ghostty implement the DECCARA rectangular SGR extension.
                self.deccara = true;
            }
            TerminalKind::WezTerm => {
                self.image_protocol = Some(ImageProtocol::Kitty);
                self.true_color = true;
                self.hyperlinks = true;
                self.kitty_protocol = true;
                self.synchronized_output = true;
                self.sixel = true;
            }
            TerminalKind::ITerm2 => {
                self.image_protocol = Some(ImageProtocol::ITerm2);
                self.true_color = true;
                self.hyperlinks = true;
                self.synchronized_output = true;
            }
            TerminalKind::Foot => {
                self.true_color = true;
                self.hyperlinks = true;
                self.synchronized_output = true;
                self.sixel = true;
            }
            TerminalKind::Contour => {
                self.image_protocol = Some(ImageProtocol::Kitty);
                self.true_color = true;
                self.hyperlinks = true;
                self.kitty_protocol = true;
                self.synchronized_output = true;
                self.sixel = true;
            }
            TerminalKind::Alacritty => {
                self.true_color = true;
                // Alacritty supports BSU/ESU (synchronized output) since 0.13.
                self.synchronized_output = true;
            }
            TerminalKind::Konsole => {
                self.true_color = true;
                self.hyperlinks = true;
                self.synchronized_output = true;
            }
            TerminalKind::Blackbox | TerminalKind::Tabby => {
                self.true_color = true;
                self.hyperlinks = true;
            }
            TerminalKind::AppleTerminal => {
                self.true_color = true;
                self.hyperlinks = true;
            }
            TerminalKind::Xterm => {
                self.true_color = colorterm == "truecolor" || colorterm == "24bit";
                // synchronized_output stays at its safe default (true).
            }
            TerminalKind::Tmux | TerminalKind::Screen | TerminalKind::Unknown => {
                self.true_color =
                    colorterm == "truecolor" || colorterm == "24bit" || term.contains("256color");
                // synchronized_output stays at its safe default (true); tmux
                // passes unknown sequences through or strips them harmlessly.
            }
        }
        if no_sync {
            self.synchronized_output = false;
        }
    }

    /// Check if images are supported.
    pub fn supports_images(&self) -> bool {
        self.image_protocol.is_some()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn classify(
        term_program: &str,
        term: &str,
        kitty: bool,
        ghostty: bool,
        wezterm: bool,
        iterm: bool,
        tmux: bool,
    ) -> TerminalKind {
        TerminalKind::signals(term_program, term, kitty, ghostty, wezterm, iterm, tmux)
    }

    #[test]
    fn default_sync_on_deccara_sixel_off() {
        // Safe defaults: sync harmless-on, deccara/sixel harmful-off.
        let caps = TerminalCapabilities::default();
        assert!(caps.synchronized_output);
        assert!(!caps.deccara);
        assert!(!caps.sixel);
        assert!(caps.image_protocol.is_none());
        assert!(!caps.true_color);
        assert!(caps.terminal_name.is_none());
    }

    #[test]
    fn supports_images_with_kitty() {
        let caps = TerminalCapabilities {
            image_protocol: Some(ImageProtocol::Kitty),
            ..Default::default()
        };
        assert!(caps.supports_images());
    }

    #[test]
    fn supports_images_with_iterm2() {
        let caps = TerminalCapabilities {
            image_protocol: Some(ImageProtocol::ITerm2),
            ..Default::default()
        };
        assert!(caps.supports_images());
    }

    // ── classification ────────────────────────────────────────────────────

    #[test]
    fn classify_kitty() {
        assert_eq!(
            classify("", "xterm-kitty", true, false, false, false, false),
            TerminalKind::Kitty
        );
        assert_eq!(
            classify("kitty", "", false, false, false, false, false),
            TerminalKind::Kitty
        );
    }

    #[test]
    fn classify_ghostty() {
        assert_eq!(
            classify("ghostty", "xterm-ghostty", false, true, false, false, false),
            TerminalKind::Ghostty
        );
    }

    #[test]
    fn classify_wezterm_and_iterm() {
        assert_eq!(
            classify("WezTerm", "", false, false, true, false, false),
            TerminalKind::WezTerm
        );
        assert_eq!(
            classify("iTerm.app", "", false, false, false, true, false),
            TerminalKind::ITerm2
        );
    }

    #[test]
    fn classify_tmux_and_screen_take_priority_over_xterm() {
        // TERM is xterm but TMUX is set → tmux, not xterm.
        assert_eq!(
            classify("", "xterm-256color", false, false, false, false, true),
            TerminalKind::Tmux
        );
        assert_eq!(
            classify("", "screen-256color", false, false, false, false, false),
            TerminalKind::Screen
        );
    }

    #[test]
    fn classify_unknown_fallback() {
        assert_eq!(
            classify("", "dumb", false, false, false, false, false),
            TerminalKind::Unknown
        );
    }

    // ── capability table ──────────────────────────────────────────────────

    fn caps_for(kind: TerminalKind) -> TerminalCapabilities {
        let mut caps = TerminalCapabilities::default();
        caps.apply_kind(kind, "truecolor", "xterm-256color", false);
        caps
    }

    #[test]
    fn kitty_and_ghostty_get_deccara() {
        // The headline capability Phase 2 (DECCARA bg-fill) will gate on.
        for kind in [TerminalKind::Kitty, TerminalKind::Ghostty] {
            let caps = caps_for(kind);
            assert!(caps.deccara, "{kind:?} should support DECCARA");
            assert!(caps.synchronized_output);
            assert!(caps.kitty_protocol);
            assert_eq!(caps.image_protocol, Some(ImageProtocol::Kitty));
        }
    }

    #[test]
    fn wezterm_foot_contour_get_sixel_not_deccara() {
        for kind in [
            TerminalKind::WezTerm,
            TerminalKind::Foot,
            TerminalKind::Contour,
        ] {
            let caps = caps_for(kind);
            assert!(caps.sixel, "{kind:?} should support sixel");
            assert!(!caps.deccara, "{kind:?} should NOT claim DECCARA");
            assert!(caps.synchronized_output);
        }
    }

    #[test]
    fn iterm2_uses_iterm_image_protocol() {
        let caps = caps_for(TerminalKind::ITerm2);
        assert_eq!(caps.image_protocol, Some(ImageProtocol::ITerm2));
        assert!(!caps.deccara);
        assert!(caps.synchronized_output);
    }

    #[test]
    fn xterm_true_color_only_with_colorterm() {
        let mut on = TerminalCapabilities::default();
        on.apply_kind(TerminalKind::Xterm, "truecolor", "xterm-256color", false);
        assert!(on.true_color);
        let mut off = TerminalCapabilities::default();
        off.apply_kind(TerminalKind::Xterm, "", "xterm", false);
        assert!(!off.true_color);
    }

    #[test]
    fn no_sync_override_forces_synchronized_output_off() {
        // Even Kitty (which supports sync) must respect the manual opt-out.
        let mut caps = TerminalCapabilities::default();
        caps.apply_kind(TerminalKind::Kitty, "truecolor", "xterm-kitty", true);
        assert!(!caps.synchronized_output);
        assert!(
            caps.deccara,
            "opt-out only affects sync, not other capabilities"
        );
    }

    #[test]
    fn detect_runs_without_panic() {
        // Reads the real process env; just must not panic.
        let caps = TerminalCapabilities::detect();
        // Safe defaults always hold absent an explicit opt-out.
        let no_override = std::env::var_os("OXI_NO_SYNC_OUTPUT").is_none();
        if no_override {
            assert!(caps.synchronized_output);
        }
        assert!(caps.terminal_name.is_some());
    }
}