Skip to main content

uncurses/event/decode/
flags.rs

1//! Decoder behavior flags for ambiguous legacy input bytes.
2//!
3//! ## Purpose
4//!
5//! [`DecoderFlags`] toggles byte sequences that have more than one historical
6//! interpretation when the terminal is not using an unambiguous keyboard
7//! protocol. With no flags set, the decoder favors semantic keys such as Tab,
8//! Enter, Escape, Backspace, Home, and End.
9//!
10//! ## Affected paths
11//!
12//! The flags affect C0 controls, a timed-out lone `ESC`, Delete/Backspace, and
13//! VT220 Find/Select tilde codes.
14//!
15//! ## Gotchas
16//!
17//! These flags select legacy key interpretations. Choose them to match the
18//! bindings your application exposes. Backarrow mode supplies the baseline;
19//! the flags select explicit overrides.
20use bitflags::bitflags;
21
22bitflags! {
23    /// Optional disambiguation knobs for the input decoder.
24    ///
25    /// With no flag set and Backarrow mode reset, the decoder reports:
26    ///
27    /// * `0x00` → `Ctrl+Space`
28    /// * `0x08` → `Ctrl+h`
29    /// * `0x09` → `Tab`
30    /// * `0x0a` → `Ctrl+j`
31    /// * `0x0d` → `Enter`
32    /// * `0x7f` → `Backspace`
33    /// * `CSI 1 ~` → `Home`
34    /// * `CSI 4 ~` → `End`
35    ///
36    /// Set the corresponding flag to swap each mapping to its alternative
37    /// reading.
38    ///
39    /// In raw mode, terminals normally send LF for Ctrl+J and CR for Enter.
40    /// An `ESC` prefix adds Alt to the selected interpretation.
41    ///
42    /// Backarrow mode (DECBKM) defaults to reset. When set, `0x08` reads as
43    /// Backspace and `0x7f` as Ctrl+Backspace. These are legacy byte
44    /// interpretations: terminals can send the same bytes for different keys.
45    ///
46    /// [`CTRL_H`](Self::CTRL_H) overrides `0x08` in either mode and takes
47    /// precedence over [`CTRL_BACKSPACE`](Self::CTRL_BACKSPACE).
48    /// [`DEL_IS_DELETE`](Self::DEL_IS_DELETE) overrides `0x7f` in either mode.
49    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
50    pub struct DecoderFlags: u16 {
51        /// Report `0x00` as `Ctrl+@` instead of `Ctrl+Space`.
52        const CTRL_AT             = 1 << 0;
53        /// Report `0x09` as `Ctrl+i` instead of `Tab`.
54        const CTRL_I              = 1 << 1;
55        /// Report `0x0d` as `Ctrl+m` instead of `Enter`.
56        const CTRL_M              = 1 << 2;
57        /// Report a lone `0x1b` as `Ctrl+[` instead of `Escape`.
58        ///
59        /// Wherever that byte resolves as a key of its own: an
60        /// [`EventSource`](crate::event::EventSource) reaching its escape
61        /// deadline, whether the `ESC` was alone or at the head of a
62        /// sequence that never finished, and the inner `ESC` of a run of
63        /// them, which reads as `Alt+Ctrl+[`.
64        const CTRL_OPEN_BRACKET   = 1 << 3;
65        /// Report `0x7f` as `Delete` instead of `Backspace`.
66        ///
67        /// Takes precedence over Backarrow mode, including when `0x7f`
68        /// would otherwise read as Ctrl+Backspace.
69        const DEL_IS_DELETE       = 1 << 4;
70        /// Report `CSI 1 ~` as the VT220 `Find` key instead of `Home`.
71        const FIND_KEY            = 1 << 5;
72        /// Report `CSI 4 ~` as the VT220 `Select` key instead of `End`.
73        const SELECT_KEY          = 1 << 6;
74        /// Report `0x0a` (LF) as `Enter` instead of `Ctrl+j`.
75        ///
76        /// An `ESC` prefix reports `Alt+Enter`. This is useful for input
77        /// with CR-to-LF conversion enabled. CR keeps its own interpretation,
78        /// selected by [`CTRL_M`](Self::CTRL_M).
79        const LF_IS_ENTER         = 1 << 7;
80        /// Report `0x08` as `Ctrl+h` in either Backarrow mode.
81        ///
82        /// Takes precedence over [`CTRL_BACKSPACE`](Self::CTRL_BACKSPACE)
83        /// and the mode's Backspace interpretation.
84        const CTRL_H              = 1 << 8;
85        /// Report `0x08` as Ctrl+Backspace when Backarrow mode is reset.
86        ///
87        /// When the mode is set, `0x08` reads as Backspace and `0x7f`
88        /// already reads as Ctrl+Backspace. [`CTRL_H`](Self::CTRL_H) and
89        /// [`DEL_IS_DELETE`](Self::DEL_IS_DELETE) retain their precedence.
90        const CTRL_BACKSPACE      = 1 << 9;
91    }
92}