reedline/terminal_extensions/kitty.rs
1use crossterm::{event, execute};
2
3/// Helper managing proper setup and teardown of the kitty keyboard enhancement protocol
4///
5/// Note that, currently, only the following support this protocol:
6/// * [kitty terminal](https://sw.kovidgoyal.net/kitty/)
7/// * [foot terminal](https://codeberg.org/dnkl/foot/issues/319)
8/// * [WezTerm terminal](https://wezfurlong.org/wezterm/config/lua/config/enable_kitty_keyboard.html)
9/// * [notcurses library](https://github.com/dankamongmen/notcurses/issues/2131)
10/// * [neovim text editor](https://github.com/neovim/neovim/pull/18181)
11/// * [kakoune text editor](https://github.com/mawww/kakoune/issues/4103)
12/// * [dte text editor](https://gitlab.com/craigbarnes/dte/-/issues/138)
13///
14/// Refer to <https://sw.kovidgoyal.net/kitty/keyboard-protocol/> if you're curious.
15#[derive(Default)]
16pub(crate) struct KittyProtocolGuard {
17 enabled: bool,
18 active: bool,
19}
20
21impl KittyProtocolGuard {
22 pub fn set(&mut self, enable: bool) {
23 self.enabled = enable && super::kitty_protocol_available();
24 }
25 pub fn enter(&mut self) {
26 if self.enabled && !self.active {
27 let _ = execute!(
28 std::io::stdout(),
29 event::PushKeyboardEnhancementFlags(
30 event::KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES
31 )
32 );
33
34 self.active = true;
35 }
36 }
37 pub fn exit(&mut self) {
38 if self.active {
39 let _ = execute!(std::io::stdout(), event::PopKeyboardEnhancementFlags);
40 self.active = false;
41 }
42 }
43}
44
45impl Drop for KittyProtocolGuard {
46 fn drop(&mut self) {
47 if self.active {
48 let _ = execute!(std::io::stdout(), event::PopKeyboardEnhancementFlags);
49 }
50 }
51}