aboutsummaryrefslogtreecommitdiff
path: root/helix-tui/src/backend/crossterm.rs
diff options
context:
space:
mode:
Diffstat (limited to 'helix-tui/src/backend/crossterm.rs')
-rw-r--r--helix-tui/src/backend/crossterm.rs70
1 files changed, 69 insertions, 1 deletions
diff --git a/helix-tui/src/backend/crossterm.rs b/helix-tui/src/backend/crossterm.rs
index 5305640c..e81c1e00 100644
--- a/helix-tui/src/backend/crossterm.rs
+++ b/helix-tui/src/backend/crossterm.rs
@@ -1,6 +1,11 @@
-use crate::{backend::Backend, buffer::Cell};
+use crate::{backend::Backend, buffer::Cell, terminal::Config};
use crossterm::{
cursor::{Hide, MoveTo, SetCursorStyle, Show},
+ event::{
+ DisableBracketedPaste, DisableFocusChange, DisableMouseCapture, EnableBracketedPaste,
+ EnableFocusChange, EnableMouseCapture, KeyboardEnhancementFlags,
+ PopKeyboardEnhancementFlags, PushKeyboardEnhancementFlags,
+ },
execute, queue,
style::{
Attribute as CAttribute, Color as CColor, Print, SetAttribute, SetBackgroundColor,
@@ -83,6 +88,69 @@ impl<W> Backend for CrosstermBackend<W>
where
W: Write,
{
+ fn claim(&mut self, config: Config) -> io::Result<()> {
+ terminal::enable_raw_mode()?;
+ execute!(
+ self.buffer,
+ terminal::EnterAlternateScreen,
+ EnableBracketedPaste,
+ EnableFocusChange
+ )?;
+ execute!(self.buffer, terminal::Clear(terminal::ClearType::All))?;
+ if config.enable_mouse_capture {
+ execute!(self.buffer, EnableMouseCapture)?;
+ }
+ if matches!(terminal::supports_keyboard_enhancement(), Ok(true)) {
+ log::debug!("The enhanced keyboard protocol is supported on this terminal");
+ execute!(
+ self.buffer,
+ PushKeyboardEnhancementFlags(
+ KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES
+ | KeyboardEnhancementFlags::REPORT_ALTERNATE_KEYS
+ )
+ )?;
+ } else {
+ log::debug!("The enhanced keyboard protocol is not supported on this terminal");
+ }
+ Ok(())
+ }
+
+ fn restore(&mut self, config: Config) -> io::Result<()> {
+ // reset cursor shape
+ write!(self.buffer, "\x1B[0 q")?;
+ if config.enable_mouse_capture {
+ execute!(self.buffer, DisableMouseCapture)?;
+ }
+ if matches!(terminal::supports_keyboard_enhancement(), Ok(true)) {
+ execute!(self.buffer, PopKeyboardEnhancementFlags)?;
+ }
+ execute!(
+ self.buffer,
+ DisableBracketedPaste,
+ DisableFocusChange,
+ terminal::LeaveAlternateScreen
+ )?;
+ terminal::disable_raw_mode()
+ }
+
+ fn force_restore() -> io::Result<()> {
+ let mut stdout = io::stdout();
+
+ // reset cursor shape
+ write!(stdout, "\x1B[0 q")?;
+ // Ignore errors on disabling, this might trigger on windows if we call
+ // disable without calling enable previously
+ let _ = execute!(stdout, DisableMouseCapture);
+ let _ = execute!(stdout, PopKeyboardEnhancementFlags);
+ execute!(
+ stdout,
+ DisableBracketedPaste,
+ DisableFocusChange,
+ terminal::LeaveAlternateScreen
+ )?;
+ terminal::disable_raw_mode()
+ }
+
fn draw<'a, I>(&mut self, content: I) -> io::Result<()>
where
I: Iterator<Item = (u16, u16, &'a Cell)>,