Skip to main content

uncurses/program/
modes.rs

1//! Non-render terminal/input mode toggles for the [`Program`] facade —
2//! cursor style, mouse tracking, bracketed paste, focus reporting,
3//! color-scheme update reports, in-band resize reports, window title,
4//! progress reports, and the default foreground/background/cursor colors.
5//!
6//! Each setter emits its escape bytes through the owned renderer and
7//! flushes immediately, so the mode change takes effect on the terminal
8//! right away and the call returns [`io::Result<()>`](std::io::Result). A
9//! setter whose tracked value is unchanged is a no-op and performs no I/O.
10//!
11//! [`Program`]: super::Program
12
13use std::io::{self, Write};
14
15use crate::ansi::{self, color, cursor, kitty, mode, progress, status, xterm};
16use crate::color::Color;
17use crate::event::Input;
18
19use super::MouseTracking;
20use super::Program;
21use super::ProgressState;
22use crate::ansi::cursor::{CursorShape, CursorStyle};
23
24/// DEC private modes for the mouse tracking modes and encodings this
25/// library supports. Reset together to unconditionally turn mouse
26/// reporting off.
27const MOUSE_MODES: &[mode::Mode] = &[
28    mode::Mode::MOUSE_X10,
29    mode::Mode::MOUSE_NORMAL,
30    mode::Mode::MOUSE_BUTTON,
31    mode::Mode::MOUSE_ANY,
32    mode::Mode::MOUSE_SGR,
33    mode::Mode::MOUSE_SGR_PIXEL,
34];
35
36impl<I: Input, O: Write> Program<I, O> {
37    /// Send a complete OSC 7501 program status report and flush.
38    ///
39    /// Reports replace the addressed record. Include its app, title, and
40    /// message on every update when they should remain present. Use
41    /// [`ProgramStatusState::CLEAR`](super::ProgramStatusState::CLEAR) to
42    /// remove the record and its descendants; a root clear removes all records.
43    ///
44    /// The terminal owns record lifetime. Pause, resume, screen switches,
45    /// and finish leave these reports unchanged, so done and error records
46    /// can survive exit. Explicit discovery is optional; unsupported
47    /// terminals ignore reports.
48    ///
49    /// Invalid fields return [`io::ErrorKind::InvalidInput`] before output.
50    /// See [`write_program_status`](ansi::program_status::write_program_status)
51    /// for validation and protocol limits.
52    pub fn set_program_status(&mut self, report: &super::ProgramStatus<'_>) -> io::Result<()> {
53        ansi::program_status::write_program_status(&mut self.screen, report)?;
54        self.screen.flush()
55    }
56
57    /// Query OSC 7501 support and flush, without waiting for a reply.
58    ///
59    /// The reply is [`Event::ProgramStatus`](crate::event::Event::ProgramStatus),
60    /// with the accepted state and kind names. Event observation records
61    /// support and both lists in [`Capabilities`](super::Capabilities).
62    /// To include this query in [`Self::query_capabilities`], enable
63    /// [`ProgramOptions::query_program_status`](super::ProgramOptions::query_program_status).
64    /// Initialization sends no queries.
65    pub fn request_program_status(&mut self) -> io::Result<()> {
66        self.screen
67            .write_all(ansi::program_status::REQUEST_PROGRAM_STATUS)?;
68        self.screen.flush()
69    }
70
71    /// Set the cursor shape and blinking state (`DECSCUSR`) and flush.
72    ///
73    /// * `shape` — the visual cursor shape ([`Block`](CursorShape::Block),
74    ///   [`Underline`](CursorShape::Underline), or [`Bar`](CursorShape::Bar)).
75    /// * `blinking` — whether the cursor blinks.
76    pub fn set_cursor_style(&mut self, shape: CursorShape, blinking: bool) -> io::Result<()> {
77        let style = CursorStyle::new(shape, blinking);
78        cursor::write_cursor_style(&mut self.screen, style)?;
79        self.state.cursor_style = style;
80        self.screen.flush()
81    }
82
83    /// Hand the cursor style back to the terminal (`DECSCUSR` parameter 0)
84    /// and flush.
85    ///
86    /// Every terminal surveyed for this reads its own configuration here:
87    /// kitty, foot, rio, Alacritty, wezterm, VTE, Konsole and Windows
88    /// Terminal all restore the shape their user chose. xterm documents the
89    /// parameter as a blinking block instead, so what appears is the
90    /// terminal's to decide either way.
91    ///
92    /// That makes this the portable way to undo
93    /// [`set_cursor_style`](Self::set_cursor_style) without knowing what
94    /// came before. To put back the cursor *this session* found rather than
95    /// the one the user configured, read it with
96    /// [`request_cursor_style`](Self::request_cursor_style) before changing
97    /// it and set that style again.
98    pub fn reset_cursor_style(&mut self) -> io::Result<()> {
99        cursor::write_cursor_style(&mut self.screen, CursorStyle::Default)?;
100        self.state.cursor_style = CursorStyle::Default;
101        self.screen.flush()
102    }
103
104    /// Ring the terminal bell (`BEL`) and flush.
105    pub fn beep(&mut self) -> io::Result<()> {
106        self.screen.write_all(b"\x07")?;
107        self.screen.flush()
108    }
109
110    /// Set the pointer (mouse cursor) shape (`OSC 22`) and flush.
111    ///
112    /// `shape` is a pointer shape name such as `"default"`, `"text"`, or
113    /// `"pointer"`. The shape is recorded for save/restore.
114    pub fn set_pointer_shape(&mut self, shape: &str) -> io::Result<()> {
115        cursor::write_set_pointer_shape(&mut self.screen, shape)?;
116        self.state.pointer_shape = Some(shape.to_string());
117        self.screen.flush()
118    }
119
120    /// Reset the pointer (mouse cursor) shape to the terminal default
121    /// (`OSC 22 ; default`) and flush.
122    ///
123    /// Uses the explicit `"default"` shape name rather than an empty one: some
124    /// terminals don't treat an empty `OSC 22` as a reset.
125    pub fn reset_pointer_shape(&mut self) -> io::Result<()> {
126        cursor::write_set_pointer_shape(&mut self.screen, "default")?;
127        self.state.pointer_shape = None;
128        self.screen.flush()
129    }
130
131    /// Report progress to the terminal (`OSC 9;4`) and flush.
132    ///
133    /// Terminals that support it show the progress in the taskbar, tab, or
134    /// window chrome; the rest ignore the sequence. Percentages are clamped
135    /// to `0..=100`.
136    ///
137    /// The state is recorded for save/restore: it is removed on a shell
138    /// handoff ([`pause`](Self::pause), Unix `suspend`,
139    /// [`finish`](Self::finish)) and re-reported by
140    /// [`resume`](Self::resume). Take it down with
141    /// [`reset_progress_state`](Self::reset_progress_state).
142    pub fn set_progress_state(&mut self, progress: ProgressState) -> io::Result<()> {
143        progress.write(&mut self.screen)?;
144        self.state.progress = Some(progress);
145        self.screen.flush()
146    }
147
148    /// Remove the progress report (`OSC 9;4;0`) and flush.
149    pub fn reset_progress_state(&mut self) -> io::Result<()> {
150        self.screen.write_all(progress::RESET_PROGRESS_BAR)?;
151        self.state.progress = None;
152        self.screen.flush()
153    }
154
155    /// Enable mouse tracking and flush.
156    ///
157    /// This emits exactly what is asked for and does not consult terminal
158    /// [`capabilities`](Self::capabilities). Unsupported modes are ignored by
159    /// the terminal, and because the mode requests are mutually exclusive, each
160    /// terminal settles on the most capable variant it understands:
161    ///
162    /// * Tracking: button (`1000`) and button-event (`1002`) are always
163    ///   requested, so a terminal reports drag where it can and plain clicks
164    ///   otherwise. With [`MouseTracking::MOTION`], any-event tracking (`1003`)
165    ///   is added on top, so motion without a button held is reported where
166    ///   supported.
167    /// * Encoding: SGR (`1006`) is always requested, since the legacy byte
168    ///   encoding caps coordinates at 223 and SGR is universally supported.
169    ///   With [`MouseTracking::PIXELS`], SGR-pixel (`1016`) is added; terminals
170    ///   that support it report pixel coordinates, and the rest fall back to
171    ///   SGR cell coordinates.
172    ///
173    /// Pass [`MouseTracking::empty()`] for basic button tracking with no
174    /// extras. To turn mouse tracking off, call [`disable_mouse`](Self::disable_mouse).
175    ///
176    /// To learn which variant a terminal actually chose, read
177    /// [`capabilities`](Self::capabilities) (for example
178    /// [`supports(Mode::MOUSE_SGR_PIXEL)`](super::Capabilities::supports) to
179    /// tell whether pixels or cells will arrive). When pixel reporting is active, a
180    /// [`Mouse`](crate::event::Mouse) event's pixel coordinates can be converted
181    /// to cells with [`mouse_pixels_to_cells`](Self::mouse_pixels_to_cells).
182    ///
183    /// Mouse coordinates are physical screen coordinates. Inline, follow this
184    /// with [`request_origin`](Self::request_origin) to learn where the managed
185    /// area sits, so [`mouse_to_origin`](Self::mouse_to_origin) can map them
186    /// into it.
187    ///
188    /// The request is recorded for save/restore.
189    pub fn enable_mouse(&mut self, tracking: MouseTracking) -> io::Result<()> {
190        // Drop any prior tracking first so modes don't stack ambiguously.
191        mode::write_reset_mode(&mut self.screen, MOUSE_MODES)?;
192        self.write_mouse_modes(tracking, true)?;
193        self.state.mouse = Some(tracking);
194        self.screen.flush()
195    }
196
197    /// Disable all mouse tracking modes and encodings, and flush.
198    pub fn disable_mouse(&mut self) -> io::Result<()> {
199        mode::write_reset_mode(&mut self.screen, MOUSE_MODES)?;
200        self.state.mouse = None;
201        self.screen.flush()
202    }
203
204    /// Set or reset the mouse tracking modes and encoding for the given
205    /// tracking flags. `enable` selects set vs reset. The modes are emitted in
206    /// ascending order so that, where the requests are mutually exclusive, the
207    /// most capable supported variant wins (`1003` over `1002`/`1000`, `1016`
208    /// over `1006`).
209    fn write_mouse_modes(&mut self, tracking: MouseTracking, enable: bool) -> io::Result<()> {
210        // Always request plain and button-event tracking as a fallback pair,
211        // adding any-event tracking on top when motion is requested.
212        let mut modes = vec![mode::Mode::MOUSE_NORMAL, mode::Mode::MOUSE_BUTTON];
213        if tracking.contains(MouseTracking::MOTION) {
214            modes.push(mode::Mode::MOUSE_ANY);
215        }
216        // Always request SGR encoding; add SGR-pixel on top when pixels are
217        // requested. Terminals without pixel support fall back to SGR cells.
218        modes.push(mode::Mode::MOUSE_SGR);
219        if tracking.contains(MouseTracking::PIXELS) {
220            modes.push(mode::Mode::MOUSE_SGR_PIXEL);
221        }
222        if enable {
223            mode::write_set_mode(&mut self.screen, &modes)
224        } else {
225            mode::write_reset_mode(&mut self.screen, &modes)
226        }
227    }
228
229    /// Enable bracketed paste mode (DEC private mode 2004) and flush.
230    pub fn enable_bracketed_paste(&mut self) -> io::Result<()> {
231        mode::Mode::BRACKETED_PASTE.set(&mut self.screen)?;
232        self.state.bracketed_paste = true;
233        self.screen.flush()
234    }
235
236    /// Disable bracketed paste mode (DEC private mode 2004) and flush.
237    pub fn disable_bracketed_paste(&mut self) -> io::Result<()> {
238        mode::Mode::BRACKETED_PASTE.reset(&mut self.screen)?;
239        self.state.bracketed_paste = false;
240        self.screen.flush()
241    }
242
243    /// Enable focus in/out reporting (DEC private mode 1004) and flush.
244    pub fn enable_focus_events(&mut self) -> io::Result<()> {
245        mode::Mode::FOCUS.set(&mut self.screen)?;
246        self.state.focus_events = true;
247        self.screen.flush()
248    }
249
250    /// Disable focus in/out reporting (DEC private mode 1004) and flush.
251    pub fn disable_focus_events(&mut self) -> io::Result<()> {
252        mode::Mode::FOCUS.reset(&mut self.screen)?;
253        self.state.focus_events = false;
254        self.screen.flush()
255    }
256
257    /// Enable color-scheme update notifications (DEC private mode 2031) and
258    /// flush. The terminal then sends a `CSI ? 997 ; {1|2} n` report
259    /// whenever the user or operating system switches between dark and
260    /// light schemes; these surface as [`Event::ColorScheme`]. The report
261    /// indicates only the dark/light preference, not the actual colors.
262    ///
263    /// [`Event::ColorScheme`]: crate::event::Event::ColorScheme
264    pub fn enable_color_scheme_updates(&mut self) -> io::Result<()> {
265        mode::Mode::LIGHT_DARK.set(&mut self.screen)?;
266        self.state.color_scheme_updates = true;
267        self.screen.flush()
268    }
269
270    /// Disable color-scheme update notifications (DEC private mode 2031) and
271    /// flush.
272    pub fn disable_color_scheme_updates(&mut self) -> io::Result<()> {
273        mode::Mode::LIGHT_DARK.reset(&mut self.screen)?;
274        self.state.color_scheme_updates = false;
275        self.screen.flush()
276    }
277
278    /// Enable terminal visibility reports (DEC private mode 2033) and flush.
279    /// The terminal then sends a `CSI ? 999 ; {1|2} n` report whenever the
280    /// view stops being observable or becomes observable again, which the
281    /// decoder surfaces as [`Event::Visibility`]. Being covered by another
282    /// window, scrolled out of a tab, or on a minimized window all count.
283    ///
284    /// The report is advisory and asymmetric: [`Visibility::Hidden`] means
285    /// the terminal knows nothing can be seen, so a render can be skipped,
286    /// while [`Visibility::Visible`] only means it may be observable. A
287    /// terminal that never reports is therefore treated as visible, which is
288    /// what makes ignoring this mode safe.
289    ///
290    /// Visibility is independent of focus: an unfocused window is usually
291    /// still visible.
292    ///
293    /// [`Event::Visibility`]: crate::event::Event::Visibility
294    /// [`Visibility::Hidden`]: crate::event::Visibility::Hidden
295    /// [`Visibility::Visible`]: crate::event::Visibility::Visible
296    pub fn enable_visibility_reports(&mut self) -> io::Result<()> {
297        mode::Mode::VISIBILITY_REPORTS.set(&mut self.screen)?;
298        self.state.visibility_reports = true;
299        self.screen.flush()
300    }
301
302    /// Disable terminal visibility reports (DEC private mode 2033) and flush.
303    pub fn disable_visibility_reports(&mut self) -> io::Result<()> {
304        mode::Mode::VISIBILITY_REPORTS.reset(&mut self.screen)?;
305        self.state.visibility_reports = false;
306        self.screen.flush()
307    }
308
309    /// Ask the terminal to report its visibility once and flush.
310    ///
311    /// The reply arrives as [`Event::Visibility`], the same event the
312    /// unsolicited reports use. This does not enable or disable
313    /// [`enable_visibility_reports`](Self::enable_visibility_reports), so it
314    /// is the way to read visibility without subscribing to changes. A
315    /// terminal that does not implement DEC private mode 2033 answers
316    /// nothing at all, so never block waiting for this reply.
317    ///
318    /// [`Event::Visibility`]: crate::event::Event::Visibility
319    pub fn request_visibility(&mut self) -> io::Result<()> {
320        self.screen
321            .write_all(status::REQUEST_VISIBILITY_REPORT)
322            .and_then(|()| self.screen.flush())
323    }
324
325    /// Enable in-band resize notifications (DEC private mode 2048) and
326    /// flush. The terminal then reports every surface size change in-band
327    /// as a `CSI 48 ; height ; width ; ypixel ; xpixel t` sequence, which
328    /// the decoder surfaces as [`Event::Resize`] — no `SIGWINCH` handler
329    /// required. The event source stops synthesizing resizes from `SIGWINCH`
330    /// while this is on, so a size change is reported once, not twice.
331    ///
332    /// Only call this after [`capabilities`](Self::capabilities) reports
333    /// [`supports(Mode::IN_BAND_RESIZE)`](super::Capabilities::supports); a
334    /// terminal that ignores the mode would otherwise leave you with no
335    /// resize events at all.
336    ///
337    /// [`Event::Resize`]: crate::event::Event::Resize
338    pub fn enable_in_band_resize(&mut self) -> io::Result<()> {
339        mode::Mode::IN_BAND_RESIZE.set(&mut self.screen)?;
340        self.state.in_band_resize = true;
341        self.state.chosen.insert(mode::Mode::IN_BAND_RESIZE);
342        self.source.lock().unwrap().set_handle_resize(false);
343        self.screen.flush()
344    }
345
346    /// Disable in-band resize notifications (DEC private mode 2048) and
347    /// flush, handing resize reporting back to the `SIGWINCH` path.
348    pub fn disable_in_band_resize(&mut self) -> io::Result<()> {
349        mode::Mode::IN_BAND_RESIZE.reset(&mut self.screen)?;
350        self.state.in_band_resize = false;
351        self.state.chosen.insert(mode::Mode::IN_BAND_RESIZE);
352        self.source.lock().unwrap().set_handle_resize(true);
353        self.screen.flush()
354    }
355
356    /// Set both the window title and icon name (`OSC 0`) and flush.
357    ///
358    /// An empty `title` clears both overrides, restoring the terminal's
359    /// defaults; the state is recorded as unset so teardown and resume skip
360    /// them. To set just one, use
361    /// [`set_window_title`](Self::set_window_title) (`OSC 2`) or
362    /// [`set_icon_title`](Self::set_icon_title) (`OSC 1`).
363    pub fn set_title(&mut self, title: &str) -> io::Result<()> {
364        ansi::title::write_window_title_and_icon(&mut self.screen, title)?;
365        let stored = (!title.is_empty()).then(|| title.to_string());
366        self.state.window_title = stored.clone();
367        self.state.icon_name = stored;
368        self.screen.flush()
369    }
370
371    /// Set the window title only (`OSC 2`) and flush.
372    ///
373    /// An empty `title` clears the override, restoring the terminal's default
374    /// window title. Unlike [`set_title`](Self::set_title) (`OSC 0`), this
375    /// leaves the icon name untouched.
376    pub fn set_window_title(&mut self, title: &str) -> io::Result<()> {
377        ansi::title::write_window_title(&mut self.screen, title)?;
378        self.state.window_title = (!title.is_empty()).then(|| title.to_string());
379        self.screen.flush()
380    }
381
382    /// Set the icon name only (`OSC 1`) and flush.
383    ///
384    /// An empty `title` clears the override, restoring the terminal's default
385    /// icon name. Unlike [`set_title`](Self::set_title) (`OSC 0`), this leaves
386    /// the window title untouched.
387    pub fn set_icon_title(&mut self, title: &str) -> io::Result<()> {
388        ansi::title::write_icon_name(&mut self.screen, title)?;
389        self.state.icon_name = (!title.is_empty()).then(|| title.to_string());
390        self.screen.flush()
391    }
392
393    /// Enter the alternate screen buffer (DECSET 1049) and flush.
394    ///
395    /// The managed area becomes the whole viewport: the screen switches to
396    /// absolute addressing and repaints in full on the next
397    /// [`render`](crate::screen::Screen::render). The normal buffer, its scrollback, and the
398    /// shell prompt are left untouched underneath and come back on
399    /// [`exit_alt_screen`](Self::exit_alt_screen).
400    ///
401    /// Cursor visibility and the Kitty keyboard stack are per-screen-buffer
402    /// on some terminals, so both are re-asserted on the newly active buffer.
403    pub fn enter_alt_screen(&mut self) -> io::Result<()> {
404        self.set_alt_screen(true)
405    }
406
407    /// Leave the alternate screen buffer (DECRST 1049) and flush, restoring
408    /// the normal buffer and its scrollback. The managed area becomes an
409    /// inline band again, addressed with relative moves.
410    pub fn exit_alt_screen(&mut self) -> io::Result<()> {
411        self.set_alt_screen(false)
412    }
413
414    /// Emit the alternate-screen switch and bring the screen and the
415    /// per-buffer modes with it. Always emits the mode; the bookkeeping runs
416    /// only on an actual transition.
417    pub(super) fn set_alt_screen(&mut self, enter: bool) -> io::Result<()> {
418        let changed = self.state.alt_screen != enter;
419        if changed && enter {
420            // Capture the inline anchor before the buffer switch hides it.
421            self.screen.save_cursor();
422        }
423        if enter {
424            mode::Mode::ALT_SCREEN_SAVE_CURSOR.set(&mut self.screen)?;
425        } else {
426            mode::Mode::ALT_SCREEN_SAVE_CURSOR.reset(&mut self.screen)?;
427        }
428        self.state.alt_screen = enter;
429        if changed {
430            self.screen.set_fullscreen(enter);
431            // Some terminals track DECTCEM and the Kitty keyboard stack per
432            // screen buffer, so the newly active one can disagree with what
433            // this program asked for. Re-assert both.
434            if !self.state.cursor_visible {
435                mode::Mode::CURSOR_VISIBLE.reset(&mut self.screen)?;
436            } else {
437                mode::Mode::CURSOR_VISIBLE.set(&mut self.screen)?;
438            }
439            if !self.state.kitty_keyboard.is_empty() {
440                kitty::write_set_kitty_keyboard(
441                    &mut self.screen,
442                    self.state.kitty_keyboard,
443                    kitty::KittyKeyboardMode::Set,
444                )?;
445            }
446        }
447        self.screen.flush()
448    }
449
450    /// Show the terminal cursor (DECSET 25) and flush.
451    ///
452    /// Also tells the screen, which hides a visible cursor around each frame's
453    /// cell diff so it does not dance across cells as the renderer
454    /// repositions it.
455    pub fn show_cursor(&mut self) -> io::Result<()> {
456        mode::Mode::CURSOR_VISIBLE.set(&mut self.screen)?;
457        self.state.cursor_visible = true;
458        self.screen.set_cursor_visible(true);
459        self.screen.flush()
460    }
461
462    /// Hide the terminal cursor (DECRST 25) and flush.
463    pub fn hide_cursor(&mut self) -> io::Result<()> {
464        mode::Mode::CURSOR_VISIBLE.reset(&mut self.screen)?;
465        self.state.cursor_visible = false;
466        self.screen.set_cursor_visible(false);
467        self.screen.flush()
468    }
469
470    /// Enable Unicode core / grapheme-cluster mode (DECSET 2027) and flush,
471    /// switching the screen to measure text per extended grapheme cluster so
472    /// it agrees with the terminal.
473    ///
474    /// Only call this after [`capabilities`](Self::capabilities) reports
475    /// [`supports(Mode::UNICODE_CORE)`](super::Capabilities::supports); a
476    /// terminal that ignores the mode still measures per code point, and the
477    /// two disagreeing misplaces every cell after the first cluster on a line.
478    pub fn enable_grapheme_clusters(&mut self) -> io::Result<()> {
479        mode::Mode::UNICODE_CORE.set(&mut self.screen)?;
480        self.state.grapheme_clusters = true;
481        self.state.chosen.insert(mode::Mode::UNICODE_CORE);
482        self.screen.set_grapheme_clusters(true);
483        self.screen.flush()
484    }
485
486    /// Disable grapheme-cluster mode (DECRST 2027) and flush, returning the
487    /// screen to per-code-point (wcwidth-style) measurement.
488    pub fn disable_grapheme_clusters(&mut self) -> io::Result<()> {
489        mode::Mode::UNICODE_CORE.reset(&mut self.screen)?;
490        self.state.grapheme_clusters = false;
491        self.state.chosen.insert(mode::Mode::UNICODE_CORE);
492        self.screen.set_grapheme_clusters(false);
493        self.screen.flush()
494    }
495
496    /// Return the decoder's current Backarrow mode (DECBKM).
497    ///
498    /// Reset (`false`) is the default assumption. [`Self::observe_event`]
499    /// applies recognized reports to future decoding. Already queued events
500    /// keep their decoded values.
501    pub fn backspace_mode(&self) -> bool {
502        self.source.lock().unwrap().backspace_mode()
503    }
504
505    /// Set Backarrow mode (DECBKM), flush, and update legacy key decoding.
506    ///
507    /// Set (`true`) selects BS (`0x08`) for ordinary Backspace; reset (`false`)
508    /// selects DEL (`0x7f`). [`DecoderFlags`](crate::event::DecoderFlags)
509    /// choose the ambiguous interpretations. Terminals can ignore the request.
510    ///
511    /// Teardown resets an application-selected mode. Resume reapplies this
512    /// selection. Received reports update decoding without changing that
513    /// lifecycle selection. The caller's options remain unchanged.
514    pub fn set_backspace_mode(&mut self, enabled: bool) -> io::Result<()> {
515        self.write_backspace_mode(enabled)?;
516        self.state.backspace_mode = enabled;
517        self.state.chosen.insert(mode::Mode::BACKARROW_KEY);
518        Ok(())
519    }
520
521    fn write_backspace_mode(&mut self, enabled: bool) -> io::Result<()> {
522        // Keep async decoding in step with the terminal mode change.
523        #[cfg(windows)]
524        let mut source = super::lock_source_for_mode_change(&self.source, &self.source_waker)?;
525        #[cfg(not(windows))]
526        let mut source = self.source.lock().unwrap();
527        if enabled {
528            mode::Mode::BACKARROW_KEY.set(&mut self.screen)?;
529        } else {
530            mode::Mode::BACKARROW_KEY.reset(&mut self.screen)?;
531        }
532        self.screen.flush()?;
533        source.set_backspace_mode(enabled);
534        Ok(())
535    }
536
537    /// Set the per-screen-buffer Kitty keyboard enhancements and flush.
538    /// `Some(flags)` enables the selected progressive-enhancement bits;
539    /// `None` disables every enhancement.
540    ///
541    /// The Kitty stack is per-screen-buffer, so the flags are re-applied on
542    /// the newly active buffer by [`enter_alt_screen`](Self::enter_alt_screen)
543    /// / [`exit_alt_screen`](Self::exit_alt_screen), as part of the switch.
544    pub fn set_kitty_keyboard(
545        &mut self,
546        flags: Option<kitty::KittyKeyboardFlags>,
547    ) -> io::Result<()> {
548        let flags = flags.unwrap_or_else(kitty::KittyKeyboardFlags::empty);
549        kitty::write_set_kitty_keyboard(&mut self.screen, flags, kitty::KittyKeyboardMode::Set)?;
550        self.state.kitty_keyboard = flags;
551        self.screen.flush()
552    }
553
554    /// Set the xterm modifyOtherKeys mode (`CSI > 4 ; n m`) and flush.
555    /// Passing [`ModifyOtherKeysMode::Disabled`] resets it (`CSI > 4 m`).
556    /// The mode is recorded so [`Program::finish`](super::Program::finish)
557    /// can reset it and [`Program::resume`](super::Program::resume) re-apply
558    /// it.
559    ///
560    /// [`ModifyOtherKeysMode::Disabled`]: crate::event::ModifyOtherKeysMode::Disabled
561    pub fn set_modify_other_keys(
562        &mut self,
563        mode: crate::event::ModifyOtherKeysMode,
564    ) -> io::Result<()> {
565        use crate::event::ModifyOtherKeysMode;
566        match mode {
567            ModifyOtherKeysMode::Disabled => {
568                self.screen.write_all(xterm::RESET_MODIFY_OTHER_KEYS)?
569            }
570            ModifyOtherKeysMode::Mode1 => self.screen.write_all(xterm::SET_MODIFY_OTHER_KEYS_1)?,
571            ModifyOtherKeysMode::Mode2 => self.screen.write_all(xterm::SET_MODIFY_OTHER_KEYS_2)?,
572        }
573        self.state.modify_other_keys = mode;
574        self.screen.flush()
575    }
576
577    /// Set the default foreground color (`OSC 10`) and flush. The color is
578    /// converted to 24-bit RGB and emitted as `rgb:RRRR/GGGG/BBBB`, and is
579    /// recorded so [`Program::finish`](super::Program::finish) can restore
580    /// the terminal default and [`Program::resume`](super::Program::resume)
581    /// can re-apply it.
582    pub fn set_foreground_color(&mut self, color: Color) -> io::Result<()> {
583        let (r, g, b) = color.to_rgb();
584        color::write_set_foreground_color(&mut self.screen, &color::xparse_rgb(r, g, b))?;
585        self.state.foreground_color = Some(color);
586        self.screen.flush()
587    }
588
589    /// Restore the terminal's default foreground color (`OSC 110`) and
590    /// flush.
591    pub fn reset_foreground_color(&mut self) -> io::Result<()> {
592        self.screen.write_all(color::RESET_FOREGROUND_COLOR)?;
593        self.state.foreground_color = None;
594        self.screen.flush()
595    }
596
597    /// Set the default background color (`OSC 11`) and flush. See
598    /// [`set_foreground_color`](Self::set_foreground_color) for
599    /// state-tracking semantics.
600    pub fn set_background_color(&mut self, color: Color) -> io::Result<()> {
601        let (r, g, b) = color.to_rgb();
602        color::write_set_background_color(&mut self.screen, &color::xparse_rgb(r, g, b))?;
603        self.state.background_color = Some(color);
604        self.screen.flush()
605    }
606
607    /// Restore the terminal's default background color (`OSC 111`) and
608    /// flush.
609    pub fn reset_background_color(&mut self) -> io::Result<()> {
610        self.screen.write_all(color::RESET_BACKGROUND_COLOR)?;
611        self.state.background_color = None;
612        self.screen.flush()
613    }
614
615    /// Set the cursor color (`OSC 12`) and flush. See
616    /// [`set_foreground_color`](Self::set_foreground_color) for
617    /// state-tracking semantics.
618    pub fn set_cursor_color(&mut self, color: Color) -> io::Result<()> {
619        let (r, g, b) = color.to_rgb();
620        color::write_set_cursor_color(&mut self.screen, &color::xparse_rgb(r, g, b))?;
621        self.state.cursor_color = Some(color);
622        self.screen.flush()
623    }
624
625    /// Restore the terminal's default cursor color (`OSC 112`) and flush.
626    pub fn reset_cursor_color(&mut self) -> io::Result<()> {
627        self.screen.write_all(color::RESET_CURSOR_COLOR)?;
628        self.state.cursor_color = None;
629        self.screen.flush()
630    }
631
632    /// Set a terminal palette color by index (`OSC 4`) and flush. The
633    /// override is tracked so [`Program::finish`](super::Program::finish) can
634    /// restore it and [`Program::resume`](super::Program::resume) re-apply it.
635    pub fn set_palette_color(&mut self, index: u8, color: Color) -> io::Result<()> {
636        let (r, g, b) = color.to_rgb();
637        color::write_set_palette_color(&mut self.screen, index, &color::xparse_rgb(r, g, b))?;
638        self.state.palette.insert(index, color);
639        self.screen.flush()
640    }
641
642    /// Reset a single terminal palette color to its default
643    /// (`OSC 104 ; index`) and flush.
644    pub fn reset_palette_color(&mut self, index: u8) -> io::Result<()> {
645        color::write_reset_palette_color(&mut self.screen, index)?;
646        self.state.palette.remove(&index);
647        self.screen.flush()
648    }
649
650    /// Reset the entire terminal palette to its defaults (`OSC 104`) and
651    /// flush, clearing every tracked palette override.
652    pub fn reset_palette_colors(&mut self) -> io::Result<()> {
653        self.screen.write_all(color::RESET_PALETTE_COLORS)?;
654        self.state.palette.clear();
655        self.screen.flush()
656    }
657
658    /// Stage the teardown of every mode currently held — non-render modes
659    /// (cursor style, mouse, paste, focus, colors, title, …) followed by the
660    /// render-coupled modes (cursor visibility, alternate screen, Kitty
661    /// keyboard, Unicode core) — returning the terminal to a clean baseline
662    /// before handing control back to the shell. Preserves the selected
663    /// state, so [`restore`](Self::restore) re-applies the same modes.
664    /// Backarrow changes also update the decoder. The caller flushes.
665    pub(super) fn reset(&mut self) -> io::Result<()> {
666        // --- Non-render modes ---
667        if self.state.cursor_style != cursor::CursorStyle::Default {
668            cursor::write_cursor_style(&mut self.screen, cursor::CursorStyle::Default)?;
669        }
670        if self.state.bracketed_paste {
671            mode::Mode::BRACKETED_PASTE.reset(&mut self.screen)?;
672        }
673        if self.state.focus_events {
674            mode::Mode::FOCUS.reset(&mut self.screen)?;
675        }
676        if let Some(tracking) = self.state.mouse {
677            self.write_mouse_modes(tracking, false)?;
678        }
679        if self.state.color_scheme_updates {
680            mode::Mode::LIGHT_DARK.reset(&mut self.screen)?;
681        }
682        if self.state.visibility_reports {
683            mode::Mode::VISIBILITY_REPORTS.reset(&mut self.screen)?;
684        }
685        if self.state.in_band_resize {
686            mode::Mode::IN_BAND_RESIZE.reset(&mut self.screen)?;
687        }
688        if self.state.chosen.contains(&mode::Mode::BACKARROW_KEY) {
689            self.write_backspace_mode(false)?;
690        }
691        if self.state.modify_other_keys != crate::event::ModifyOtherKeysMode::Disabled {
692            self.screen.write_all(xterm::RESET_MODIFY_OTHER_KEYS)?;
693        }
694        if self.state.foreground_color.is_some() {
695            self.screen.write_all(color::RESET_FOREGROUND_COLOR)?;
696        }
697        if self.state.background_color.is_some() {
698            self.screen.write_all(color::RESET_BACKGROUND_COLOR)?;
699        }
700        if self.state.cursor_color.is_some() {
701            self.screen.write_all(color::RESET_CURSOR_COLOR)?;
702        }
703        if !self.state.palette.is_empty() {
704            self.screen.write_all(color::RESET_PALETTE_COLORS)?;
705        }
706        match (&self.state.window_title, &self.state.icon_name) {
707            // Both set to the same string (e.g. via `set_title`): clear both
708            // with a single `OSC 0`.
709            (Some(w), Some(i)) if w == i => {
710                ansi::title::write_window_title_and_icon(&mut self.screen, "")?;
711            }
712            (window_title, icon_name) => {
713                if window_title.is_some() {
714                    ansi::title::write_window_title(&mut self.screen, "")?;
715                }
716                if icon_name.is_some() {
717                    ansi::title::write_icon_name(&mut self.screen, "")?;
718                }
719            }
720        }
721        if self.state.pointer_shape.is_some() {
722            cursor::write_set_pointer_shape(&mut self.screen, "default")?;
723        }
724        if self.state.progress.is_some() {
725            self.screen.write_all(progress::RESET_PROGRESS_BAR)?;
726        }
727
728        // --- Render-coupled modes ---
729        // Park the cursor below the managed area before any teardown, so the
730        // shell prompt lands where the user expects.
731        self.screen.park_cursor()?;
732        if !self.state.cursor_visible {
733            mode::Mode::CURSOR_VISIBLE.set(&mut self.screen)?;
734        }
735        let fullscreen = self.state.alt_screen;
736        // Clear the alt screen's kitty keyboard frame *before* leaving the alt
737        // screen — the stack is per-screen-buffer.
738        if fullscreen && !self.state.kitty_keyboard.is_empty() {
739            kitty::write_set_kitty_keyboard(
740                &mut self.screen,
741                kitty::KittyKeyboardFlags::empty(),
742                kitty::KittyKeyboardMode::Set,
743            )?;
744        }
745        if fullscreen {
746            mode::Mode::ALT_SCREEN_SAVE_CURSOR.reset(&mut self.screen)?;
747            self.screen.restore_cursor();
748        }
749        // Now on the main screen — clear its frame too.
750        if !self.state.kitty_keyboard.is_empty() {
751            kitty::write_set_kitty_keyboard(
752                &mut self.screen,
753                kitty::KittyKeyboardFlags::empty(),
754                kitty::KittyKeyboardMode::Set,
755            )?;
756        }
757        if self.state.grapheme_clusters {
758            mode::Mode::UNICODE_CORE.reset(&mut self.screen)?;
759        }
760        self.screen.invalidate_cursor();
761        Ok(())
762    }
763
764    /// Re-emit every mode held in the tracked state — the render-coupled
765    /// modes (Kitty keyboard, alternate screen, Unicode core, cursor
766    /// visibility) first, then the non-render modes — for any scenario where
767    /// the terminal was temporarily handed back to the shell. Pairs with
768    /// [`reset`](Self::reset). Preserves the selected state and updates the
769    /// decoder's Backarrow mode. The caller flushes.
770    pub(super) fn restore(&mut self) -> io::Result<()> {
771        // --- Render-coupled modes ---
772        // Re-apply the desired kitty keyboard flags on the main screen
773        // *before* entering the alt screen — the stack is per-buffer.
774        if !self.state.kitty_keyboard.is_empty() {
775            kitty::write_set_kitty_keyboard(
776                &mut self.screen,
777                self.state.kitty_keyboard,
778                kitty::KittyKeyboardMode::Set,
779            )?;
780        }
781        let fullscreen = self.state.alt_screen;
782        if fullscreen {
783            self.screen.save_cursor();
784            mode::Mode::ALT_SCREEN_SAVE_CURSOR.set(&mut self.screen)?;
785        }
786        // Now on the alt screen (if alt was active) — re-apply on the alt
787        // buffer too, since its stack is independent.
788        if fullscreen && !self.state.kitty_keyboard.is_empty() {
789            kitty::write_set_kitty_keyboard(
790                &mut self.screen,
791                self.state.kitty_keyboard,
792                kitty::KittyKeyboardMode::Set,
793            )?;
794        }
795        if self.state.grapheme_clusters {
796            mode::Mode::UNICODE_CORE.set(&mut self.screen)?;
797        }
798        if !self.state.cursor_visible {
799            mode::Mode::CURSOR_VISIBLE.reset(&mut self.screen)?;
800        }
801
802        // --- Non-render modes ---
803        if self.state.cursor_style != cursor::CursorStyle::Default {
804            cursor::write_cursor_style(&mut self.screen, self.state.cursor_style)?;
805        }
806        if self.state.color_scheme_updates {
807            mode::Mode::LIGHT_DARK.set(&mut self.screen)?;
808        }
809        if self.state.visibility_reports {
810            mode::Mode::VISIBILITY_REPORTS.set(&mut self.screen)?;
811        }
812        if self.state.in_band_resize {
813            mode::Mode::IN_BAND_RESIZE.set(&mut self.screen)?;
814        }
815        if self.state.chosen.contains(&mode::Mode::BACKARROW_KEY) {
816            self.write_backspace_mode(self.state.backspace_mode)?;
817        }
818        match self.state.modify_other_keys {
819            crate::event::ModifyOtherKeysMode::Mode1 => {
820                self.screen.write_all(xterm::SET_MODIFY_OTHER_KEYS_1)?;
821            }
822            crate::event::ModifyOtherKeysMode::Mode2 => {
823                self.screen.write_all(xterm::SET_MODIFY_OTHER_KEYS_2)?;
824            }
825            crate::event::ModifyOtherKeysMode::Disabled => {}
826        }
827        if self.state.bracketed_paste {
828            mode::Mode::BRACKETED_PASTE.set(&mut self.screen)?;
829        }
830        if self.state.focus_events {
831            mode::Mode::FOCUS.set(&mut self.screen)?;
832        }
833        if let Some(pref) = self.state.mouse {
834            self.write_mouse_modes(pref, true)?;
835        }
836        if let Some(c) = self.state.foreground_color {
837            let (r, g, b) = c.to_rgb();
838            color::write_set_foreground_color(&mut self.screen, &color::xparse_rgb(r, g, b))?;
839        }
840        if let Some(c) = self.state.background_color {
841            let (r, g, b) = c.to_rgb();
842            color::write_set_background_color(&mut self.screen, &color::xparse_rgb(r, g, b))?;
843        }
844        if let Some(c) = self.state.cursor_color {
845            let (r, g, b) = c.to_rgb();
846            color::write_set_cursor_color(&mut self.screen, &color::xparse_rgb(r, g, b))?;
847        }
848        for (&index, &c) in &self.state.palette {
849            let (r, g, b) = c.to_rgb();
850            color::write_set_palette_color(&mut self.screen, index, &color::xparse_rgb(r, g, b))?;
851        }
852        match (
853            self.state.window_title.clone(),
854            self.state.icon_name.clone(),
855        ) {
856            // Both set to the same string (e.g. via `set_title`): restore both
857            // with a single `OSC 0`.
858            (Some(w), Some(i)) if w == i => {
859                ansi::title::write_window_title_and_icon(&mut self.screen, &w)?;
860            }
861            (window_title, icon_name) => {
862                if let Some(title) = window_title {
863                    ansi::title::write_window_title(&mut self.screen, &title)?;
864                }
865                if let Some(name) = icon_name {
866                    ansi::title::write_icon_name(&mut self.screen, &name)?;
867                }
868            }
869        }
870        if let Some(shape) = self.state.pointer_shape.clone() {
871            cursor::write_set_pointer_shape(&mut self.screen, &shape)?;
872        }
873        if let Some(progress) = self.state.progress {
874            progress.write(&mut self.screen)?;
875        }
876        Ok(())
877    }
878
879    // --- Request delegates -----------------------------------------------
880    //
881    // Each writes a terminal query and flushes; the reply arrives later
882    // through the event flow. Replies that double as capability
883    // reports (mode, kitty keyboard) are recorded into
884    // [`capabilities`](Self::capabilities); value replies (cursor
885    // position, colors, pixel sizes) surface to the caller as events.
886
887    /// Request the window size in pixels (XTWINOPS `CSI 14 t`). Reply:
888    /// [`Event::WindowPixelSize`](crate::event::Event::WindowPixelSize).
889    pub fn request_window_pixel_size(&mut self) -> io::Result<()> {
890        self.screen
891            .write_all(crate::ansi::winop::REQUEST_WINDOW_PIXEL_SIZE)?;
892        self.screen.flush()
893    }
894
895    /// Request the character cell size in pixels (XTWINOPS `CSI 16 t`).
896    /// Reply: [`Event::CellPixelSize`](crate::event::Event::CellPixelSize).
897    pub fn request_cell_pixel_size(&mut self) -> io::Result<()> {
898        self.screen
899            .write_all(crate::ansi::winop::REQUEST_CELL_PIXEL_SIZE)?;
900        self.screen.flush()
901    }
902
903    /// Request the physical screen coordinate of the managed area's top-left
904    /// cell: park the cursor there and ask the terminal where it landed
905    /// (`CSI 6n`).
906    ///
907    /// The reply arrives asynchronously as a
908    /// [`CursorPosition`](crate::event::Event::CursorPosition) event and is
909    /// recorded (and clipped to keep the managed area on screen) by
910    /// [`observe_event`](Self::observe_event); read the result with
911    /// [`origin`](Self::origin). The event is still delivered to you.
912    ///
913    /// Call this once mouse mapping starts, again whenever the terminal
914    /// resizes, and again after [`resume`](Self::resume), each of which can
915    /// move the managed area. Without it the origin stays at `(0, 0)` and
916    /// [`mouse_to_origin`](Self::mouse_to_origin) is an identity.
917    ///
918    /// A no-op in fullscreen, where the origin is always `(0, 0)`.
919    pub fn request_origin(&mut self) -> io::Result<()> {
920        if self.screen.fullscreen() {
921            return Ok(());
922        }
923        // Park the cursor at the surface top-left; in relative-cursor inline
924        // mode that is the physical origin, so the reply *is* the origin.
925        // Stage the move rather than using move_cursor_to, which would flush
926        // before the query is written.
927        self.screen
928            .stage_move_cursor_to(crate::layout::Position::ORIGIN);
929        self.screen
930            .write_all(crate::ansi::status::REQUEST_CURSOR_POSITION)?;
931        self.origin_queries_pending = self.origin_queries_pending.saturating_add(1);
932        self.screen.flush()
933    }
934
935    /// Request the terminal's active Kitty keyboard flags (`CSI ? u`).
936    /// The reply is recorded in [`capabilities`](Self::capabilities).
937    pub fn request_kitty_keyboard(&mut self) -> io::Result<()> {
938        self.screen
939            .write_all(crate::ansi::kitty::REQUEST_KITTY_KEYBOARD)?;
940        self.screen.flush()
941    }
942
943    /// Request the terminal's modifyOtherKeys state (`CSI ? 4 m`). The
944    /// reply is recorded in [`capabilities`](Self::capabilities).
945    pub fn request_modify_other_keys(&mut self) -> io::Result<()> {
946        self.screen
947            .write_all(crate::ansi::xterm::QUERY_MODIFY_OTHER_KEYS)?;
948        self.screen.flush()
949    }
950
951    /// Request the default foreground color (`OSC 10 ; ? ST`). Reply:
952    /// [`Event::ForegroundColor`](crate::event::Event::ForegroundColor).
953    pub fn request_foreground_color(&mut self) -> io::Result<()> {
954        self.screen
955            .write_all(crate::ansi::color::REQUEST_FOREGROUND_COLOR)?;
956        self.screen.flush()
957    }
958
959    /// Request the default background color (`OSC 11 ; ? ST`). Reply:
960    /// [`Event::BackgroundColor`](crate::event::Event::BackgroundColor).
961    pub fn request_background_color(&mut self) -> io::Result<()> {
962        self.screen
963            .write_all(crate::ansi::color::REQUEST_BACKGROUND_COLOR)?;
964        self.screen.flush()
965    }
966
967    /// Request the cursor color (`OSC 12 ; ? ST`). Reply:
968    /// [`Event::CursorColor`](crate::event::Event::CursorColor).
969    pub fn request_cursor_color(&mut self) -> io::Result<()> {
970        self.screen
971            .write_all(crate::ansi::color::REQUEST_CURSOR_COLOR)?;
972        self.screen.flush()
973    }
974
975    /// Request a terminal palette color by index (`OSC 4 ; index ; ? ST`).
976    /// Reply: `OSC 4 ; index ; rgb:... ST`.
977    pub fn request_palette_color(&mut self, index: u8) -> io::Result<()> {
978        crate::ansi::color::write_request_palette_color(&mut self.screen, index)?;
979        self.screen.flush()
980    }
981
982    /// Request a terminal mode's current setting (DECRQM). Reply:
983    /// [`Event::ModeReport`](crate::event::Event::ModeReport).
984    ///
985    /// The reply's [`ModeSetting`](crate::ansi::mode::ModeSetting) reports whether
986    /// the mode is set, reset, or permanently fixed. A permanently reset mode
987    /// is recognized but can never be enabled, so check
988    /// [`ModeSetting::is_available`](crate::ansi::mode::ModeSetting::is_available)
989    /// before relying on it.
990    pub fn request_mode(&mut self, mode: crate::ansi::mode::Mode) -> io::Result<()> {
991        mode.request(&mut self.screen)?;
992        self.screen.flush()
993    }
994
995    /// Request the cursor position (`CSI 6 n`). Reply:
996    /// [`Event::CursorPosition`](crate::event::Event::CursorPosition).
997    ///
998    /// For a reply distinct from modified F3, use
999    /// [`request_extended_cursor_position`](Self::request_extended_cursor_position)
1000    /// on terminals that support DECXCPR.
1001    pub fn request_cursor_position(&mut self) -> io::Result<()> {
1002        self.screen
1003            .write_all(crate::ansi::status::REQUEST_CURSOR_POSITION)?;
1004        self.screen.flush()
1005    }
1006
1007    /// Request the extended cursor position (`CSI ? 6 n`) and flush.
1008    ///
1009    /// DECXCPR replies produce [`Event::CursorPosition`](crate::event::Event::CursorPosition)
1010    /// with zero-based cell coordinates and a one-based page number when
1011    /// reported. An absent page is zero. The private reply distinguishes cursor
1012    /// reports from modified F3.
1013    pub fn request_extended_cursor_position(&mut self) -> io::Result<()> {
1014        self.screen
1015            .write_all(crate::ansi::status::REQUEST_EXTENDED_CURSOR_POSITION)?;
1016        self.screen.flush()
1017    }
1018
1019    /// Request the current cursor style (DECRQSS for `DECSCUSR`). Reply:
1020    /// [`Event::SettingReport`](crate::event::Event::SettingReport) carrying
1021    /// [`SettingReport::CursorStyle`](crate::event::SettingReport::CursorStyle).
1022    ///
1023    /// The reply says what the cursor is *now*, so ask before changing it if
1024    /// you mean to put it back. Teardown writes [`CursorStyle::Default`],
1025    /// which returns the cursor the terminal's user configured; keeping the
1026    /// one this session found is yours to do, because only you know when you
1027    /// asked.
1028    ///
1029    /// A terminal that does not implement DECRQSS may answer
1030    /// [`SettingReport::Unrecognized`](crate::event::SettingReport::Unrecognized)
1031    /// or stay silent, so treat the reply as optional.
1032    ///
1033    /// Read the answer with [`CursorStyle::shape`] and
1034    /// [`CursorStyle::blinking`], the same terms
1035    /// [`set_cursor_style`](Self::set_cursor_style) takes.
1036    pub fn request_cursor_style(&mut self) -> io::Result<()> {
1037        crate::ansi::status::write_decrqss(&mut self.screen, " q")?;
1038        self.screen.flush()
1039    }
1040
1041    /// Request the current color scheme (`CSI ? 996 n`): whether the
1042    /// terminal's scheme is dark or light. This reports only the dark/light
1043    /// preference, not the actual colors. Reply:
1044    /// [`Event::ColorScheme`](crate::event::Event::ColorScheme).
1045    pub fn request_color_scheme(&mut self) -> io::Result<()> {
1046        self.screen
1047            .write_all(crate::ansi::status::REQUEST_LIGHT_DARK_REPORT)?;
1048        self.screen.flush()
1049    }
1050
1051    /// Set the system clipboard contents (`OSC 52 ; c`). `data` is
1052    /// base64-encoded for transport.
1053    pub fn set_system_clipboard(&mut self, data: &[u8]) -> io::Result<()> {
1054        crate::ansi::clipboard::write_set_clipboard(
1055            &mut self.screen,
1056            crate::ansi::clipboard::SYSTEM_CLIPBOARD,
1057            data,
1058        )?;
1059        self.screen.flush()
1060    }
1061
1062    /// Set the primary selection contents (`OSC 52 ; p`). `data` is
1063    /// base64-encoded for transport.
1064    pub fn set_primary_clipboard(&mut self, data: &[u8]) -> io::Result<()> {
1065        crate::ansi::clipboard::write_set_clipboard(
1066            &mut self.screen,
1067            crate::ansi::clipboard::PRIMARY_CLIPBOARD,
1068            data,
1069        )?;
1070        self.screen.flush()
1071    }
1072
1073    /// Request the system clipboard contents (`OSC 52 ; c ; ?`). Reply:
1074    /// [`Event::ClipboardStart`](crate::event::Event::ClipboardStart), raw
1075    /// [`Event::ClipboardChunk`](crate::event::Event::ClipboardChunk) bytes,
1076    /// then [`Event::ClipboardEnd`](crate::event::Event::ClipboardEnd).
1077    pub fn request_system_clipboard(&mut self) -> io::Result<()> {
1078        crate::ansi::clipboard::write_request_clipboard(
1079            &mut self.screen,
1080            crate::ansi::clipboard::SYSTEM_CLIPBOARD,
1081        )?;
1082        self.screen.flush()
1083    }
1084
1085    /// Request the primary selection contents (`OSC 52 ; p ; ?`). Reply:
1086    /// [`Event::ClipboardStart`](crate::event::Event::ClipboardStart), raw
1087    /// [`Event::ClipboardChunk`](crate::event::Event::ClipboardChunk) bytes,
1088    /// then [`Event::ClipboardEnd`](crate::event::Event::ClipboardEnd).
1089    pub fn request_primary_clipboard(&mut self) -> io::Result<()> {
1090        crate::ansi::clipboard::write_request_clipboard(
1091            &mut self.screen,
1092            crate::ansi::clipboard::PRIMARY_CLIPBOARD,
1093        )?;
1094        self.screen.flush()
1095    }
1096}