diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs index df7ce871edbe..347c7e79d36d 100644 --- a/helix-term/src/application.rs +++ b/helix-term/src/application.rs @@ -18,7 +18,7 @@ use crate::{ use log::{error, warn}; use std::{ - io::{stdin, stdout, Write}, + io::stdin, sync::Arc, time::{Duration, Instant}, }; @@ -26,10 +26,10 @@ use std::{ use anyhow::Error; use crossterm::{ - event::{DisableMouseCapture, EnableMouseCapture, Event, EventStream}, + event::{DisableMouseCapture, Event, EventStream}, execute, terminal, - tty::IsTty, }; +use termwiz::istty::IsTty; #[cfg(not(windows))] use { signal_hook::{consts::signal, low_level}, @@ -805,26 +805,12 @@ impl Application { } } - async fn claim_term(&mut self) -> Result<(), Error> { - terminal::enable_raw_mode()?; - let mut stdout = stdout(); - execute!(stdout, terminal::EnterAlternateScreen)?; - if self.config.editor.mouse { - execute!(stdout, EnableMouseCapture)?; - } - Ok(()) + async fn claim_term(&mut self) -> Result<(), termwiz::Error> { + self.compositor.claim_term() } - fn restore_term(&mut self) -> Result<(), Error> { - let mut stdout = stdout(); - // reset cursor shape - write!(stdout, "\x1B[2 q")?; - // Ignore errors on disabling, this might trigger on windows if we call - // disable without calling enable previously - let _ = execute!(stdout, DisableMouseCapture); - execute!(stdout, terminal::LeaveAlternateScreen)?; - terminal::disable_raw_mode()?; - Ok(()) + fn restore_term(&mut self) -> Result<(), termwiz::Error> { + self.compositor.clear() } pub async fn run(&mut self) -> Result { diff --git a/helix-term/src/compositor.rs b/helix-term/src/compositor.rs index ce29eb4999cd..4291625800de 100644 --- a/helix-term/src/compositor.rs +++ b/helix-term/src/compositor.rs @@ -72,14 +72,14 @@ pub trait Component: Any + AnyComponent { } use termwiz::{ - caps::Capabilities, surface::CursorVisibility, terminal::buffered::BufferedTerminal, - terminal::SystemTerminal, + caps::Capabilities, + surface::CursorVisibility, + terminal::{buffered::BufferedTerminal, SystemTerminal, Terminal}, }; -type Terminal = BufferedTerminal; pub struct Compositor { layers: Vec>, - terminal: Terminal, + terminal: BufferedTerminal, surface: Surface, pub(crate) last_picker: Option>, @@ -248,6 +248,17 @@ impl Compositor { .find(|component| component.id() == Some(id)) .and_then(|component| component.as_any_mut().downcast_mut()) } + + pub fn claim_term(&mut self) -> Result<(), termwiz::Error> { + self.terminal.terminal().enter_alternate_screen()?; + self.terminal.terminal().set_raw_mode() + } + + pub(crate) fn clear(&mut self) -> Result<(), termwiz::Error> { + let inner_term = self.terminal.terminal(); + inner_term.exit_alternate_screen()?; + inner_term.set_cooked_mode() + } } // View casting, taken straight from Cursive