-
Notifications
You must be signed in to change notification settings - Fork 158
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
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)
- Loading branch information
1 parent
c853d71
commit ac88209
Showing
5 changed files
with
110 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use crossterm::{execute, event}; | ||
|
||
/// 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,52 @@ | ||
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 && crossterm::terminal::supports_keyboard_enhancement().unwrap_or_default(); | ||
} | ||
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,2 @@ | ||
pub(crate) mod kitty; | ||
pub(crate) mod bracketed_paste; |