the runtime your coding agents live on
Rust agentagent-orchestrationaiai-agentsclaude-codeclicodexcoding-agentsdeveloper-toolsdevtoolsmultiplexerrustterminalterminal-multiplexerterminal-uitmuxtuiworkspace-manager
fix: prevent ghostty pane redraw corruption on wide cells
Can Celik46c65725036f
diff --git a/Cargo.lock b/Cargo.lockindex 01d01267c..8b66e213e 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -467,6 +467,7 @@ dependencies = ["tracing","tracing-subscriber","tui-term",+ "unicode-width","vt100",]diff --git a/Cargo.toml b/Cargo.tomlindex 59bb5af1d..f0849154c 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -30,6 +30,7 @@ toml = "0.8"tracing = "0.1.44"tracing-subscriber = { version = "0.3.23", features = ["env-filter"] }tui-term = "0.3"+unicode-width = "0.2"vt100 = "0.16"[patch.crates-io]diff --git a/src/ghostty/mod.rs b/src/ghostty/mod.rsindex fe76d00da..c52144725 100644--- a/src/ghostty/mod.rs+++ b/src/ghostty/mod.rs@@ -199,6 +199,26 @@ pub struct RenderColors {pub foreground: RgbColor,}+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub enum CellWide {+ Narrow,+ Wide,+ SpacerTail,+ SpacerHead,+}++impl CellWide {+ fn from_raw(value: ffi::GhosttyCellWide) -> Self {+ match value {+ ffi::GhosttyCellWide_GHOSTTY_CELL_WIDE_NARROW => Self::Narrow,+ ffi::GhosttyCellWide_GHOSTTY_CELL_WIDE_WIDE => Self::Wide,+ ffi::GhosttyCellWide_GHOSTTY_CELL_WIDE_SPACER_TAIL => Self::SpacerTail,+ ffi::GhosttyCellWide_GHOSTTY_CELL_WIDE_SPACER_HEAD => Self::SpacerHead,+ _ => Self::Narrow,+ }+ }+}+struct WritePtyCallbackState {callback: Box<dyn FnMut(&[u8]) + Send>,}@@ -917,6 +937,33 @@ impl<'a> RowCellIter<'a> {unsafe { ffi::ghostty_render_state_row_cells_select(self.cells.raw, x).into_result() }}+ fn raw_cell(&self) -> Result<ffi::GhosttyCell, Error> {+ let mut raw = ffi::GhosttyCell::default();+ unsafe {+ ffi::ghostty_render_state_row_cells_get(+ self.cells.raw,+ ffi::GhosttyRenderStateRowCellsData_GHOSTTY_RENDER_STATE_ROW_CELLS_DATA_RAW,+ (&mut raw as *mut ffi::GhosttyCell).cast(),+ )+ .into_result()?;+ }+ Ok(raw)+ }++ pub fn wide(&self) -> Result<CellWide, Error> {+ let raw = self.raw_cell()?;+ let mut wide = ffi::GhosttyCellWide_GHOSTTY_CELL_WIDE_NARROW;+ unsafe {+ ffi::ghostty_cell_get(+ raw,+ ffi::GhosttyCellData_GHOSTTY_CELL_DATA_WIDE,+ (&mut wide as *mut ffi::GhosttyCellWide).cast(),+ )+ .into_result()?;+ }+ Ok(CellWide::from_raw(wide))+ }+pub fn style(&self) -> Result<CellStyle, Error> {let mut style = ffi::GhosttyStyle {size: mem::size_of::<ffi::GhosttyStyle>(),diff --git a/src/pane.rs b/src/pane.rsindex 2e0140275..3a4c48640 100644--- a/src/pane.rs+++ b/src/pane.rs@@ -13,6 +13,8 @@ use ratatui::{layout::Rect, Frame};use tokio::sync::{mpsc, Notify};use tracing::{debug, error, info, warn};use tui_term::widget::PseudoTerminal;+#[cfg(feature = "ghostty-vt")]+use unicode_width::UnicodeWidthStr;use crate::detect::{Agent, AgentState};use crate::events::AppEvent;@@ -965,22 +967,19 @@ impl GhosttyPaneTerminal {};let mut x = 0u16;while x < area.width && cells.next() {+ let wide = cells.wide().unwrap_or(crate::ghostty::CellWide::Narrow);let style = ghostty_cell_style(&cells, default_fg, default_bg);- let symbol = ghostty_cell_symbol(&cells).unwrap_or_else(|_| " ".to_string());+ let symbol = ghostty_buffer_symbol(&cells, wide)+ .unwrap_or_else(|_| ghostty_blank_symbol_for_width(wide).to_string());let cell = &mut buf[(area.x + x, area.y + y)];+ cell.reset();cell.set_symbol(&symbol);cell.set_style(style);x += 1;}while x < area.width {let cell = &mut buf[(area.x + x, area.y + y)];- cell.set_symbol(" ");- if let Some(bg) = default_bg {- cell.set_bg(bg);- }- if let Some(fg) = default_fg {- cell.set_fg(fg);- }+ ghostty_reset_cell(cell, default_fg, default_bg);x += 1;}y += 1;@@ -988,13 +987,7 @@ impl GhosttyPaneTerminal {while y < area.height {for x in 0..area.width {let cell = &mut buf[(area.x + x, area.y + y)];- cell.set_symbol(" ");- if let Some(bg) = default_bg {- cell.set_bg(bg);- }- if let Some(fg) = default_fg {- cell.set_fg(fg);- }+ ghostty_reset_cell(cell, default_fg, default_bg);}y += 1;}@@ -1453,6 +1446,60 @@ fn ghostty_cell_symbol(Ok(text)}+#[cfg(feature = "ghostty-vt")]+fn ghostty_blank_symbol_for_width(wide: crate::ghostty::CellWide) -> &'static str {+ match wide {+ crate::ghostty::CellWide::Wide => " ",+ crate::ghostty::CellWide::SpacerTail => "",+ crate::ghostty::CellWide::Narrow | crate::ghostty::CellWide::SpacerHead => " ",+ }+}++#[cfg(feature = "ghostty-vt")]+fn ghostty_normalize_buffer_symbol(symbol: &str, wide: crate::ghostty::CellWide) -> String {+ let expected_width = match wide {+ crate::ghostty::CellWide::Wide => 2,+ crate::ghostty::CellWide::Narrow | crate::ghostty::CellWide::SpacerHead => 1,+ crate::ghostty::CellWide::SpacerTail => 0,+ };+ let actual_width = symbol.width();+ if actual_width == expected_width {+ return symbol.to_string();+ }+ ghostty_blank_symbol_for_width(wide).to_string()+}++#[cfg(feature = "ghostty-vt")]+fn ghostty_buffer_symbol(+ cells: &crate::ghostty::RowCellIter<'_>,+ wide: crate::ghostty::CellWide,+) -> Result<String, crate::ghostty::Error> {+ let symbol = match wide {+ crate::ghostty::CellWide::SpacerTail => String::new(),+ crate::ghostty::CellWide::SpacerHead => " ".to_string(),+ crate::ghostty::CellWide::Narrow | crate::ghostty::CellWide::Wide => {+ ghostty_cell_symbol(cells)?+ }+ };+ Ok(ghostty_normalize_buffer_symbol(&symbol, wide))+}++#[cfg(feature = "ghostty-vt")]+fn ghostty_reset_cell(+ cell: &mut ratatui::buffer::Cell,+ default_fg: Option<Color>,+ default_bg: Option<Color>,+) {+ cell.reset();+ cell.set_symbol(" ");+ if let Some(bg) = default_bg {+ cell.set_bg(bg);+ }+ if let Some(fg) = default_fg {+ cell.set_fg(fg);+ }+}+#[cfg(feature = "ghostty-vt")]fn ghostty_cell_style(cells: &crate::ghostty::RowCellIter<'_>,@@ -2274,6 +2321,31 @@ mod tests {assert_eq!(encoded.as_deref(), Some(&b"\x1b[<36;5;7M"[..]));}+ #[cfg(feature = "ghostty-vt")]+ #[test]+ fn ghostty_normalize_buffer_symbol_uses_ratatui_width_contract() {+ assert_eq!(+ ghostty_normalize_buffer_symbol("π", crate::ghostty::CellWide::Wide),+ "π"+ );+ assert_eq!(+ ghostty_normalize_buffer_symbol("a", crate::ghostty::CellWide::Wide),+ " "+ );+ assert_eq!(+ ghostty_normalize_buffer_symbol("β¨οΈ", crate::ghostty::CellWide::Narrow),+ " "+ );+ assert_eq!(+ ghostty_normalize_buffer_symbol(" ", crate::ghostty::CellWide::SpacerTail),+ ""+ );+ assert_eq!(+ ghostty_normalize_buffer_symbol("xx", crate::ghostty::CellWide::SpacerHead),+ " "+ );+ }+#[cfg(feature = "ghostty-vt")]#[tokio::test]async fn focus_events_are_forwarded_when_enabled() {