tui: Refactor Config type handling in backends

The `Config` can be passed when creating the backend (for example
`CrosstermBackend::new`) and is already updated in the
`Backend::reconfigure` callback. Recreating the tui `Config` during
`claim` and `restore` is unnecessary and causes a clone of the editor's
Config which is a fairly large type. This change drops the `Config`
parameter from those callbacks and updates the callers. Instead it is
passed to `CrosstermBackend` which then owns it.

I've also moved the override from the `editor.undercurl` key onto the
tui `Config` type - I believe it was just an oversight that this was not
done originally. And I've updated the `From<EditorConfig> for Config`
to take a reference to the editor's `Config` to avoid the unnecessary
clone during `CrosstermBackend::new` and `Backend::reconfigure`.
termina
Michael Davis 2025-04-08 11:37:31 -04:00
parent 63a1a94d92
commit 4c46871904
No known key found for this signature in database
5 changed files with 31 additions and 36 deletions

View File

@ -104,7 +104,7 @@ impl Application {
let theme_loader = theme::Loader::new(&theme_parent_dirs); let theme_loader = theme::Loader::new(&theme_parent_dirs);
#[cfg(not(feature = "integration"))] #[cfg(not(feature = "integration"))]
let backend = CrosstermBackend::new(stdout(), &config.editor); let backend = CrosstermBackend::new(stdout(), (&config.editor).into());
#[cfg(feature = "integration")] #[cfg(feature = "integration")]
let backend = TestBackend::new(120, 150); let backend = TestBackend::new(120, 150);
@ -365,7 +365,7 @@ impl Application {
ConfigEvent::Update(editor_config) => { ConfigEvent::Update(editor_config) => {
let mut app_config = (*self.config.load().clone()).clone(); let mut app_config = (*self.config.load().clone()).clone();
app_config.editor = *editor_config; app_config.editor = *editor_config;
if let Err(err) = self.terminal.reconfigure(app_config.editor.clone().into()) { if let Err(err) = self.terminal.reconfigure((&app_config.editor).into()) {
self.editor.set_error(err.to_string()); self.editor.set_error(err.to_string());
}; };
self.config.store(Arc::new(app_config)); self.config.store(Arc::new(app_config));
@ -409,8 +409,7 @@ impl Application {
self.refresh_language_config()?; self.refresh_language_config()?;
// Refresh theme after config change // Refresh theme after config change
Self::load_configured_theme(&mut self.editor, &default_config); Self::load_configured_theme(&mut self.editor, &default_config);
self.terminal self.terminal.reconfigure((&default_config.editor).into())?;
.reconfigure(default_config.editor.clone().into())?;
// Store new config // Store new config
self.config.store(Arc::new(default_config)); self.config.store(Arc::new(default_config));
Ok(()) Ok(())
@ -500,7 +499,7 @@ impl Application {
// https://github.com/neovim/neovim/issues/12322 // https://github.com/neovim/neovim/issues/12322
// https://github.com/neovim/neovim/pull/13084 // https://github.com/neovim/neovim/pull/13084
for retries in 1..=10 { for retries in 1..=10 {
match self.claim_term().await { match self.terminal.claim() {
Ok(()) => break, Ok(()) => break,
Err(err) if retries == 10 => panic!("Failed to claim terminal: {}", err), Err(err) if retries == 10 => panic!("Failed to claim terminal: {}", err),
Err(_) => continue, Err(_) => continue,
@ -1088,26 +1087,20 @@ impl Application {
lsp::ShowDocumentResult { success: true } lsp::ShowDocumentResult { success: true }
} }
async fn claim_term(&mut self) -> std::io::Result<()> {
let terminal_config = self.config.load().editor.clone().into();
self.terminal.claim(terminal_config)
}
fn restore_term(&mut self) -> std::io::Result<()> { fn restore_term(&mut self) -> std::io::Result<()> {
let terminal_config = self.config.load().editor.clone().into();
use helix_view::graphics::CursorKind; use helix_view::graphics::CursorKind;
self.terminal self.terminal
.backend_mut() .backend_mut()
.show_cursor(CursorKind::Block) .show_cursor(CursorKind::Block)
.ok(); .ok();
self.terminal.restore(terminal_config) self.terminal.restore()
} }
pub async fn run<S>(&mut self, input_stream: &mut S) -> Result<i32, Error> pub async fn run<S>(&mut self, input_stream: &mut S) -> Result<i32, Error>
where where
S: Stream<Item = std::io::Result<crossterm::event::Event>> + Unpin, S: Stream<Item = std::io::Result<crossterm::event::Event>> + Unpin,
{ {
self.claim_term().await?; self.terminal.claim()?;
// Exit the alternate screen and disable raw mode before panicking // Exit the alternate screen and disable raw mode before panicking
let hook = std::panic::take_hook(); let hook = std::panic::take_hook();

View File

@ -14,10 +14,7 @@ use crossterm::{
terminal::{self, Clear, ClearType}, terminal::{self, Clear, ClearType},
Command, Command,
}; };
use helix_view::{ use helix_view::graphics::{Color, CursorKind, Modifier, Rect, UnderlineStyle};
editor::Config as EditorConfig,
graphics::{Color, CursorKind, Modifier, Rect, UnderlineStyle},
};
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use std::{ use std::{
fmt, fmt,
@ -74,17 +71,17 @@ impl Capabilities {
/// on the $TERM environment variable. If detection fails, returns /// on the $TERM environment variable. If detection fails, returns
/// a default value where no capability is supported, or just undercurl /// a default value where no capability is supported, or just undercurl
/// if config.undercurl is set. /// if config.undercurl is set.
pub fn from_env_or_default(config: &EditorConfig) -> Self { pub fn from_env_or_default(config: &Config) -> Self {
match termini::TermInfo::from_env() { match termini::TermInfo::from_env() {
Err(_) => Capabilities { Err(_) => Capabilities {
has_extended_underlines: config.undercurl, has_extended_underlines: config.force_enable_extended_underlines,
..Capabilities::default() ..Capabilities::default()
}, },
Ok(t) => Capabilities { Ok(t) => Capabilities {
// Smulx, VTE: https://unix.stackexchange.com/a/696253/246284 // Smulx, VTE: https://unix.stackexchange.com/a/696253/246284
// Su (used by kitty): https://sw.kovidgoyal.net/kitty/underlines // Su (used by kitty): https://sw.kovidgoyal.net/kitty/underlines
// WezTerm supports underlines but a lot of distros don't properly install its terminfo // WezTerm supports underlines but a lot of distros don't properly install its terminfo
has_extended_underlines: config.undercurl has_extended_underlines: config.force_enable_extended_underlines
|| t.extended_cap("Smulx").is_some() || t.extended_cap("Smulx").is_some()
|| t.extended_cap("Su").is_some() || t.extended_cap("Su").is_some()
|| vte_version() >= Some(5102) || vte_version() >= Some(5102)
@ -97,6 +94,7 @@ impl Capabilities {
pub struct CrosstermBackend<W: Write> { pub struct CrosstermBackend<W: Write> {
buffer: W, buffer: W,
config: Config,
capabilities: Capabilities, capabilities: Capabilities,
supports_keyboard_enhancement_protocol: OnceCell<bool>, supports_keyboard_enhancement_protocol: OnceCell<bool>,
mouse_capture_enabled: bool, mouse_capture_enabled: bool,
@ -107,14 +105,15 @@ impl<W> CrosstermBackend<W>
where where
W: Write, W: Write,
{ {
pub fn new(buffer: W, config: &EditorConfig) -> CrosstermBackend<W> { pub fn new(buffer: W, config: Config) -> CrosstermBackend<W> {
// helix is not usable without colors, but crossterm will disable // helix is not usable without colors, but crossterm will disable
// them by default if NO_COLOR is set in the environment. Override // them by default if NO_COLOR is set in the environment. Override
// this behaviour. // this behaviour.
crossterm::style::force_color_output(true); crossterm::style::force_color_output(true);
CrosstermBackend { CrosstermBackend {
buffer, buffer,
capabilities: Capabilities::from_env_or_default(config), capabilities: Capabilities::from_env_or_default(&config),
config,
supports_keyboard_enhancement_protocol: OnceCell::new(), supports_keyboard_enhancement_protocol: OnceCell::new(),
mouse_capture_enabled: false, mouse_capture_enabled: false,
supports_bracketed_paste: true, supports_bracketed_paste: true,
@ -156,7 +155,7 @@ impl<W> Backend for CrosstermBackend<W>
where where
W: Write, W: Write,
{ {
fn claim(&mut self, config: Config) -> io::Result<()> { fn claim(&mut self) -> io::Result<()> {
terminal::enable_raw_mode()?; terminal::enable_raw_mode()?;
execute!( execute!(
self.buffer, self.buffer,
@ -172,7 +171,7 @@ where
Ok(_) => (), Ok(_) => (),
}; };
execute!(self.buffer, terminal::Clear(terminal::ClearType::All))?; execute!(self.buffer, terminal::Clear(terminal::ClearType::All))?;
if config.enable_mouse_capture { if self.config.enable_mouse_capture {
execute!(self.buffer, EnableMouseCapture)?; execute!(self.buffer, EnableMouseCapture)?;
self.mouse_capture_enabled = true; self.mouse_capture_enabled = true;
} }
@ -197,15 +196,16 @@ where
} }
self.mouse_capture_enabled = config.enable_mouse_capture; self.mouse_capture_enabled = config.enable_mouse_capture;
} }
self.config = config;
Ok(()) Ok(())
} }
fn restore(&mut self, config: Config) -> io::Result<()> { fn restore(&mut self) -> io::Result<()> {
// reset cursor shape // reset cursor shape
self.buffer self.buffer
.write_all(self.capabilities.reset_cursor_command.as_bytes())?; .write_all(self.capabilities.reset_cursor_command.as_bytes())?;
if config.enable_mouse_capture { if self.config.enable_mouse_capture {
execute!(self.buffer, DisableMouseCapture)?; execute!(self.buffer, DisableMouseCapture)?;
} }
if self.supports_keyboard_enhancement_protocol() { if self.supports_keyboard_enhancement_protocol() {

View File

@ -13,9 +13,9 @@ mod test;
pub use self::test::TestBackend; pub use self::test::TestBackend;
pub trait Backend { pub trait Backend {
fn claim(&mut self, config: Config) -> Result<(), io::Error>; fn claim(&mut self) -> Result<(), io::Error>;
fn reconfigure(&mut self, config: Config) -> Result<(), io::Error>; fn reconfigure(&mut self, config: Config) -> Result<(), io::Error>;
fn restore(&mut self, config: Config) -> Result<(), io::Error>; fn restore(&mut self) -> Result<(), io::Error>;
fn force_restore() -> Result<(), io::Error>; fn force_restore() -> Result<(), io::Error>;
fn draw<'a, I>(&mut self, content: I) -> Result<(), io::Error> fn draw<'a, I>(&mut self, content: I) -> Result<(), io::Error>
where where

View File

@ -107,7 +107,7 @@ impl TestBackend {
} }
impl Backend for TestBackend { impl Backend for TestBackend {
fn claim(&mut self, _config: Config) -> Result<(), io::Error> { fn claim(&mut self) -> Result<(), io::Error> {
Ok(()) Ok(())
} }
@ -115,7 +115,7 @@ impl Backend for TestBackend {
Ok(()) Ok(())
} }
fn restore(&mut self, _config: Config) -> Result<(), io::Error> { fn restore(&mut self) -> Result<(), io::Error> {
Ok(()) Ok(())
} }

View File

@ -20,12 +20,14 @@ pub struct Viewport {
#[derive(Debug)] #[derive(Debug)]
pub struct Config { pub struct Config {
pub enable_mouse_capture: bool, pub enable_mouse_capture: bool,
pub force_enable_extended_underlines: bool,
} }
impl From<EditorConfig> for Config { impl From<&EditorConfig> for Config {
fn from(config: EditorConfig) -> Self { fn from(config: &EditorConfig) -> Self {
Self { Self {
enable_mouse_capture: config.mouse, enable_mouse_capture: config.mouse,
force_enable_extended_underlines: config.undercurl,
} }
} }
} }
@ -98,16 +100,16 @@ where
}) })
} }
pub fn claim(&mut self, config: Config) -> io::Result<()> { pub fn claim(&mut self) -> io::Result<()> {
self.backend.claim(config) self.backend.claim()
} }
pub fn reconfigure(&mut self, config: Config) -> io::Result<()> { pub fn reconfigure(&mut self, config: Config) -> io::Result<()> {
self.backend.reconfigure(config) self.backend.reconfigure(config)
} }
pub fn restore(&mut self, config: Config) -> io::Result<()> { pub fn restore(&mut self) -> io::Result<()> {
self.backend.restore(config) self.backend.restore()
} }
// /// Get a Frame object which provides a consistent view into the terminal state for rendering. // /// Get a Frame object which provides a consistent view into the terminal state for rendering.