Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-tui/src/backend/crossterm.rs')
| -rw-r--r-- | helix-tui/src/backend/crossterm.rs | 128 |
1 files changed, 54 insertions, 74 deletions
diff --git a/helix-tui/src/backend/crossterm.rs b/helix-tui/src/backend/crossterm.rs index 3b53c21f..c55ab6bb 100644 --- a/helix-tui/src/backend/crossterm.rs +++ b/helix-tui/src/backend/crossterm.rs @@ -8,45 +8,28 @@ use crossterm::{ }, execute, queue, style::{ - Attribute as CAttribute, Color as CColor, Colors, Print, SetAttribute, SetBackgroundColor, - SetColors, SetForegroundColor, + Attribute as CAttribute, Color as CColor, Print, SetAttribute, SetBackgroundColor, + SetForegroundColor, }, terminal::{self, Clear, ClearType}, Command, }; -use helix_view::graphics::{Color, CursorKind, Modifier, Rect, UnderlineStyle}; +use helix_view::{ + editor::Config as EditorConfig, + graphics::{Color, CursorKind, Modifier, Rect, UnderlineStyle}, +}; use once_cell::sync::OnceCell; use std::{ fmt, io::{self, Write}, }; -use termini::TermInfo; fn term_program() -> Option<String> { - // Some terminals don't set $TERM_PROGRAM - match std::env::var("TERM_PROGRAM") { - Err(_) => std::env::var("TERM").ok(), - Ok(term_program) => Some(term_program), - } + std::env::var("TERM_PROGRAM").ok() } fn vte_version() -> Option<usize> { std::env::var("VTE_VERSION").ok()?.parse().ok() } -fn reset_cursor_approach(terminfo: TermInfo) -> String { - let mut reset_str = "\x1B[0 q".to_string(); - - if let Some(termini::Value::Utf8String(se_str)) = terminfo.extended_cap("Se") { - reset_str.push_str(se_str); - }; - - reset_str.push_str( - terminfo - .utf8_string_cap(termini::StringCapability::CursorNormal) - .unwrap_or(""), - ); - - reset_str -} /// Describes terminal capabilities like extended underline, truecolor, etc. #[derive(Clone, Debug)] @@ -69,55 +52,45 @@ impl Default for Capabilities { impl Capabilities { /// Detect capabilities from the terminfo database located based /// on the $TERM environment variable. If detection fails, returns - /// a default value where no capability is supported, or just undercurl - /// if config.undercurl is set. - pub fn from_env_or_default(config: &Config) -> Self { + /// a default value where no capability is supported. + pub fn from_env_or_default(config: &EditorConfig) -> Self { match termini::TermInfo::from_env() { - Err(_) => Capabilities { - has_extended_underlines: config.force_enable_extended_underlines, - ..Capabilities::default() - }, + Err(_) => Capabilities::default(), Ok(t) => Capabilities { // Smulx, VTE: https://unix.stackexchange.com/a/696253/246284 // Su (used by kitty): https://sw.kovidgoyal.net/kitty/underlines - // WezTerm supports underlines but a lot of distros don't properly install its terminfo - has_extended_underlines: config.force_enable_extended_underlines + // WezTerm supports underlines but a lot of distros don't properly install it's terminfo + has_extended_underlines: config.undercurl || t.extended_cap("Smulx").is_some() || t.extended_cap("Su").is_some() || vte_version() >= Some(5102) || matches!(term_program().as_deref(), Some("WezTerm")), - reset_cursor_command: reset_cursor_approach(t), + reset_cursor_command: t + .utf8_string_cap(termini::StringCapability::CursorNormal) + .unwrap_or("\x1B[0 q") + .to_string(), }, } } } -/// Terminal backend supporting a wide variety of terminals pub struct CrosstermBackend<W: Write> { buffer: W, - config: Config, capabilities: Capabilities, supports_keyboard_enhancement_protocol: OnceCell<bool>, mouse_capture_enabled: bool, - supports_bracketed_paste: bool, } impl<W> CrosstermBackend<W> where W: Write, { - pub fn new(buffer: W, config: Config) -> CrosstermBackend<W> { - // helix is not usable without colors, but crossterm will disable - // them by default if NO_COLOR is set in the environment. Override - // this behaviour. - crossterm::style::force_color_output(true); + pub fn new(buffer: W, config: &EditorConfig) -> CrosstermBackend<W> { CrosstermBackend { buffer, - capabilities: Capabilities::from_env_or_default(&config), - config, + capabilities: Capabilities::from_env_or_default(config), supports_keyboard_enhancement_protocol: OnceCell::new(), mouse_capture_enabled: false, - supports_bracketed_paste: true, } } @@ -156,23 +129,16 @@ impl<W> Backend for CrosstermBackend<W> where W: Write, { - fn claim(&mut self) -> io::Result<()> { + fn claim(&mut self, config: Config) -> io::Result<()> { terminal::enable_raw_mode()?; execute!( self.buffer, terminal::EnterAlternateScreen, + EnableBracketedPaste, EnableFocusChange )?; - match execute!(self.buffer, EnableBracketedPaste,) { - Err(err) if err.kind() == io::ErrorKind::Unsupported => { - log::warn!("Bracketed paste is not supported on this terminal."); - self.supports_bracketed_paste = false; - } - Err(err) => return Err(err), - Ok(_) => (), - }; execute!(self.buffer, terminal::Clear(terminal::ClearType::All))?; - if self.config.enable_mouse_capture { + if config.enable_mouse_capture { execute!(self.buffer, EnableMouseCapture)?; self.mouse_capture_enabled = true; } @@ -197,26 +163,41 @@ where } self.mouse_capture_enabled = config.enable_mouse_capture; } - self.config = config; Ok(()) } - fn restore(&mut self) -> io::Result<()> { + fn restore(&mut self, config: Config) -> io::Result<()> { // reset cursor shape self.buffer .write_all(self.capabilities.reset_cursor_command.as_bytes())?; - if self.config.enable_mouse_capture { + if config.enable_mouse_capture { execute!(self.buffer, DisableMouseCapture)?; } if self.supports_keyboard_enhancement_protocol() { execute!(self.buffer, PopKeyboardEnhancementFlags)?; } - if self.supports_bracketed_paste { - execute!(self.buffer, DisableBracketedPaste,)?; - } 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 )?; @@ -247,12 +228,14 @@ where diff.queue(&mut self.buffer)?; modifier = cell.modifier; } - if cell.fg != fg || cell.bg != bg { - queue!( - self.buffer, - SetColors(Colors::new(cell.fg.into(), cell.bg.into())) - )?; + if cell.fg != fg { + let color = CColor::from(cell.fg); + queue!(self.buffer, SetForegroundColor(color))?; fg = cell.fg; + } + if cell.bg != bg { + let color = CColor::from(cell.bg); + queue!(self.buffer, SetBackgroundColor(color))?; bg = cell.bg; } @@ -302,6 +285,11 @@ where execute!(self.buffer, Show, shape) } + fn get_cursor(&mut self) -> io::Result<(u16, u16)> { + crossterm::cursor::position() + .map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string())) + } + fn set_cursor(&mut self, x: u16, y: u16) -> io::Result<()> { execute!(self.buffer, MoveTo(x, y)) } @@ -320,14 +308,6 @@ where fn flush(&mut self) -> io::Result<()> { self.buffer.flush() } - - fn supports_true_color(&self) -> bool { - false - } - - fn get_theme_mode(&self) -> Option<helix_view::theme::Mode> { - None - } } #[derive(Debug)] |