Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Kitty protocol keyboard enhancement support #607

Merged
merged 5 commits into from
Aug 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExternalPrinter<String>>,
}
Expand All @@ -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);
}
}
}

Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -243,6 +250,25 @@ 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) {
self.use_kitty_protocol = true;
}

/// Disable keyboard enhancement to disambiguate escape code
pub fn disable_kitty_protocol(&mut self) {
self.use_kitty_protocol = false;
}

/// Return the previously generated history session id
pub fn get_history_session_id(&self) -> Option<HistorySessionId> {
self.history_session_id
Expand Down Expand Up @@ -575,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
}
Expand Down Expand Up @@ -620,6 +651,31 @@ impl Reedline {
let mut crossterm_events: Vec<ReedlineRawEvent> = vec![];
let mut reedline_events: Vec<ReedlineEvent> = 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;

Expand Down
Loading