Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

perf(tui): share project snapshots and bound title rendering

RoversX6bab51c8caf5
diff --git a/Cargo.lock b/Cargo.lock
index 4da4b37d..00544586 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -7914,6 +7914,7 @@ dependencies = [
"codec",
"config",
"filedescriptor 0.8.3",
+ "finl_unicode 1.3.0",
"log",
"mux",
"promise",
diff --git a/thinkterm-tui/Cargo.toml b/thinkterm-tui/Cargo.toml
index e150b0cd..9ebddfdb 100644
--- a/thinkterm-tui/Cargo.toml
+++ b/thinkterm-tui/Cargo.toml
@@ -11,6 +11,7 @@ base64.workspace = true
codec.workspace = true
config.workspace = true
filedescriptor.workspace = true
+finl_unicode.workspace = true
log.workspace = true
mux.workspace = true
promise.workspace = true
diff --git a/thinkterm-tui/src/model.rs b/thinkterm-tui/src/model.rs
index fb878658..8a303e13 100644
--- a/thinkterm-tui/src/model.rs
+++ b/thinkterm-tui/src/model.rs
@@ -4,6 +4,7 @@ use codec::{
};
use mux::tab::TabId;
use std::collections::{BTreeMap, HashMap};
+use std::sync::Arc;
/// A thread is only unique inside one running server. Keep both the configured
/// transport name and the server runtime id in every UI key so that a restart
@@ -33,7 +34,7 @@ pub enum TreeNodeKey {
pub struct ThreadRow {
pub key: ThreadKey,
pub space: ThinkTermSessionSpace,
- pub project: ThinkTermSessionProject,
+ pub project: Arc<ThinkTermSessionProject>,
pub thread: ThinkTermSessionThread,
}
@@ -161,6 +162,8 @@ impl AppModel {
let Some(space) = spaces.get(project.space_id.as_str()) else {
continue;
};
+ // One shared project per rebuild, not one full thread list per row.
+ let project = Arc::new(project.clone());
for thread in &project.threads {
self.rows.push(ThreadRow {
key: ThreadKey {
@@ -169,7 +172,7 @@ impl AppModel {
thread_id: thread.id.clone(),
},
space: (*space).clone(),
- project: project.clone(),
+ project: Arc::clone(&project),
thread: thread.clone(),
});
}
@@ -463,3 +466,44 @@ mod tests {
assert_eq!(model.selected_tab().map(|tab| tab.tab_id), Some(7));
}
}
+
+#[cfg(test)]
+mod budget_tests {
+ use super::*;
+ #[test]
+ fn rows_share_projects_and_replace_them_on_new_snapshots() {
+ let mut model = AppModel::default();
+ let mut state = ThinkTermSessionState {
+ server_id: "server".into(),
+ generation: 1,
+ spaces: vec![ThinkTermSessionSpace {
+ id: "space".into(),
+ ..Default::default()
+ }],
+ projects: vec![ThinkTermSessionProject {
+ id: "project".into(),
+ space_id: "space".into(),
+ threads: (0..40)
+ .map(|i| ThinkTermSessionThread {
+ id: format!("thread-{i}"),
+ ..Default::default()
+ })
+ .collect(),
+ ..Default::default()
+ }],
+ ..Default::default()
+ };
+ assert!(model.apply_snapshot("remote", state.clone()));
+ assert_eq!(model.rows().len(), 40);
+ let old = model.rows()[0].project.clone();
+ assert!(model
+ .rows()
+ .iter()
+ .all(|row| Arc::ptr_eq(&old, &row.project)));
+ state.generation += 1;
+ state.projects[0].path = "/new".into();
+ assert!(model.apply_snapshot("remote", state));
+ assert_eq!(model.rows()[0].project.path, "/new");
+ assert!(!Arc::ptr_eq(&old, &model.rows()[0].project));
+ }
+}
diff --git a/thinkterm-tui/src/render.rs b/thinkterm-tui/src/render.rs
index 689a6825..b03d3ebf 100644
--- a/thinkterm-tui/src/render.rs
+++ b/thinkterm-tui/src/render.rs
@@ -1553,16 +1553,45 @@ fn draw_text(frame: &mut Frame<'_>, area: Rect, text: &str, style: Style) {
}
fn fit_text(text: &str, width: usize) -> String {
- let mut result = String::new();
- for ch in text.chars() {
- let mut candidate = result.clone();
- candidate.push(ch);
- if unicode_column_width(&candidate, None) > width {
+ use finl_unicode::grapheme_clusters::Graphemes;
+ use termwiz::cell::grapheme_column_width;
+ // Bound even a single enormous combining cluster before segmentation.
+ const MAX_BYTES: usize = 16 * 1024;
+ let mut limit = text.len().min(MAX_BYTES);
+ while !text.is_char_boundary(limit) {
+ limit -= 1;
+ }
+ let truncated = limit < text.len();
+ let mut end = 0;
+ let mut columns = 0usize;
+ for grapheme in Graphemes::new(&text[..limit]) {
+ let next = end + grapheme.len();
+ // The final cluster may have been cut by the byte budget.
+ if truncated && next == limit {
break;
}
- result.push(ch);
+ let next_columns = columns.saturating_add(grapheme_column_width(grapheme, None));
+ if next_columns > width {
+ break;
+ }
+ columns = next_columns;
+ end = next;
+ }
+ text[..end].to_string()
+}
+
+#[cfg(test)]
+mod text_budget_tests {
+ use super::fit_text;
+ #[test]
+ fn fitting_keeps_graphemes_and_bounds_zero_width_titles() {
+ assert_eq!(fit_text("abc", 2), "ab");
+ assert_eq!(fit_text("中文x", 3), "中");
+ assert_eq!(fit_text("e\u{301}x", 1), "e\u{301}");
+ assert_eq!(fit_text("👩‍💻x", 2), "👩‍💻");
+ assert!(fit_text(&"\u{200b}".repeat(100_000), 16).len() <= 16 * 1024);
+ assert!(fit_text(&format!("e{}", "\u{301}".repeat(100_000)), 16).is_empty());
}
- result
}
fn chrome() -> Style {