The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
fix(tui): write the screen again where a terminal may have kept stale cells (#592)
lingrd0bab2e022835
diff --git a/tui/src/app.rs b/tui/src/app.rsindex 31ed80b6e..23cff596f 100644--- a/tui/src/app.rs+++ b/tui/src/app.rs@@ -94,6 +94,14 @@ impl StartSession {/// The next in the order sessions are used in (a key, a switch, a new session): finer than/// #{session_activity}'s seconds, as tmux compares its activity times to the microsecond.+/// How long the wheel must rest before the screen is written whole once more.+pub const SCROLL_SETTLE: Duration = Duration::from_millis(250);++/// How long until the settle repaint is due (None: no scroll to settle; zero: due now).+pub fn scroll_settle_in(scrolled_at: Option<Instant>, now: Instant) -> Option<Duration> {+ scrolled_at.map(|at| (at + SCROLL_SETTLE).saturating_duration_since(now))+}+pub fn use_order() -> u64 {static N: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);N.fetch_add(1, std::sync::atomic::Ordering::Relaxed)@@ -470,6 +478,10 @@ pub struct App {pub repeat_until: Option<Instant>,/// Redraw everything next frame (refresh-client).pub redraw_all: bool,+ /// When the wheel last turned. A scroll rewrites most of the screen cell by cell; once it stops+ /// for SCROLL_SETTLE the whole screen is written once more, so a cell the terminal missed+ /// (a ghost of the old text) is overwritten whatever the reason it was missed.+ pub scrolled_at: Option<Instant>,/// tmux `status-position`.pub status_top: bool,pub mouse: bool,@@ -954,6 +966,7 @@ impl App {look: Default::default(),repeat_until: None,redraw_all: false,+ scrolled_at: None,status_top: false,mouse: true,loop_seen: Vec::new(),@@ -5986,6 +5999,25 @@ pub fn hostname() -> String {}).clone()}+#[cfg(test)]+mod scroll_settle_tests {+ use super::*;++ #[test]+ fn the_screen_is_written_whole_once_the_wheel_has_rested() {+ let t0 = Instant::now();+ // No scroll, nothing to settle.+ assert_eq!(scroll_settle_in(None, t0), None);+ // Just scrolled: the whole rest period is still owed.+ assert_eq!(scroll_settle_in(Some(t0), t0), Some(SCROLL_SETTLE));+ // Part of it has passed.+ assert_eq!(scroll_settle_in(Some(t0), t0 + SCROLL_SETTLE / 2), Some(SCROLL_SETTLE / 2));+ // Rested for long enough: due now (zero), and never negative.+ assert_eq!(scroll_settle_in(Some(t0), t0 + SCROLL_SETTLE), Some(Duration::ZERO));+ assert_eq!(scroll_settle_in(Some(t0), t0 + SCROLL_SETTLE * 4), Some(Duration::ZERO));+ }+}+#[cfg(test)]mod recovery_tests {use super::*;diff --git a/tui/src/input.rs b/tui/src/input.rsindex d315f1439..411c9dc13 100644--- a/tui/src/input.rs+++ b/tui/src/input.rs@@ -30,10 +30,19 @@ pub fn handle(app: &mut App, event: CEvent) {app.session_activity = now; app.session_used = crate::app::use_order(); on_key(app, key)}CEvent::Paste(text) => on_paste(app, text),- CEvent::Mouse(mouse) => { if app.mouse { on_mouse(app, mouse) } }- CEvent::Resize(cols, rows) => { app.size = (cols, rows); app.fit_panes(); crate::commands::notify(app, "client-resized", None, None) }+ CEvent::Mouse(mouse) => {+ if app.mouse {+ // A turn of the wheel: the screen is written whole once it rests (app.scrolled_at).+ if matches!(mouse.kind, MouseEventKind::ScrollUp | MouseEventKind::ScrollDown | MouseEventKind::ScrollLeft | MouseEventKind::ScrollRight) { app.scrolled_at = Some(Instant::now()) }+ on_mouse(app, mouse)+ }+ }+ // (A resize also writes the whole screen again: other diff renderers do the same, since a+ // terminal reflows its own cells and the diff then trusts a screen that is not there.)+ CEvent::Resize(cols, rows) => { app.size = (cols, rows); app.redraw_all = true; app.fit_panes(); crate::commands::notify(app, "client-resized", None, None) }// The terminal in front: the dial follows its pane again (and hears it is in front).- CEvent::FocusGained => { app.terminal_focused = true; app.welcome_back(); crate::dial::announce(app, false); app.announce_focus(); crate::commands::notify(app, "client-focus-in", None, None) }+ // (A repaint of the whole screen too: whatever the terminal drew wrongly while it was behind goes.)+ CEvent::FocusGained => { app.terminal_focused = true; app.redraw_all = true; app.welcome_back(); crate::dial::announce(app, false); app.announce_focus(); crate::commands::notify(app, "client-focus-in", None, None) }CEvent::FocusLost => { app.terminal_focused = false; app.away = Some((Instant::now(), app.fleet_counts())); crate::dial::announce(app, false); crate::commands::notify(app, "client-focus-out", None, None) }_ => {}}diff --git a/tui/src/main.rs b/tui/src/main.rsindex eb7e5dbb4..ac48d61cf 100644--- a/tui/src/main.rs+++ b/tui/src/main.rs@@ -479,6 +479,8 @@ async fn run(config: config::Config) -> io::Result<()> {// Wait for something — or for the frame we owe to come due.let wait = if need_draw { frame_budget.saturating_sub(last_draw.elapsed()) } else { Duration::from_secs(3600) };let wait = next_repaint.map(|at| wait.min(at.saturating_duration_since(Instant::now()))).unwrap_or(wait);+ // The wheel at rest: wake for the whole-screen repaint it owes.+ let wait = app::scroll_settle_in(app.scrolled_at, Instant::now()).map(|d| wait.min(d)).unwrap_or(wait);let first = tokio::select! {event = rx.recv() => event,_ = tokio::time::sleep(wait) => None,@@ -550,6 +552,9 @@ async fn run(config: config::Config) -> io::Result<()> {if all != mouse_all { execute!(term.backend_mut(), term_out::Mouse(if all { 2 } else { 1 }))?; mouse_all = all }app.flush_acks();if refill && matches!(app.modal, Some(modal::Modal::Picker { .. } | modal::Modal::NewHarness(_))) { input::refill(&mut app) }+ // A scroll that has rested: every cell of the screen written again, once. The terminal+ // may have kept a ghost of old text through the cell-by-cell updates; this overwrites it.+ if app::scroll_settle_in(app.scrolled_at, Instant::now()) == Some(Duration::ZERO) { app.scrolled_at = None; app.redraw_all = true }if std::mem::take(&mut app.redraw_all) { term.clear()?; need_draw = true; }if need_draw && last_draw.elapsed() >= frame_budget {// (The backend makes each frame's changes one synchronized update, and writes nothingdiff --git a/tui/src/term_out.rs b/tui/src/term_out.rsindex c4586140f..3ac0c5b53 100644--- a/tui/src/term_out.rs+++ b/tui/src/term_out.rs@@ -36,6 +36,12 @@ fn risky(symbol: &str) -> bool {if n > 1 { return true }let u = c as u32;if (0x0E00..=0x0FFF).contains(&u) || (0x1000..=0x109F).contains(&u) || (0x1780..=0x17FF).contains(&u) { return true }+ // One code point, but terminals and unicode-width part on it: private-use icons (Nerd+ // Font), the arrows, shapes and dingbats of ambiguous width (⚡ ✓ ● ▶), and emoji.+ // Box-drawing and block characters (U+2500–259F) are every border's and count alike.+ if (0xE000..=0xF8FF).contains(&u) || (0xF0000..=0x10FFFF).contains(&u)+ || ((0x2190..=0x2BFF).contains(&u) && !(0x2500..=0x259F).contains(&u))+ || (0x1F000..=0x1FAFF).contains(&u) { return true }}false}@@ -591,6 +597,19 @@ impl<W: Write> Backend for TmuxBackend<W> {mod tests {use super::*;+ #[test]+ fn single_symbols_terminals_count_otherwise_are_risky_too() {+ // Private-use icons (Nerd Font), arrows/shapes/dingbats of ambiguous width, and emoji+ // written as one code point: terminals disagree on their width as they do on clusters.+ for s in ["\u{e0a0}", "\u{f07b}", "\u{26a1}", "\u{2713}", "\u{25cf}", "\u{25b6}", "\u{1f44d}"] {+ assert!(risky(s), "{s:?} should be written with its row");+ }+ // Text, the box-drawing and block characters of every border, and wide CJK are counted alike everywhere.+ for s in ["a", "é", "中", "─", "│", "█", "▀"] {+ assert!(!risky(s), "{s:?} should stay a cell-by-cell update");+ }+ }+#[test]fn plain_echo_keeps_the_printed_cursor_and_needs_few_bytes() {let mut written = Vec::new();