-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly handle optional event modes (#659)
* Properly handle optional event modes Both bracketed paste and the kitty keyboard enhancement flag are not always supported. This goes both for terminals that may not emit events according to their spec and also applications that will run in the terminal independent of reedline. Make sure we enter the mode when starting to take control and exit the mode when leaving the mode or being dropped. The settings exposed to the user will just internally enable a setting (in the case of the kitty keyboard enhancement flags crossterm provides and easy way to check for support that we take into account with that) * Adjust demo to sane bracketed paste API * cargo fmt * Add a static helper `kitty_protocol_available` Public as `reedline::kitty_protocol_available` Replace the method `Reedline::can_use_kitty_protocol` that doesn't depend on `Reedline` in the future Rewrite `KittyProtocolGuard` in terms of it. * Remove `Reedline::can_use_kitty_protocol` * Add builder style setters for terminal enhancement * Switch demo to bracketed paste via builder * Expand `use_kitty_keyboard_enhancement` doccomment * Remove old terminal enhancement APIs * Use `use_kitty_keyboard_enhancement(true)` in demo As we internally check whether it is available, this should be fine in nearly all terminals (if they can safely be probed) * Fix typo
- Loading branch information
1 parent
63c43a9
commit 8792726
Showing
6 changed files
with
138 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use crossterm::{event, execute}; | ||
|
||
/// Helper managing proper setup and teardown of bracketed paste mode | ||
/// | ||
/// https://en.wikipedia.org/wiki/Bracketed-paste | ||
#[derive(Default)] | ||
pub(crate) struct BracketedPasteGuard { | ||
enabled: bool, | ||
active: bool, | ||
} | ||
|
||
impl BracketedPasteGuard { | ||
pub fn set(&mut self, enable: bool) { | ||
self.enabled = enable; | ||
} | ||
pub fn enter(&mut self) { | ||
if self.enabled && !self.active { | ||
let _ = execute!(std::io::stdout(), event::EnableBracketedPaste); | ||
self.active = true; | ||
} | ||
} | ||
pub fn exit(&mut self) { | ||
if self.active { | ||
let _ = execute!(std::io::stdout(), event::DisableBracketedPaste); | ||
self.active = false; | ||
} | ||
} | ||
} | ||
|
||
impl Drop for BracketedPasteGuard { | ||
fn drop(&mut self) { | ||
if self.active { | ||
let _ = execute!(std::io::stdout(), event::DisableBracketedPaste); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use crossterm::{event, execute}; | ||
|
||
/// Helper managing proper setup and teardown of the kitty keyboard enhancement 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. | ||
#[derive(Default)] | ||
pub(crate) struct KittyProtocolGuard { | ||
enabled: bool, | ||
active: bool, | ||
} | ||
|
||
impl KittyProtocolGuard { | ||
pub fn set(&mut self, enable: bool) { | ||
self.enabled = enable && super::kitty_protocol_available(); | ||
} | ||
pub fn enter(&mut self) { | ||
if self.enabled && !self.active { | ||
let _ = execute!( | ||
std::io::stdout(), | ||
event::PushKeyboardEnhancementFlags( | ||
event::KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES | ||
) | ||
); | ||
|
||
self.active = true; | ||
} | ||
} | ||
pub fn exit(&mut self) { | ||
if self.active { | ||
let _ = execute!(std::io::stdout(), event::PopKeyboardEnhancementFlags); | ||
self.active = false; | ||
} | ||
} | ||
} | ||
|
||
impl Drop for KittyProtocolGuard { | ||
fn drop(&mut self) { | ||
if self.active { | ||
let _ = execute!(std::io::stdout(), event::PopKeyboardEnhancementFlags); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
pub(crate) mod bracketed_paste; | ||
pub(crate) mod kitty; | ||
|
||
/// Return if the terminal supports the kitty keyboard enhancement protocol | ||
/// | ||
/// Read more: https://sw.kovidgoyal.net/kitty/keyboard-protocol/ | ||
/// | ||
/// SIDE EFFECT: Touches the terminal file descriptors | ||
pub fn kitty_protocol_available() -> bool { | ||
crossterm::terminal::supports_keyboard_enhancement().unwrap_or_default() | ||
} |