Skip to main content

rusty_bubbletea/
keyboard.rs

1//! Cleanroom Rust port of upstream Go source file: `keyboard.go`
2//! Upstream Target Tag / Version: `v2.0.8`
3//!
4//! <public-docs>
5//! # Keyboard Enhancements
6//!
7//! Kitty keyboard protocol enhancement message structures and capability queries.
8//! </public-docs>
9
10/// Bitmask constants matching Kitty keyboard protocol enhancements.
11pub const KITTY_REPORT_EVENT_TYPES: i32 = 1 << 0;
12/// Report alternate keys capability flag.
13pub const KITTY_REPORT_ALTERNATE_KEYS: i32 = 1 << 1;
14/// Report all keys as escape codes capability flag.
15pub const KITTY_REPORT_ALL_KEYS_AS_ESCAPE_CODES: i32 = 1 << 2;
16/// Report associated keys capability flag.
17pub const KITTY_REPORT_ASSOCIATED_KEYS: i32 = 1 << 3;
18
19/// KeyboardEnhancements struct matching tea.KeyboardEnhancements in v2.0.8.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
21pub struct KeyboardEnhancements {
22    /// Report press, release, and repeat events.
23    pub report_event_types: bool,
24    /// Report alternate keys.
25    pub report_alternate_keys: bool,
26    /// Report all keys as escape codes.
27    pub report_all_keys_as_escape_codes: bool,
28    /// Report associated text.
29    pub report_associated_text: bool,
30}
31
32/// KeyboardEnhancementsMsg is a message that gets sent when the terminal
33/// supports keyboard enhancements.
34#[derive(Debug, Clone, Copy, PartialEq, Eq)]
35pub struct KeyboardEnhancementsMsg {
36    /// Flags is a bitmask of enabled keyboard enhancement features. A non-zero
37    /// value indicates that at least we have key disambiguation support.
38    ///
39    /// Use the `supports_*` methods for the easy way to check individual flags.
40    pub flags: i32,
41}
42
43impl KeyboardEnhancementsMsg {
44    /// SupportsKeyDisambiguation returns whether the terminal supports key
45    /// disambiguation (e.g., distinguishing between different modifier keys).
46    pub fn supports_key_disambiguation(&self) -> bool {
47        self.flags > 0
48    }
49
50    /// SupportsEventTypes returns whether the terminal supports reporting
51    /// different types of key events (press, release, and repeat).
52    pub fn supports_event_types(&self) -> bool {
53        (self.flags & KITTY_REPORT_EVENT_TYPES) != 0
54    }
55
56    /// SupportsAlternateKeys returns whether the terminal supports reporting
57    /// alternate key codes.
58    pub fn supports_alternate_keys(&self) -> bool {
59        (self.flags & KITTY_REPORT_ALTERNATE_KEYS) != 0
60    }
61
62    /// SupportsAllKeysAsEscapeCodes returns whether the terminal supports
63    /// reporting all keys as escape codes.
64    pub fn supports_all_keys_as_escape_codes(&self) -> bool {
65        (self.flags & KITTY_REPORT_ALL_KEYS_AS_ESCAPE_CODES) != 0
66    }
67
68    /// SupportsAssociatedText returns whether the terminal supports reporting
69    /// associated text with key events.
70    pub fn supports_associated_text(&self) -> bool {
71        (self.flags & KITTY_REPORT_ASSOCIATED_KEYS) != 0
72    }
73}