uncurses/program/state.rs
1//! Terminal/input mode state owned by the [`Program`] facade.
2//!
3//! Most fields record a mode the facade has *emitted*, so it can tear the
4//! mode down on a shell handoff and re-apply it afterwards.
5//! [`chosen`](State::chosen) is the exception: it records which modes the app
6//! has decided for either way, so discovery can tell an explicit `disable_*`
7//! apart from silence.
8//!
9//! Three of them — [`alt_screen`](State::alt_screen),
10//! [`cursor_visible`](State::cursor_visible), and
11//! [`grapheme_clusters`](State::grapheme_clusters) — are mirrored by a render
12//! property on the [`Screen`](crate::screen::Screen) the facade draws with.
13//! They are tracked separately on purpose: the screen's copy says how to draw
14//! a frame, this one says what the terminal was told. Since
15//! [`screen_mut`](super::Program::screen_mut) lets an app move the render
16//! property on its own, inferring one from the other would make
17//! [`reset`](super::Program::reset) emit modes that were never set (or skip
18//! ones that were) and leave the terminal wedged after exit.
19//!
20//! [`Program`]: super::Program
21
22use std::collections::{BTreeMap, BTreeSet};
23
24use crate::ansi::cursor::CursorStyle;
25use crate::ansi::kitty::KittyKeyboardFlags;
26use crate::ansi::mode::{Mode, ModeSetting};
27use crate::color::Color;
28use crate::event::{ColorScheme, Event, ModifyOtherKeysMode, ProgramStatusCapabilities};
29
30use super::MouseTracking;
31use super::ProgressState;
32
33/// Session choices tracked by [`Program`](super::Program) for save and restore.
34///
35/// The values record requests staged through `Program`, including preferences
36/// adopted after discovery. They describe the state restored after a shell
37/// handoff, even while the program is paused. They do not confirm terminal
38/// acceptance or successful output after an I/O error. Direct writes and
39/// changes through `screen_mut` are outside this record.
40///
41/// [`Capabilities`] holds terminal replies separately. Startup configuration
42/// stays in [`ProgramOptions`](super::ProgramOptions). Read the current choices
43/// through [`Program::state`](super::Program::state), or clone them for an owned
44/// snapshot. Neither operation sends queries or waits for replies.
45#[derive(Debug, Clone, PartialEq, Eq)]
46pub struct State {
47 /// Cursor style.
48 pub(super) cursor_style: CursorStyle,
49 /// Requested mouse tracking, or `None` when mouse tracking is disabled.
50 pub(super) mouse: Option<MouseTracking>,
51 /// Bracketed paste mode.
52 pub(super) bracketed_paste: bool,
53 /// Focus in/out reporting (DECSET 1004).
54 pub(super) focus_events: bool,
55 /// Color-scheme update notifications (DEC 2031). When `true`, the
56 /// terminal sends unsolicited reports as the user/OS toggles the
57 /// dark/light scheme. Reports the dark/light preference only, not the
58 /// actual colors.
59 pub(super) color_scheme_updates: bool,
60 /// Terminal visibility reports (DEC 2033). When `true`, the terminal
61 /// sends an unsolicited report whenever the view becomes observable or
62 /// stops being observable, surfaced as [`Event::Visibility`].
63 ///
64 /// [`Event::Visibility`]: crate::event::Event::Visibility
65 pub(super) visibility_reports: bool,
66 /// In-band resize notifications (DEC 2048). When `true`, the
67 /// terminal sends a `CSI 48 ; … t` report whenever the surface
68 /// changes size, surfaced as [`Event::Resize`].
69 ///
70 /// [`Event::Resize`]: crate::event::Event::Resize
71 pub(super) in_band_resize: bool,
72 /// Window title set via [`OSC 2`] (or [`OSC 0`], which sets both this and
73 /// [`icon_name`](Self::icon_name)). `None` when no
74 /// [`set_window_title`](super::Program::set_window_title) or
75 /// [`set_title`](super::Program::set_title) override has been set.
76 ///
77 /// [`OSC 2`]: crate::ansi::title::write_window_title
78 /// [`OSC 0`]: crate::ansi::title::write_window_title_and_icon
79 pub(super) window_title: Option<String>,
80 /// Icon name set via [`OSC 1`] (or [`OSC 0`], which sets both this and
81 /// [`window_title`](Self::window_title)). `None` when no
82 /// [`set_icon_title`](super::Program::set_icon_title) or
83 /// [`set_title`](super::Program::set_title) override has been set.
84 ///
85 /// [`OSC 1`]: crate::ansi::title::write_icon_name
86 /// [`OSC 0`]: crate::ansi::title::write_window_title_and_icon
87 pub(super) icon_name: Option<String>,
88 /// Default foreground color override. `Some(c)` when the facade has
89 /// emitted `OSC 10` to install `c`; `None` when the terminal is
90 /// using its built-in default. Drives `OSC 110` on reset and
91 /// re-emission on restore.
92 pub(super) foreground_color: Option<Color>,
93 /// Default background color override. See [`State::foreground_color`].
94 pub(super) background_color: Option<Color>,
95 /// Cursor color override. See [`State::foreground_color`].
96 pub(super) cursor_color: Option<Color>,
97 /// Indexed palette overrides set via `OSC 4`, keyed by palette index.
98 /// Drives `OSC 104 ; index` on reset and re-emission on restore.
99 pub(super) palette: BTreeMap<u8, Color>,
100 /// Active xterm modifyOtherKeys mode (`CSI > 4 ; n m`). Drives
101 /// `CSI > 4 m` on reset and re-emission on restore.
102 pub(super) modify_other_keys: ModifyOtherKeysMode,
103 /// Whether the application selected modifyOtherKeys, including disabled.
104 pub(super) modify_other_keys_chosen: bool,
105 /// Pointer (mouse cursor) shape override set via `OSC 22`. `None` when
106 /// using the terminal default. Drives the `OSC 22` reset on reset and
107 /// re-emission on restore.
108 pub(super) pointer_shape: Option<String>,
109 /// Progress reported via `OSC 9;4`. `None` when no progress is being
110 /// reported. Drives the `OSC 9;4;0` removal on reset and re-emission on
111 /// restore.
112 pub(super) progress: Option<ProgressState>,
113 /// Active Kitty keyboard enhancement flag set. The stack is
114 /// per-screen-buffer, so the program re-emits this onto whichever buffer
115 /// becomes active. An empty set explicitly disables enhancements when
116 /// `kitty_keyboard_chosen` is true.
117 pub(super) kitty_keyboard: KittyKeyboardFlags,
118 /// An explicit choice or discovery has settled the keyboard flags.
119 pub(super) kitty_keyboard_chosen: bool,
120 /// Whether the facade has put the terminal on the alternate screen buffer
121 /// (DECSET 1049). Mirrors [`Screen::fullscreen`](crate::screen::Screen::fullscreen)
122 /// while the app drives the buffer through
123 /// [`enter_alt_screen`](super::Program::enter_alt_screen) /
124 /// [`exit_alt_screen`](super::Program::exit_alt_screen).
125 pub(super) alt_screen: bool,
126 /// Whether the terminal cursor is visible (DECTCEM). Mirrors
127 /// [`Screen::cursor_visible`](crate::screen::Screen::cursor_visible).
128 /// Starts `true`: a terminal shows its cursor until told otherwise.
129 pub(super) cursor_visible: bool,
130 /// Whether grapheme-cluster mode is on (DEC 2027). Mirrors
131 /// [`Screen::grapheme_clusters`](crate::screen::Screen::grapheme_clusters).
132 pub(super) grapheme_clusters: bool,
133 /// Application-selected Backarrow mode, restored after a pause.
134 /// The decoder separately tracks the current mode from incoming reports.
135 pub(super) backspace_mode: bool,
136 /// Modes selected through the program's mode methods or adopted on the
137 /// application's behalf.
138 ///
139 /// The mode fields above cannot carry this: `false` reads the same whether
140 /// the app turned the mode off or never mentioned it. Discovery adopts a
141 /// preferred mode only for a mode absent from this set, so an explicit
142 /// `disable_*` issued before the terminal ever reports is not quietly
143 /// undone by the report when it arrives.
144 pub(super) chosen: BTreeSet<Mode>,
145}
146
147impl Default for State {
148 fn default() -> Self {
149 Self {
150 cursor_style: CursorStyle::Default,
151 mouse: None,
152 bracketed_paste: false,
153 focus_events: false,
154 color_scheme_updates: false,
155 visibility_reports: false,
156 in_band_resize: false,
157 window_title: None,
158 icon_name: None,
159 foreground_color: None,
160 background_color: None,
161 cursor_color: None,
162 palette: BTreeMap::new(),
163 modify_other_keys: ModifyOtherKeysMode::Disabled,
164 modify_other_keys_chosen: false,
165 pointer_shape: None,
166 progress: None,
167 kitty_keyboard: KittyKeyboardFlags::empty(),
168 kitty_keyboard_chosen: false,
169 alt_screen: false,
170 cursor_visible: true,
171 grapheme_clusters: false,
172 backspace_mode: false,
173 chosen: BTreeSet::new(),
174 }
175 }
176}
177
178impl State {
179 /// Cursor appearance selected through `Program`, or the terminal default.
180 pub fn cursor_style(&self) -> CursorStyle {
181 self.cursor_style
182 }
183
184 /// Requested cursor color override, or the terminal default.
185 pub fn cursor_color(&self) -> Option<Color> {
186 self.cursor_color
187 }
188
189 /// Requested hardware cursor visibility. Initially `true`.
190 pub fn cursor_visible(&self) -> bool {
191 self.cursor_visible
192 }
193
194 /// Whether the alternate screen buffer is selected.
195 pub fn alt_screen(&self) -> bool {
196 self.alt_screen
197 }
198
199 /// Selected mouse policy. `None` means no choice; `Some(None)` disables
200 /// tracking. `Some(Some(empty()))` requests basic button tracking.
201 pub fn mouse(&self) -> Option<Option<MouseTracking>> {
202 self.chosen
203 .contains(&Mode::MOUSE_NORMAL)
204 .then_some(self.mouse)
205 }
206
207 /// Selected Kitty flags. `None` means no choice; `Some(empty())` explicitly
208 /// disables all enhancements. The selection follows buffer transitions.
209 pub fn kitty_keyboard(&self) -> Option<KittyKeyboardFlags> {
210 self.kitty_keyboard_chosen.then_some(self.kitty_keyboard)
211 }
212
213 /// Selected modifyOtherKeys mode, including an explicit disabled choice.
214 /// `None` means the application has made no choice.
215 pub fn modify_other_keys(&self) -> Option<ModifyOtherKeysMode> {
216 self.modify_other_keys_chosen
217 .then_some(self.modify_other_keys)
218 }
219
220 /// Selected value of a tracked boolean mode.
221 ///
222 /// `None` means no choice was made through `Program`, or the mode is
223 /// outside this record. `Some(false)` is an explicit reset. Supported modes
224 /// are alternate screen (1049), cursor visibility (25), bracketed paste
225 /// (2004), focus (1004), color scheme (2031), visibility reports (2033),
226 /// in-band resize (2048), Unicode core (2027), and Backarrow (67).
227 ///
228 /// This records application choices and adopted preferences, independently
229 /// of the last report in [`Capabilities::mode`].
230 pub fn mode(&self, mode: Mode) -> Option<bool> {
231 if !self.chosen.contains(&mode) {
232 return None;
233 }
234 Some(match mode {
235 Mode::ALT_SCREEN_SAVE_CURSOR => self.alt_screen,
236 Mode::CURSOR_VISIBLE => self.cursor_visible,
237 Mode::BRACKETED_PASTE => self.bracketed_paste,
238 Mode::FOCUS => self.focus_events,
239 Mode::LIGHT_DARK => self.color_scheme_updates,
240 Mode::VISIBILITY_REPORTS => self.visibility_reports,
241 Mode::IN_BAND_RESIZE => self.in_band_resize,
242 Mode::UNICODE_CORE => self.grapheme_clusters,
243 Mode::BACKARROW_KEY => self.backspace_mode,
244 _ => return None,
245 })
246 }
247
248 /// Requested default foreground override.
249 pub fn foreground_color(&self) -> Option<Color> {
250 self.foreground_color
251 }
252
253 /// Requested default background override.
254 pub fn background_color(&self) -> Option<Color> {
255 self.background_color
256 }
257
258 /// Requested indexed palette overrides.
259 pub fn palette(&self) -> &BTreeMap<u8, Color> {
260 &self.palette
261 }
262
263 /// Requested window title override.
264 pub fn window_title(&self) -> Option<&str> {
265 self.window_title.as_deref()
266 }
267
268 /// Requested icon name override.
269 pub fn icon_name(&self) -> Option<&str> {
270 self.icon_name.as_deref()
271 }
272
273 /// Requested pointer shape override.
274 pub fn pointer_shape(&self) -> Option<&str> {
275 self.pointer_shape.as_deref()
276 }
277
278 /// Requested progress indicator.
279 pub fn progress(&self) -> Option<ProgressState> {
280 self.progress
281 }
282}
283
284/// What the terminal told us about itself.
285///
286/// This holds the replies themselves, not a summary of them: the
287/// [`ModeSetting`] reported for every mode that was asked about, the raw
288/// device-attribute lists, the reported colors, and so on. A reply that says
289/// "I do not recognize that" is recorded too, so `None` from an accessor
290/// generally means the terminal never answered, which is different from
291/// answering no. [`termcap`](Self::termcap) is the exception, folding both
292/// into `None`; use [`termcap_reports`](Self::termcap_reports) to separate
293/// them.
294///
295/// Capability values are what the terminal reported, not what the facade
296/// told it. The two are easy to confuse where both exist: a
297/// [`background_color`](Self::background_color) recorded here stays the
298/// terminal's own default even after
299/// [`set_background_color`](super::Program::set_background_color) overrides
300/// it.
301///
302/// Capability values come from replies, so questions the environment can
303/// also answer are deliberately absent. Direct-color support is the example:
304/// `COLORTERM` and `TERM` establish it as readily as an XTGETTCAP reply does.
305/// The answer is [`Screen::color_profile`](crate::screen::Screen::color_profile),
306/// which folds in both, and what remains here is the reply itself via
307/// [`supports_termcap`](Self::supports_termcap).
308///
309/// The application owns this record.
310/// [`Probe::query_capabilities`](super::Probe::query_capabilities) sends the
311/// queries and returns a record for that batch. The record also keeps the
312/// modes queried by the probe. Pass events from your own read loop, sync or
313/// async, to [`record`](Self::record). The record keeps
314/// each core reply about the terminal. The application detects the final
315/// Primary DA event, then calls
316/// [`apply_defaults`](super::Program::apply_defaults) to act on the record.
317///
318/// The record accepts every reply that describes the terminal, whichever way
319/// the question was put. An individual `request_*` method fills a single
320/// entry, and a few reports arrive unprompted once their mode is on, such as
321/// a color-scheme change under DEC mode 2031. A [`Default`] record has no
322/// pending measurement, so it collects identified replies.
323#[derive(Debug, Clone, Default, PartialEq, Eq)]
324pub struct Capabilities {
325 /// The batch's width probe report has not arrived yet.
326 pub(super) eaw_probe: bool,
327 pub(super) eaw_width: Option<u8>,
328 pub(super) queried_modes: Vec<Mode>,
329 pub(super) modes: BTreeMap<Mode, ModeSetting>,
330 pub(super) primary_device_attributes: Option<Vec<Option<u32>>>,
331 pub(super) secondary_device_attributes: Option<Vec<Option<u32>>>,
332 pub(super) tertiary_device_attributes: Option<String>,
333 pub(super) kitty_keyboard: Option<KittyKeyboardFlags>,
334 pub(super) modify_other_keys: Option<ModifyOtherKeysMode>,
335 pub(super) terminal_name: Option<String>,
336 pub(super) termcap: BTreeMap<String, Option<String>>,
337 pub(super) foreground_color: Option<Color>,
338 pub(super) background_color: Option<Color>,
339 pub(super) cursor_color: Option<Color>,
340 pub(super) palette: BTreeMap<u8, Color>,
341 pub(super) color_scheme: Option<ColorScheme>,
342 pub(super) program_status: Option<ProgramStatusCapabilities>,
343}
344
345impl Capabilities {
346 /// Update the core results from `event`.
347 ///
348 /// The event is only read, so your loop still handles it as usual. Pass
349 /// each relevant event once, in arrival order. The application owns the
350 /// deadline and detects Primary DA. Pass Primary DA here before ending
351 /// initial collection so it closes the pending width measurement.
352 ///
353 /// Cursor and Primary DA replies carry no request ID. The record takes
354 /// the first cursor report routed here as the pending width measurement.
355 /// The caller routes cursor reports according to its query order. See
356 /// [`Probe::query_capabilities`](super::Probe::query_capabilities) for the
357 /// request order that this needs.
358 ///
359 /// Keep calling this after the batch is complete to record late replies,
360 /// mid-session `request_*` replies, and unprompted reports. A DECRPSS
361 /// [`Event::SettingReport`] is not recorded: it does not say which
362 /// setting it answers, so match it against your own DECRQSS request.
363 pub fn record(&mut self, event: &Event) {
364 match *event {
365 Event::ModeReport { mode, setting } => {
366 // A "not recognized" report is a definite no, which is not
367 // the same as silence, so it is recorded too.
368 self.modes.insert(mode, setting);
369 }
370 Event::KittyKeyboardEnhancements(flags) => self.kitty_keyboard = Some(flags),
371 // Any modifyOtherKeys report (`CSI > 4 ; n m`) answers the
372 // query, so a reply means the terminal recognizes the feature.
373 Event::ModifyOtherKeys(mode) => self.modify_other_keys = Some(mode),
374 Event::PrimaryDeviceAttributes(ref attrs) => {
375 // Stored unparsed: the attribute numbers are a terminal-author
376 // extension point, so callers test the ones they care about.
377 self.primary_device_attributes = Some(attrs.clone());
378 // Close the measurement before later cursor requests arrive.
379 self.eaw_probe = false;
380 }
381 Event::SecondaryDeviceAttributes(ref attrs) => {
382 self.secondary_device_attributes = Some(attrs.clone());
383 }
384 Event::TertiaryDeviceAttributes(ref id) => {
385 self.tertiary_device_attributes = Some(id.clone());
386 }
387 Event::TerminalName(ref report) => self.terminal_name = Some(report.clone()),
388 Event::ProgramStatus(ref report) => self.program_status = Some(report.clone()),
389 Event::ForegroundColor(color) => self.foreground_color = Some(color),
390 Event::BackgroundColor(color) => self.background_color = Some(color),
391 Event::CursorColor(color) => self.cursor_color = Some(color),
392 Event::PaletteColor { index, color } => {
393 self.palette.insert(index, color);
394 }
395 // Mode 2031 keeps sending these as the scheme changes, so the
396 // record is the current scheme rather than a one-time answer.
397 Event::ColorScheme(scheme) => self.color_scheme = Some(scheme),
398 Event::CursorPosition { position, .. } if self.eaw_probe => {
399 self.eaw_probe = false;
400 // CPR coordinates are zero-based, so x is the measured
401 // advance from the carriage return.
402 if matches!(position.x, 1 | 2) {
403 self.eaw_width = Some(position.x as u8);
404 }
405 }
406 Event::Termcap {
407 recognized,
408 ref entries,
409 } => {
410 // A failure reply echoes the requested names, so it is
411 // recorded as an explicit "not supported" rather than
412 // dropped.
413 for (name, value) in entries {
414 self.termcap.insert(
415 name.clone(),
416 recognized.then(|| value.clone().unwrap_or_default()),
417 );
418 }
419 }
420 _ => {}
421 }
422 }
423
424 /// Measured East Asian Ambiguous width: one or two terminal columns.
425 ///
426 /// `None` means the probe has not returned a valid width.
427 /// [`Probe::query_capabilities`](super::Probe::query_capabilities)
428 /// measures U+2026 (HORIZONTAL ELLIPSIS) when selected.
429 /// [`Program::apply_defaults`](super::Program::apply_defaults)
430 /// applies the result to [`Screen::set_ambiguous_wide`](crate::screen::Screen::set_ambiguous_wide).
431 pub fn eaw_width(&self) -> Option<u8> {
432 self.eaw_width
433 }
434
435 /// Modes queried by the probe that created this record, in request order.
436 ///
437 /// This list includes duplicates and contains only this probe's mode
438 /// queries. Apple Terminal skips the
439 /// probe's mode queries, so its list is empty. A default record has an
440 /// empty list. Recording later replies does not change this list.
441 pub fn queried_modes(&self) -> &[Mode] {
442 &self.queried_modes
443 }
444
445 /// The [`ModeSetting`] the terminal reported for `mode`, or `None` if it
446 /// never reported on that mode.
447 ///
448 /// Use this when the distinction matters: [`ModeSetting::Set`] means the
449 /// mode is currently on, [`ModeSetting::PermanentlySet`] means it cannot
450 /// be turned off, and [`ModeSetting::NotRecognized`] is a definite "no"
451 /// rather than silence.
452 pub fn mode(&self, mode: Mode) -> Option<ModeSetting> {
453 self.modes.get(&mode).copied()
454 }
455
456 /// Whether the terminal reported `mode` as available, in any state.
457 ///
458 /// ```ignore
459 /// use uncurses::ansi::mode::Mode;
460 ///
461 /// if caps.supports(Mode::MOUSE_SGR_PIXEL) {
462 /// // pixel-accurate mouse reporting is available
463 /// }
464 /// ```
465 pub fn supports(&self, mode: Mode) -> bool {
466 self.mode(mode).is_some_and(ModeSetting::is_available)
467 }
468
469 /// Every mode report recorded so far, keyed by mode.
470 pub fn modes(&self) -> &BTreeMap<Mode, ModeSetting> {
471 &self.modes
472 }
473
474 /// The raw Primary DA (`CSI c`) attribute list, or `None` if the terminal
475 /// never answered. Entries are `None` where the terminal sent an empty
476 /// parameter.
477 ///
478 /// The first parameter is the terminal's architectural service class, not
479 /// a capability: `6` identifies a VT102 and `64` a VT420. Capability
480 /// numbers live in the parameters after it in modern replies. VT100-style
481 /// replies use different meanings for those parameters. Modern terminals
482 /// may also report extensions with class `61`.
483 ///
484 /// The capability numbers are assigned by DEC and extended by terminal
485 /// authors, so they are reported unparsed.
486 pub fn primary_device_attributes(&self) -> Option<&[Option<u32>]> {
487 self.primary_device_attributes.as_deref()
488 }
489
490 fn primary_da_has(&self, attribute: u32) -> bool {
491 matches!(
492 self.primary_device_attributes.as_deref(),
493 Some([Some(61..), attributes @ ..]) if attributes.contains(&Some(attribute))
494 )
495 }
496
497 /// Whether a Primary DA reply with class 61 or greater reports OSC 52 writes.
498 ///
499 /// This reports the clipboard extension for the system clipboard or an
500 /// empty selector. It does not confirm clipboard reads. `false` means
501 /// support is unconfirmed.
502 pub fn clipboard_write(&self) -> bool {
503 self.primary_da_has(52)
504 }
505
506 /// The raw Secondary DA (`CSI > c`) attribute list, or `None` if the
507 /// terminal never answered. Conventionally terminal type, firmware
508 /// version, and hardware option, but the meaning of each entry varies by
509 /// terminal, so it is reported unparsed.
510 pub fn secondary_device_attributes(&self) -> Option<&[Option<u32>]> {
511 self.secondary_device_attributes.as_deref()
512 }
513
514 /// The Tertiary DA (`CSI = c`) terminal unit ID, or `None` if the
515 /// terminal never answered.
516 pub fn tertiary_device_attributes(&self) -> Option<&str> {
517 self.tertiary_device_attributes.as_deref()
518 }
519
520 /// Accepted OSC 7501 states and kinds, or `None` before a reply.
521 ///
522 /// `None` means support remains unconfirmed. A reply confirms support
523 /// even when both lists are empty. The decoder supplies fixed defaults
524 /// for absent fields and preserves names from protocol extensions.
525 ///
526 /// Send a query with [`Program::request_program_status`](super::Program::request_program_status).
527 pub fn program_status(&self) -> Option<&ProgramStatusCapabilities> {
528 self.program_status.as_ref()
529 }
530
531 /// The Kitty keyboard enhancements the terminal reported, or `None` if it
532 /// never answered `CSI ? u`. An answer of
533 /// [`empty`](KittyKeyboardFlags::empty) means the protocol is supported
534 /// with no enhancements currently active.
535 pub fn kitty_keyboard(&self) -> Option<KittyKeyboardFlags> {
536 self.kitty_keyboard
537 }
538
539 /// The xterm modifyOtherKeys mode the terminal reported, or `None` if it
540 /// never answered `CSI ? 4 m`.
541 pub fn modify_other_keys(&self) -> Option<ModifyOtherKeysMode> {
542 self.modify_other_keys
543 }
544
545 /// The terminal's self-reported name from XTVERSION (for example
546 /// `"XTerm(380)"`), or `None` if it never answered.
547 pub fn terminal_name(&self) -> Option<&str> {
548 self.terminal_name.as_deref()
549 }
550
551 /// The value the terminal reported for the XTGETTCAP capability `name`,
552 /// or `None` if it reported the capability as unsupported or was never
553 /// asked. Boolean capabilities report an empty string, so use
554 /// [`supports_termcap`](Self::supports_termcap) to test for presence.
555 pub fn termcap(&self, name: &str) -> Option<&str> {
556 self.termcap.get(name)?.as_deref()
557 }
558
559 /// Whether the terminal reported the XTGETTCAP capability `name` as
560 /// supported. `false` both for a capability reported unsupported and for
561 /// one never asked about; tell them apart with
562 /// [`termcap_reports`](Self::termcap_reports).
563 pub fn supports_termcap(&self, name: &str) -> bool {
564 matches!(self.termcap.get(name), Some(Some(_)))
565 }
566
567 /// Every XTGETTCAP reply recorded so far, keyed by capability name. A
568 /// value of `None` is the terminal reporting that capability as
569 /// unsupported, which is different from the key being absent.
570 pub fn termcap_reports(&self) -> &BTreeMap<String, Option<String>> {
571 &self.termcap
572 }
573
574 /// The terminal's default foreground color (`OSC 10`), or `None` if it
575 /// never answered.
576 pub fn foreground_color(&self) -> Option<Color> {
577 self.foreground_color
578 }
579
580 /// The terminal's default background color (`OSC 11`), or `None` if it
581 /// never answered.
582 pub fn background_color(&self) -> Option<Color> {
583 self.background_color
584 }
585
586 /// The terminal's cursor color (`OSC 12`), or `None` if it never
587 /// answered.
588 pub fn cursor_color(&self) -> Option<Color> {
589 self.cursor_color
590 }
591
592 /// The color the terminal reported for palette entry `index`
593 /// (`OSC 4 ; index ; ?`), or `None` if it never answered for that entry.
594 pub fn palette_color(&self, index: u8) -> Option<Color> {
595 self.palette.get(&index).copied()
596 }
597
598 /// Every palette color reported so far, keyed by index.
599 pub fn palette(&self) -> &BTreeMap<u8, Color> {
600 &self.palette
601 }
602
603 /// Whether the terminal is in its dark or light scheme (DEC mode 2031),
604 /// or `None` if it never reported one. Updated as the scheme changes
605 /// while [`enable_color_scheme_updates`](super::Program::enable_color_scheme_updates)
606 /// is on. This is the terminal's own preference flag, which a terminal
607 /// can report independently of the colors it uses; when it is absent,
608 /// [`background_color`](Self::background_color) is the fallback.
609 pub fn color_scheme(&self) -> Option<ColorScheme> {
610 self.color_scheme
611 }
612}