From efb634275782464516b6604081e67625741c4970 Mon Sep 17 00:00:00 2001 From: Hernawan Fa'iz Abdillah Date: Wed, 19 Jul 2023 06:09:07 +0700 Subject: [PATCH 1/5] add use_kitty_protocol support option --- src/engine.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/engine.rs b/src/engine.rs index ac69df9a..baeb5d62 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -140,6 +140,9 @@ pub struct Reedline { // Indicate if global terminal have enabled BracketedPaste bracket_paste_enabled: bool, + // Use kitty protocol to handle escape code input or not + use_kitty_protocol: bool, + #[cfg(feature = "external_printer")] external_printer: Option>, } @@ -165,6 +168,9 @@ impl Drop for Reedline { if self.bracket_paste_enabled { let _ = execute!(io::stdout(), DisableBracketedPaste); } + if self.use_kitty_protocol { + let _ = execute!(io::stdout(), event::PopKeyboardEnhancementFlags); + } } } @@ -210,6 +216,7 @@ impl Reedline { buffer_editor: None, cursor_shapes: None, bracket_paste_enabled: false, + use_kitty_protocol: false, #[cfg(feature = "external_printer")] external_printer: None, } @@ -243,6 +250,27 @@ impl Reedline { res } + /// Return terminal support on keyboard enhancement + pub fn can_use_kitty_protocol(&mut self) -> bool { + if let Ok(b) = crossterm::terminal::supports_keyboard_enhancement() { + b + } else { + false + } + } + + /// Enable keyboard enhancement to disambiguate escape code + pub fn enable_kitty_protocol(&mut self) -> Result<()> { + self.use_kitty_protocol = true; + Ok(()) + } + + /// Disable keyboard enhancement to disambiguate escape code + pub fn disable_kitty_protocol(&mut self) -> Result<()> { + self.use_kitty_protocol = false; + Ok(()) + } + /// Return the previously generated history session id pub fn get_history_session_id(&self) -> Option { self.history_session_id @@ -620,6 +648,31 @@ impl Reedline { let mut crossterm_events: Vec = vec![]; let mut reedline_events: Vec = vec![]; + if self.use_kitty_protocol { + if let Ok(true) = crossterm::terminal::supports_keyboard_enhancement() { + // enable kitty protocol + // + // Note that, currently, only the following support this protocol: + // * [kitty terminal](https://sw.kovidgoyal.net/kitty/) + // * [foot terminal](https://codeberg.org/dnkl/foot/issues/319) + // * [WezTerm terminal](https://wezfurlong.org/wezterm/config/lua/config/enable_kitty_keyboard.html) + // * [notcurses library](https://github.com/dankamongmen/notcurses/issues/2131) + // * [neovim text editor](https://github.com/neovim/neovim/pull/18181) + // * [kakoune text editor](https://github.com/mawww/kakoune/issues/4103) + // * [dte text editor](https://gitlab.com/craigbarnes/dte/-/issues/138) + // + // Refer to https://sw.kovidgoyal.net/kitty/keyboard-protocol/ if you're curious. + let _ = execute!( + io::stdout(), + event::PushKeyboardEnhancementFlags( + event::KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES + ) + ); + } else { + // TODO: Log or warning + } + } + loop { let mut paste_enter_state = false; From fcea830dc4f961fc3b1d0d2dee2c7bcb3e57a5c4 Mon Sep 17 00:00:00 2001 From: Hernawan Fa'iz Abdillah Date: Fri, 21 Jul 2023 22:44:44 +0700 Subject: [PATCH 2/5] move pop key enhancement flags just before loop exit --- src/engine.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index baeb5d62..2e27c8b2 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -168,9 +168,6 @@ impl Drop for Reedline { if self.bracket_paste_enabled { let _ = execute!(io::stdout(), DisableBracketedPaste); } - if self.use_kitty_protocol { - let _ = execute!(io::stdout(), event::PopKeyboardEnhancementFlags); - } } } @@ -766,6 +763,9 @@ impl Reedline { for event in reedline_events.drain(..) { match self.handle_event(prompt, event)? { EventStatus::Exits(signal) => { + if self.use_kitty_protocol { + let _ = execute!(io::stdout(), event::PopKeyboardEnhancementFlags); + } // Move the cursor below the input area, for external commands or new read_line call self.painter.move_cursor_to_end()?; return Ok(signal); From 8b64f6b5de1f6224e1fddad9eb3dfc785aae6c5a Mon Sep 17 00:00:00 2001 From: Hernawan Fa'iz Abdillah Date: Mon, 24 Jul 2023 09:41:43 +0700 Subject: [PATCH 3/5] add pop keyboard flags pop on drop --- src/engine.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/engine.rs b/src/engine.rs index 2e27c8b2..99c19148 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -168,6 +168,9 @@ impl Drop for Reedline { if self.bracket_paste_enabled { let _ = execute!(io::stdout(), DisableBracketedPaste); } + if self.use_kitty_protocol { + let _ = execute!(io::stdout(), event::PopKeyboardEnhancementFlags); + } } } From c7d9a7d51364214d1ef708acee2acfd197d110e1 Mon Sep 17 00:00:00 2001 From: Hernawan Fa'iz Abdillah Date: Mon, 24 Jul 2023 10:38:12 +0700 Subject: [PATCH 4/5] remove toggler functions' return --- src/engine.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 99c19148..cfc0944e 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -260,15 +260,13 @@ impl Reedline { } /// Enable keyboard enhancement to disambiguate escape code - pub fn enable_kitty_protocol(&mut self) -> Result<()> { + pub fn enable_kitty_protocol(&mut self) { self.use_kitty_protocol = true; - Ok(()) } /// Disable keyboard enhancement to disambiguate escape code - pub fn disable_kitty_protocol(&mut self) -> Result<()> { + pub fn disable_kitty_protocol(&mut self) { self.use_kitty_protocol = false; - Ok(()) } /// Return the previously generated history session id From b7bc8ef9b3f301e63dadc91f03899ab6c69ca6b2 Mon Sep 17 00:00:00 2001 From: Hernawan Fa'iz Abdillah Date: Mon, 24 Jul 2023 10:39:17 +0700 Subject: [PATCH 5/5] move pop key enhancement flags beside other mode change --- src/engine.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index cfc0944e..7ba541a5 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -601,6 +601,11 @@ impl Reedline { #[cfg(not(target_os = "windows"))] self.disable_bracketed_paste()?; + + if self.use_kitty_protocol { + let _ = execute!(io::stdout(), event::PopKeyboardEnhancementFlags); + } + terminal::disable_raw_mode()?; result } @@ -764,9 +769,6 @@ impl Reedline { for event in reedline_events.drain(..) { match self.handle_event(prompt, event)? { EventStatus::Exits(signal) => { - if self.use_kitty_protocol { - let _ = execute!(io::stdout(), event::PopKeyboardEnhancementFlags); - } // Move the cursor below the input area, for external commands or new read_line call self.painter.move_cursor_to_end()?; return Ok(signal);