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.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
feat(snippets): keep snippets in a crate of their own
RoversX2f3046e5d176
diff --git a/Cargo.lock b/Cargo.lockindex f6aa3075..f77be927 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -8635,6 +8635,20 @@ dependencies = ["wezterm-term",]+[[package]]+name = "thinkterm-snippets"+version = "0.1.0"+dependencies = [+ "anyhow",+ "criterion",+ "log",+ "memchr",+ "serde",+ "serde_json",+ "tempfile",+ "uuid",+]+[[package]]name = "thinkterm-syntax"version = "0.1.0"@@ -10520,6 +10534,7 @@ dependencies = ["thinkterm-i18n","thinkterm-proto","thinkterm-render",+ "thinkterm-snippets","thinkterm-syntax","thinkterm-update","thiserror 1.0.69",diff --git a/Cargo.toml b/Cargo.tomlindex 2e09490f..4db367f1 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -15,6 +15,7 @@ members = ["thinkterm-render","thinkterm-mobile","thinkterm-session",+ "thinkterm-snippets","thinkterm-web","thinkterm-tui","thinkterm-update",@@ -181,6 +182,7 @@ thinkterm-i18n = { path = "thinkterm-i18n" }thinkterm-proto = { path = "thinkterm-proto" }thinkterm-render = { path = "thinkterm-render" }thinkterm-session = { path = "thinkterm-session" }+thinkterm-snippets = { path = "thinkterm-snippets" }thinkterm-web = { path = "thinkterm-web" }thinkterm-update = { path = "thinkterm-update" }core-foundation = "=0.10.0"@@ -256,6 +258,7 @@ logging = { path = "lua-api-crates/logging" }lru = "0.16"luahelper = { path = "luahelper" }maplit = "1.0"+memchr = "2.7"memmap2 = "0.9"memmem = "0.1"metrics = "0.23"diff --git a/thinkterm-snippets/Cargo.toml b/thinkterm-snippets/Cargo.tomlnew file mode 100644index 00000000..c3641e5f--- /dev/null+++ b/thinkterm-snippets/Cargo.toml@@ -0,0 +1,28 @@+[package]+name = "thinkterm-snippets"+version = "0.1.0"+edition = "2021"+publish = false++[features]+default = ["native"]+# The store as a file on disk, and fresh random ids. Without it the crate is+# plain data and builds anywhere, WebAssembly included.+native = ["dep:log", "dep:tempfile", "dep:uuid"]++[dependencies]+anyhow.workspace = true+log = { workspace = true, optional = true }+memchr.workspace = true+serde = { workspace = true, features = ["derive"] }+serde_json.workspace = true+tempfile = { workspace = true, optional = true }+uuid = { workspace = true, features = ["v4"], optional = true }++[dev-dependencies]+criterion.workspace = true+tempfile.workspace = true++[[bench]]+name = "matching"+harness = falsediff --git a/thinkterm-snippets/benches/matching.rs b/thinkterm-snippets/benches/matching.rsnew file mode 100644index 00000000..b79656af--- /dev/null+++ b/thinkterm-snippets/benches/matching.rs@@ -0,0 +1,59 @@+//! What the Snippets panel pays per repaint to find and copy out the snippets+//! it shows: short commands by list size, and a thousand long scripts+//! searched for text they lack, including text that shares a long prefix+//! with them.++use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};+use thinkterm_snippets::{SnippetRecord, SnippetStore};++fn store_of(count: usize, body: impl Fn(usize) -> String) -> SnippetStore {+ let mut store = SnippetStore::default();+ for index in 0..count {+ store.create(+ format!("snippet-{index}"),+ &format!("Deploy step {index}"),+ body(index),+ index as u64,+ );+ }+ store+}++fn command(index: usize) -> String {+ format!("ssh server-a 'cd /srv/app && git pull --rebase && make build-{index}'")+}++/// About 8 KiB of shell script.+fn script(index: usize) -> String {+ (0..128)+ .map(|line| format!("echo \"step {index}.{line}\" && make target-{line}\n"))+ .collect()+}++fn found(store: &SnippetStore, query: &str) -> Vec<SnippetRecord> {+ store.matching(black_box(query)).cloned().collect()+}++fn matching(c: &mut Criterion) {+ let mut group = c.benchmark_group("matching");+ for count in [100, 1_000, 10_000] {+ let store = store_of(count, command);+ for (label, query) in [("blank", ""), ("typed", "Make Build-9")] {+ group.bench_with_input(BenchmarkId::new(label, count), &store, |b, store| {+ b.iter(|| found(store, query))+ });+ }+ }++ let scripts = store_of(1_000, script);+ group.bench_function("long/miss", |b| b.iter(|| found(&scripts, "kubectl rollout")));+ let prefixes = store_of(1_000, |_| "a".repeat(8_192));+ let near_miss = format!("{}b", "a".repeat(63));+ group.bench_function("long/shared-prefix", |b| {+ b.iter(|| found(&prefixes, &near_miss))+ });+ group.finish();+}++criterion_group!(benches, matching);+criterion_main!(benches);diff --git a/thinkterm-snippets/src/file.rs b/thinkterm-snippets/src/file.rsnew file mode 100644index 00000000..160ba529--- /dev/null+++ b/thinkterm-snippets/src/file.rs@@ -0,0 +1,90 @@+//! The store as a JSON file. Saving writes a new file beside the old one and+//! renames it into place, so a crash mid-write leaves the old file whole.++use crate::SnippetStore;+use anyhow::{Context, Result};+use std::fs;+use std::io::Write;+use std::path::Path;++/// Reads the store at `path`; a missing file is an empty store.+pub fn load(path: &Path) -> Result<SnippetStore> {+ if !path.exists() {+ return Ok(SnippetStore::default());+ }+ let text = fs::read_to_string(path).with_context(|| format!("read {}", path.display()))?;+ serde_json::from_str(&text).with_context(|| format!("parse {}", path.display()))+}++pub fn save(path: &Path, store: &SnippetStore) -> Result<()> {+ if let Some(parent) = path.parent() {+ fs::create_dir_all(parent).with_context(|| format!("create {}", parent.display()))?;+ let mut file = tempfile::NamedTempFile::new_in(parent)+ .with_context(|| format!("create temporary snippets store in {}", parent.display()))?;+ serde_json::to_writer_pretty(&mut file, store)+ .with_context(|| format!("write {}", path.display()))?;+ file.flush()+ .with_context(|| format!("flush {}", path.display()))?;+ file.as_file()+ .sync_all()+ .with_context(|| format!("sync {}", path.display()))?;+ file.persist(path)+ .with_context(|| format!("replace {}", path.display()))?;+ set_user_private_permissions(path);+ return Ok(());+ }++ let mut file = fs::File::create(path).with_context(|| format!("create {}", path.display()))?;+ serde_json::to_writer_pretty(&mut file, store)+ .with_context(|| format!("write {}", path.display()))?;+ file.flush()+ .with_context(|| format!("flush {}", path.display()))?;+ file.sync_all()+ .with_context(|| format!("sync {}", path.display()))?;+ set_user_private_permissions(path);+ Ok(())+}++#[cfg(unix)]+fn set_user_private_permissions(path: &Path) {+ use std::os::unix::fs::PermissionsExt;++ if let Err(err) = fs::set_permissions(path, fs::Permissions::from_mode(0o600)) {+ log::warn!(+ "failed to set private permissions on snippets store {}: {err:#}",+ path.display()+ );+ }+}++#[cfg(not(unix))]+fn set_user_private_permissions(_path: &Path) {}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn missing_store_loads_empty() {+ let dir = tempfile::tempdir().unwrap();+ let store = load(&dir.path().join("snippets.json")).unwrap();+ assert!(store.snippets().is_empty());+ }++ #[test]+ fn store_round_trips() {+ let dir = tempfile::tempdir().unwrap();+ let path = dir.path().join("snippets.json");+ let mut store = SnippetStore::default();+ store.create("snippet-1".into(), "List", "ls -la".into(), 1);+ store.create("snippet-2".into(), "", "pwd".into(), 2);+ store.delete("snippet-2", 3);+ save(&path, &store).unwrap();+ assert_eq!(load(&path).unwrap(), store);+ }++ #[test]+ fn new_ids_do_not_repeat_within_a_millisecond() {+ assert_ne!(crate::new_id(7), crate::new_id(7));+ }+}diff --git a/thinkterm-snippets/src/lib.rs b/thinkterm-snippets/src/lib.rsnew file mode 100644index 00000000..9fe4af9c--- /dev/null+++ b/thinkterm-snippets/src/lib.rs@@ -0,0 +1,536 @@+//! ThinkTerm's command snippets: the list, the changes made to it, and how+//! two copies of it come back together.+//!+//! Snippets are ordinary user data rather than secrets: command templates,+//! not a password vault. The store is plain data and builds anywhere,+//! WebAssembly included; keeping it in a file and minting ids need the+//! `native` feature.++use memchr::memmem;+use serde::{Deserialize, Serialize};+use std::cmp::Ordering;+use std::collections::HashMap;+use std::fmt;+use std::sync::OnceLock;++#[cfg(feature = "native")]+pub mod file;++pub type SnippetId = String;++#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]+pub struct SnippetRecord {+ pub id: SnippetId,+ pub title: String,+ pub body: String,+ pub created_at_ms: u64,+ pub updated_at_ms: u64,+}++/// A deletion, kept so that a copy which still has the snippet cannot bring+/// it back when the two are merged.+#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]+pub struct Tombstone {+ pub id: SnippetId,+ pub deleted_at_ms: u64,+}++#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]+pub struct SnippetStore {+ #[serde(default = "store_version")]+ version: u32,+ /// Newest first.+ #[serde(default)]+ snippets: Vec<SnippetRecord>,+ /// Apart from `snippets`, so a build from before deletions were kept+ /// reads the same file and sees only the live snippets.+ #[serde(default, skip_serializing_if = "Vec::is_empty")]+ deleted: Vec<Tombstone>,+ #[serde(skip)]+ folded: Folded,+}++impl Default for SnippetStore {+ fn default() -> Self {+ Self {+ version: store_version(),+ snippets: vec![],+ deleted: vec![],+ folded: Folded::default(),+ }+ }+}++fn store_version() -> u32 {+ 1+}++/// A fresh snippet id. Random, so two machines creating a snippet in the+/// same millisecond cannot collide when their copies meet.+#[cfg(feature = "native")]+pub fn new_id(now_ms: u64) -> SnippetId {+ format!("snippet-{now_ms}-{}", uuid::Uuid::new_v4().simple())+}++/// The first non-empty line of `body`, shortened: the title of a snippet+/// saved without one.+pub fn title_from_body(body: &str) -> String {+ body.lines()+ .find_map(|line| {+ let trimmed = line.trim();+ (!trimmed.is_empty()).then_some(trimmed)+ })+ .unwrap_or("Untitled snippet")+ .chars()+ .take(48)+ .collect()+}++fn normalized_title(title: &str, body: &str) -> String {+ let title = title.trim();+ if title.is_empty() {+ title_from_body(body)+ } else {+ title.chars().take(80).collect()+ }+}++impl SnippetStore {+ /// The live snippets, newest first.+ pub fn snippets(&self) -> &[SnippetRecord] {+ &self.snippets+ }++ pub fn get(&self, id: &str) -> Option<&SnippetRecord> {+ self.snippets.iter().find(|snippet| snippet.id == id)+ }++ /// The snippets whose title or body contains `query`, ignoring ASCII+ /// case, in list order; all of them for a blank query.+ pub fn matching<'a>(&'a self, query: &str) -> impl Iterator<Item = &'a SnippetRecord> + 'a {+ let query = query.trim().to_ascii_lowercase();+ let finder =+ (!query.is_empty()).then(|| memmem::Finder::new(query.as_bytes()).into_owned());+ let folded = finder.as_ref().map(|_| self.folded.of(&self.snippets));+ self.snippets+ .iter()+ .enumerate()+ .filter(move |(index, _)| match (&finder, folded) {+ (Some(finder), Some(folded)) => {+ let (title, body) = &folded[*index];+ finder.find(title.as_bytes()).is_some()+ || finder.find(body.as_bytes()).is_some()+ }+ _ => true,+ })+ .map(|(_, snippet)| snippet)+ }++ /// Adds a snippet at the top of the list.+ pub fn create(+ &mut self,+ id: SnippetId,+ title: &str,+ body: String,+ now_ms: u64,+ ) -> SnippetRecord {+ let record = SnippetRecord {+ id,+ title: normalized_title(title, &body),+ body,+ created_at_ms: now_ms,+ updated_at_ms: now_ms,+ };+ self.snippets.insert(0, record.clone());+ self.folded = Folded::default();+ record+ }++ pub fn update(+ &mut self,+ id: &str,+ title: &str,+ body: String,+ now_ms: u64,+ ) -> Option<SnippetRecord> {+ let record = self.snippets.iter_mut().find(|snippet| snippet.id == id)?;+ self.folded = Folded::default();+ record.title = normalized_title(title, &body);+ record.body = body;+ // Later than the change it replaces even if the clock went back, so+ // it wins a merge against the copy it was made from.+ record.updated_at_ms = now_ms.max(record.updated_at_ms.saturating_add(1));+ Some(record.clone())+ }++ pub fn delete(&mut self, id: &str, now_ms: u64) -> bool {+ let Some(index) = self.snippets.iter().position(|snippet| snippet.id == id) else {+ return false;+ };+ let record = self.snippets.remove(index);+ self.folded = Folded::default();+ let deleted_at_ms = now_ms.max(record.updated_at_ms.saturating_add(1));+ match self+ .deleted+ .iter_mut()+ .find(|tombstone| tombstone.id == record.id)+ {+ Some(tombstone) => {+ tombstone.deleted_at_ms = tombstone.deleted_at_ms.max(deleted_at_ms);+ }+ None => self.deleted.push(Tombstone {+ id: record.id,+ deleted_at_ms,+ }),+ }+ true+ }++ /// Brings another copy's changes in. The result depends only on what the+ /// two copies hold, not on which merges into which or how often: per+ /// snippet the later change wins, and a deletion wins over any change it+ /// is not older than. A snippet missing from one copy is never taken to+ /// be deleted -- only a tombstone deletes.+ pub fn merge(&mut self, other: &SnippetStore) {+ let mut deleted: HashMap<&str, u64> = HashMap::new();+ for tombstone in self.deleted.iter().chain(&other.deleted) {+ let at = deleted.entry(&tombstone.id).or_insert(0);+ *at = (*at).max(tombstone.deleted_at_ms);+ }++ let mut latest: HashMap<&str, &SnippetRecord> = HashMap::new();+ for snippet in self.snippets.iter().chain(&other.snippets) {+ let keep = latest+ .get(snippet.id.as_str())+ .map_or(true, |kept| wins_over(snippet, kept));+ if keep {+ latest.insert(&snippet.id, snippet);+ }+ }++ let mut snippets: Vec<SnippetRecord> = latest+ .into_values()+ .filter(|snippet| {+ deleted+ .get(snippet.id.as_str())+ .map_or(true, |&at| at < snippet.updated_at_ms)+ })+ .cloned()+ .collect();+ snippets.sort_by(newest_first);++ let mut tombstones: Vec<Tombstone> = deleted+ .into_iter()+ .map(|(id, deleted_at_ms)| Tombstone {+ id: id.to_owned(),+ deleted_at_ms,+ })+ .collect();+ tombstones.sort_by(|a, b| a.id.cmp(&b.id));++ self.snippets = snippets;+ self.deleted = tombstones;+ self.folded = Folded::default();+ }+}++/// Which of two versions of one snippet a merge keeps: the later change, and+/// on a tie the greater content, so both sides of a merge keep the same one.+fn wins_over(candidate: &SnippetRecord, kept: &SnippetRecord) -> bool {+ (+ candidate.updated_at_ms,+ &candidate.title,+ &candidate.body,+ candidate.created_at_ms,+ ) > (+ kept.updated_at_ms,+ &kept.title,+ &kept.body,+ kept.created_at_ms,+ )+}++fn newest_first(a: &SnippetRecord, b: &SnippetRecord) -> Ordering {+ b.created_at_ms+ .cmp(&a.created_at_ms)+ .then_with(|| a.id.cmp(&b.id))+}++/// Each snippet's title and body ASCII-lowercased, which is how `matching`+/// has always compared them. Built by the first search after a change and+/// dropped by the next change: it never holds more than the snippets' own+/// text, and a repaint neither lowercases nor copies anything to search.+#[derive(Default)]+struct Folded(OnceLock<Vec<(String, String)>>);++impl Folded {+ fn of(&self, snippets: &[SnippetRecord]) -> &[(String, String)] {+ self.0.get_or_init(|| {+ snippets+ .iter()+ .map(|snippet| {+ (+ snippet.title.to_ascii_lowercase(),+ snippet.body.to_ascii_lowercase(),+ )+ })+ .collect()+ })+ }+}++/// A cache, not part of the store's value.+impl Clone for Folded {+ fn clone(&self) -> Self {+ Self::default()+ }+}++impl PartialEq for Folded {+ fn eq(&self, _other: &Self) -> bool {+ true+ }+}++impl Eq for Folded {}++impl fmt::Debug for Folded {+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {+ f.write_str("Folded")+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ fn record(id: &str, body: &str, created_at_ms: u64, updated_at_ms: u64) -> SnippetRecord {+ SnippetRecord {+ id: id.to_string(),+ title: title_from_body(body),+ body: body.to_string(),+ created_at_ms,+ updated_at_ms,+ }+ }++ fn store(snippets: Vec<SnippetRecord>, deleted: Vec<(&str, u64)>) -> SnippetStore {+ let mut store = SnippetStore {+ version: 1,+ snippets,+ deleted: deleted+ .into_iter()+ .map(|(id, deleted_at_ms)| Tombstone {+ id: id.to_string(),+ deleted_at_ms,+ })+ .collect(),+ folded: Folded::default(),+ };+ // Canonical order, as a merge leaves it.+ store.merge(&SnippetStore::default());+ store+ }++ fn merged(a: &SnippetStore, b: &SnippetStore) -> SnippetStore {+ let mut out = a.clone();+ out.merge(b);+ out+ }++ fn ids(store: &SnippetStore) -> Vec<&str> {+ store.snippets().iter().map(|s| s.id.as_str()).collect()+ }++ #[test]+ fn title_falls_back_to_first_non_empty_body_line() {+ assert_eq!(title_from_body("\n ls -la\npwd"), "ls -la");+ }++ #[test]+ fn create_puts_the_new_snippet_first() {+ let mut store = SnippetStore::default();+ store.create("a".into(), "", "ls".into(), 1);+ store.create("b".into(), " Build ", "make".into(), 2);+ assert_eq!(ids(&store), ["b", "a"]);+ assert_eq!(store.get("b").unwrap().title, "Build");+ assert_eq!(store.get("a").unwrap().title, "ls");+ }++ #[test]+ fn an_edit_is_later_than_the_change_it_replaces_even_if_the_clock_went_back() {+ let mut store = SnippetStore::default();+ store.create("a".into(), "", "ls".into(), 100);+ let edited = store.update("a", "", "ls -la".into(), 50).unwrap();+ assert_eq!(edited.updated_at_ms, 101);+ assert!(store.update("missing", "", "pwd".into(), 200).is_none());+ }++ #[test]+ fn delete_leaves_a_tombstone() {+ let mut store = SnippetStore::default();+ store.create("a".into(), "", "ls".into(), 1);+ assert!(store.delete("a", 5));+ assert!(!store.delete("a", 6));+ assert!(store.snippets().is_empty());+ assert_eq!(+ store.deleted,+ [Tombstone {+ id: "a".into(),+ deleted_at_ms: 5+ }]+ );+ }++ #[test]+ fn matching_ignores_ascii_case_and_blank_queries() {+ let store = store(+ vec![+ record("a", "git PULL --rebase", 3, 3),+ record("b", "ls -la", 2, 2),+ record("c", "echo Ünïcode", 1, 1),+ ],+ vec![],+ );+ let found =+ |query: &str| -> Vec<&str> { store.matching(query).map(|s| s.id.as_str()).collect() };+ assert_eq!(found("pull"), ["a"]);+ assert_eq!(found(" Pull "), ["a"]);+ assert_eq!(found(""), ["a", "b", "c"]);+ assert_eq!(found(" "), ["a", "b", "c"]);+ // Only ASCII folds, as before: `Ü` is not `ü`.+ assert_eq!(found("Ünï"), ["c"]);+ assert!(found("ünï").is_empty());+ assert!(found("nothing").is_empty());+ }++ #[test]+ fn matching_answers_as_lowercasing_both_sides_did() {+ let long = "a".repeat(4096);+ let bodies = ["git PULL --rebase", "echo Ünïcode", "", "ab", long.as_str()];+ let mut snippets = SnippetStore::default();+ for (index, body) in bodies.iter().enumerate() {+ snippets.create(format!("s{index}"), "", body.to_string(), index as u64);+ }+ let near_miss = format!("{}b", "a".repeat(63));+ let upper = "A".repeat(64);+ for query in [+ "pull", "PuLL --", "Ünï", "ünï", "b", "a", &near_miss, &upper,+ ] {+ let needle = query.trim().to_ascii_lowercase();+ let expected: Vec<&str> = snippets+ .snippets()+ .iter()+ .filter(|s| {+ s.title.to_ascii_lowercase().contains(&needle)+ || s.body.to_ascii_lowercase().contains(&needle)+ })+ .map(|s| s.id.as_str())+ .collect();+ let found: Vec<&str> = snippets.matching(query).map(|s| s.id.as_str()).collect();+ assert_eq!(found, expected, "{query:?}");+ }+ }++ #[test]+ fn a_change_is_searchable_at_once() {+ let mut store = SnippetStore::default();+ store.create("a".into(), "", "ls".into(), 1);+ assert_eq!(store.matching("ls").count(), 1);+ store.update("a", "", "pwd".into(), 2);+ assert_eq!(store.matching("ls").count(), 0);+ assert_eq!(store.matching("pwd").count(), 1);+ store.create("b".into(), "", "ls -la".into(), 3);+ assert_eq!(store.matching("ls").count(), 1);+ store.delete("b", 4);+ assert_eq!(store.matching("ls").count(), 0);+ let mut other = SnippetStore::default();+ other.create("c".into(), "", "make".into(), 5);+ store.merge(&other);+ assert_eq!(store.matching("make").count(), 1);+ }++ #[test]+ fn the_later_edit_wins() {+ let ours = store(vec![record("a", "ls", 1, 10)], vec![]);+ let theirs = store(vec![record("a", "ls -la", 1, 20)], vec![]);+ assert_eq!(merged(&ours, &theirs).get("a").unwrap().body, "ls -la");+ assert_eq!(merged(&theirs, &ours).get("a").unwrap().body, "ls -la");+ }++ #[test]+ fn a_deletion_beats_an_older_edit_and_loses_to_a_newer_one() {+ let deleted = store(vec![], vec![("a", 15)]);+ let older_edit = store(vec![record("a", "ls", 1, 10)], vec![]);+ let newer_edit = store(vec![record("a", "ls -la", 1, 20)], vec![]);+ assert!(merged(&older_edit, &deleted).snippets().is_empty());+ assert_eq!(ids(&merged(&newer_edit, &deleted)), ["a"]);+ // A tie goes to the deletion.+ let same_moment = store(vec![record("a", "ls", 1, 15)], vec![]);+ assert!(merged(&same_moment, &deleted).snippets().is_empty());+ }++ #[test]+ fn a_snippet_missing_from_one_copy_is_kept() {+ let ours = store(vec![record("a", "ls", 1, 1)], vec![]);+ let theirs = store(vec![record("b", "pwd", 2, 2)], vec![]);+ assert_eq!(ids(&merged(&ours, &theirs)), ["b", "a"]);+ assert_eq!(ids(&merged(&ours, &SnippetStore::default())), ["a"]);+ }++ #[test]+ fn merging_is_order_independent_and_idempotent() {+ let a = store(+ vec![record("x", "ls", 1, 5), record("y", "pwd", 2, 2)],+ vec![("z", 9)],+ );+ let b = store(+ vec![record("x", "ls -la", 1, 7), record("z", "top", 3, 3)],+ vec![("y", 1)],+ );+ let c = store(+ vec![record("x", "ls -lh", 1, 7), record("w", "df -h", 4, 4)],+ vec![("x", 6)],+ );++ assert_eq!(merged(&a, &b), merged(&b, &a));+ assert_eq!(merged(&merged(&a, &b), &c), merged(&a, &merged(&b, &c)));+ let all = merged(&merged(&a, &b), &c);+ assert_eq!(merged(&all, &all), all);+ assert_eq!(merged(&all, &a), all);++ // x: the two edits at 7 tie on time, and the greater content wins+ // on both sides; the deletion at 6 is older. y: the deletion at 1 is+ // older than y's creation at 2. z: deleted at 9, after it was made.+ assert_eq!(ids(&all), ["w", "y", "x"]);+ assert_eq!(all.get("x").unwrap().body, "ls -lh");+ }++ #[test]+ fn a_file_without_tombstones_still_reads() {+ let store: SnippetStore = serde_json::from_str(+ r#"{"version":1,"snippets":[{"id":"a","title":"List","body":"ls","created_at_ms":1,"updated_at_ms":2}]}"#,+ )+ .unwrap();+ assert_eq!(ids(&store), ["a"]);+ assert!(store.deleted.is_empty());+ assert!(!serde_json::to_string(&store).unwrap().contains("deleted"));+ }++ #[test]+ fn a_build_from_before_tombstones_sees_only_live_snippets() {+ // The store as those builds declared it.+ #[derive(Deserialize)]+ struct Earlier {+ #[serde(default)]+ snippets: Vec<SnippetRecord>,+ }+ let mut current = SnippetStore::default();+ current.create("a".into(), "", "ls".into(), 1);+ current.create("b".into(), "", "pwd".into(), 2);+ current.delete("a", 3);+ let earlier: Earlier =+ serde_json::from_str(&serde_json::to_string(¤t).unwrap()).unwrap();+ assert_eq!(earlier.snippets, current.snippets);+ }+}diff --git a/wezterm-gui/Cargo.toml b/wezterm-gui/Cargo.tomlindex 3fb55e72..6c311652 100644--- a/wezterm-gui/Cargo.toml+++ b/wezterm-gui/Cargo.toml@@ -118,6 +118,7 @@ thiserror.workspace = truetiny-skia.workspace = truethinkterm-syntax = { path = "../thinkterm-syntax" }thinkterm-core.workspace = true+thinkterm-snippets.workspace = truetrash.workspace = trueumask.workspace = trueunicode-normalization.workspace = truediff --git a/wezterm-gui/src/snippets.rs b/wezterm-gui/src/snippets.rsindex 96a52d94..babf5ece 100644--- a/wezterm-gui/src/snippets.rs+++ b/wezterm-gui/src/snippets.rs@@ -1,54 +1,17 @@-//! ThinkTerm command snippets shown in the right sidebar.-//!-//! Snippets are intentionally stored as ordinary user data rather than as-//! secrets. They are command templates, not a password vault.+//! ThinkTerm command snippets shown in the right sidebar: this desktop's copy+//! of the `thinkterm-snippets` store, kept in the GUI's data directory.-use anyhow::{Context, Result};+use anyhow::Result;use parking_lot::Mutex;-use serde::{Deserialize, Serialize};-use std::fs;-use std::io::Write;use std::path::{Path, PathBuf};-use std::sync::atomic::{AtomicU64, Ordering};use std::time::{SystemTime, UNIX_EPOCH};+use thinkterm_snippets::SnippetStore;-pub type SnippetId = String;--#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]-pub struct SnippetRecord {- pub id: SnippetId,- pub title: String,- pub body: String,- pub created_at_ms: u64,- pub updated_at_ms: u64,-}--#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]-struct SnippetStore {- #[serde(default = "store_version")]- version: u32,- #[serde(default)]- snippets: Vec<SnippetRecord>,-}--impl Default for SnippetStore {- fn default() -> Self {- Self {- version: store_version(),- snippets: vec![],- }- }-}--fn store_version() -> u32 {- 1-}--static NEXT_SNIPPET_COUNTER: AtomicU64 = AtomicU64::new(1);+pub use thinkterm_snippets::SnippetRecord;lazy_static::lazy_static! {static ref SNIPPET_STORE: Mutex<SnippetStore> =- Mutex::new(load_snippet_store().unwrap_or_else(|err| {+ Mutex::new(thinkterm_snippets::file::load(&snippets_store_path()).unwrap_or_else(|err| {log::warn!("failed to load ThinkTerm snippets store: {err:#}");SnippetStore::default()}));@@ -65,181 +28,50 @@ fn now_ms() -> u64 {.unwrap_or(0)}-pub fn title_from_body(body: &str) -> String {- body.lines()- .find_map(|line| {- let trimmed = line.trim();- (!trimmed.is_empty()).then_some(trimmed)- })- .unwrap_or("Untitled snippet")- .chars()- .take(48)- .collect()-}--fn load_snippet_store() -> Result<SnippetStore> {- load_snippet_store_from_path(&snippets_store_path())-}-/// Whether `path` reads as a snippet store, as an imported backup must.pub(crate) fn check_snippet_store(path: &Path) -> Result<()> {- load_snippet_store_from_path(path).map(drop)-}--fn load_snippet_store_from_path(path: &Path) -> Result<SnippetStore> {- if !path.exists() {- return Ok(SnippetStore::default());- }- let text = fs::read_to_string(path).with_context(|| format!("read {}", path.display()))?;- serde_json::from_str(&text).with_context(|| format!("parse {}", path.display()))-}--fn save_snippet_store(store: &SnippetStore) -> Result<()> {- save_snippet_store_to_path(&snippets_store_path(), store)+ thinkterm_snippets::file::load(path).map(drop)}-fn save_snippet_store_to_path(path: &Path, store: &SnippetStore) -> Result<()> {- if let Some(parent) = path.parent() {- fs::create_dir_all(parent).with_context(|| format!("create {}", parent.display()))?;- let mut file = tempfile::NamedTempFile::new_in(parent)- .with_context(|| format!("create temporary snippets store in {}", parent.display()))?;- serde_json::to_writer_pretty(&mut file, store)- .with_context(|| format!("write {}", path.display()))?;- file.flush()- .with_context(|| format!("flush {}", path.display()))?;- file.as_file()- .sync_all()- .with_context(|| format!("sync {}", path.display()))?;- file.persist(path)- .with_context(|| format!("replace {}", path.display()))?;- set_user_private_permissions(path);- return Ok(());- }-- let mut file = fs::File::create(path).with_context(|| format!("create {}", path.display()))?;- serde_json::to_writer_pretty(&mut file, store)- .with_context(|| format!("write {}", path.display()))?;- file.flush()- .with_context(|| format!("flush {}", path.display()))?;- file.sync_all()- .with_context(|| format!("sync {}", path.display()))?;- set_user_private_permissions(path);- Ok(())+fn save(store: &SnippetStore) -> Result<()> {+ thinkterm_snippets::file::save(&snippets_store_path(), store)}-#[cfg(unix)]-fn set_user_private_permissions(path: &Path) {- use std::os::unix::fs::PermissionsExt;-- if let Err(err) = fs::set_permissions(path, fs::Permissions::from_mode(0o600)) {- log::warn!(- "failed to set private permissions on snippets store {}: {err:#}",- path.display()- );- }+/// The snippets the panel shows for `query`; see `SnippetStore::matching`.+pub fn matching_snippets(query: &str) -> Vec<SnippetRecord> {+ SNIPPET_STORE.lock().matching(query).cloned().collect()}-#[cfg(not(unix))]-fn set_user_private_permissions(_path: &Path) {}--pub fn list_snippets() -> Vec<SnippetRecord> {- SNIPPET_STORE.lock().snippets.clone()+pub fn matching_snippet_count(query: &str) -> usize {+ SNIPPET_STORE.lock().matching(query).count()}pub fn get_snippet(id: &str) -> Option<SnippetRecord> {- SNIPPET_STORE- .lock()- .snippets- .iter()- .find(|snippet| snippet.id == id)- .cloned()+ SNIPPET_STORE.lock().get(id).cloned()}pub fn create_snippet(title: String, body: String) -> Result<SnippetRecord> {let now = now_ms();- let title = normalized_title(title, &body);- let record = SnippetRecord {- id: format!(- "snippet-{}-{}",- now,- NEXT_SNIPPET_COUNTER.fetch_add(1, Ordering::Relaxed)- ),- title,- body,- created_at_ms: now,- updated_at_ms: now,- };let mut store = SNIPPET_STORE.lock();- store.snippets.insert(0, record.clone());- save_snippet_store(&store)?;+ let record = store.create(thinkterm_snippets::new_id(now), &title, body, now);+ save(&store)?;Ok(record)}pub fn update_snippet(id: &str, title: String, body: String) -> Result<Option<SnippetRecord>> {let mut store = SNIPPET_STORE.lock();- let Some(index) = store.snippets.iter().position(|snippet| snippet.id == id) else {+ let Some(record) = store.update(id, &title, body, now_ms()) else {return Ok(None);};- let mut record = store.snippets[index].clone();- record.title = normalized_title(title, &body);- record.body = body;- record.updated_at_ms = now_ms();- store.snippets[index] = record.clone();- save_snippet_store(&store)?;+ save(&store)?;Ok(Some(record))}pub fn delete_snippet(id: &str) -> Result<bool> {let mut store = SNIPPET_STORE.lock();- let before = store.snippets.len();- store.snippets.retain(|snippet| snippet.id != id);- let changed = store.snippets.len() != before;+ let changed = store.delete(id, now_ms());if changed {- save_snippet_store(&store)?;+ save(&store)?;}Ok(changed)}--fn normalized_title(title: String, body: &str) -> String {- let title = title.trim();- if title.is_empty() {- title_from_body(body)- } else {- title.chars().take(80).collect()- }-}--#[cfg(test)]-mod tests {- use super::*;-- #[test]- fn title_falls_back_to_first_non_empty_body_line() {- assert_eq!(title_from_body("\n ls -la\npwd"), "ls -la");- }-- #[test]- fn missing_store_loads_empty() {- let dir = tempfile::tempdir().unwrap();- let store = load_snippet_store_from_path(&dir.path().join("snippets.json")).unwrap();- assert!(store.snippets.is_empty());- }-- #[test]- fn store_round_trips() {- let dir = tempfile::tempdir().unwrap();- let path = dir.path().join("snippets.json");- let store = SnippetStore {- version: 1,- snippets: vec![SnippetRecord {- id: "snippet-1".to_string(),- title: "List".to_string(),- body: "ls -la".to_string(),- created_at_ms: 1,- updated_at_ms: 2,- }],- };- save_snippet_store_to_path(&path, &store).unwrap();- assert_eq!(load_snippet_store_from_path(&path).unwrap(), store);- }-}diff --git a/wezterm-gui/src/termwindow/ui/right_sidebar.rs b/wezterm-gui/src/termwindow/ui/right_sidebar.rsindex 09a5f48f..70e154f9 100644--- a/wezterm-gui/src/termwindow/ui/right_sidebar.rs+++ b/wezterm-gui/src/termwindow/ui/right_sidebar.rs@@ -6922,19 +6922,7 @@ impl crate::TermWindow {}fn filtered_snippet_count(&self) -> usize {- let needle = self- .right_sidebar_snippet_search- .text()- .trim()- .to_ascii_lowercase();- crate::snippets::list_snippets()- .into_iter()- .filter(|snippet| {- needle.is_empty()- || snippet.title.to_ascii_lowercase().contains(&needle)- || snippet.body.to_ascii_lowercase().contains(&needle)- })- .count()+ crate::snippets::matching_snippet_count(&self.right_sidebar_snippet_search.text())}pub fn paint_right_sidebar(@@ -7404,7 +7392,8 @@ impl crate::TermWindow {return Ok(());}RightSidebarMode::Snippets => {- self.paint_snippets_sidebar(+ let stage = crate::input_diagnostics::StageTimer::begin("snippet_paint");+ let result = self.paint_snippets_sidebar(layers,&ui_font,ui_metrics,@@ -7416,7 +7405,9 @@ impl crate::TermWindow {content_width,rect.y.saturating_add(rect.height),icon_size,- )?;+ );+ stage.finish(result.is_ok());+ result?;return Ok(());}RightSidebarMode::Tasks => {@@ -17242,19 +17233,9 @@ impl crate::TermWindow {.ui_px(SNIPPET_TOOLBAR_HEIGHT).max(self.ui_px(SNIPPET_SEARCH_HEIGHT))+ self.ui_px(SNIPPET_LIST_TOP_GAP);- let needle = self- .right_sidebar_snippet_search- .text()- .trim()- .to_ascii_lowercase();- let snippets: Vec<_> = crate::snippets::list_snippets()- .into_iter()- .filter(|snippet| {- needle.is_empty()- || snippet.title.to_ascii_lowercase().contains(&needle)- || snippet.body.to_ascii_lowercase().contains(&needle)- })- .collect();+ let query = self.right_sidebar_snippet_search.text();+ let searching = !query.trim().is_empty();+ let snippets = crate::snippets::matching_snippets(&query);let row_height = self.ui_px(SNIPPET_CARD_HEIGHT) + self.ui_px(SNIPPET_ROW_GAP);// The panel's bottom padding, which doubles as the mask that keeps// cards out of it; whatever overshoots it is cut by the window edge.@@ -17317,7 +17298,7 @@ impl crate::TermWindow {empty_icon_size,muted_fg,)?;- let empty_label = if needle.is_empty() {+ let empty_label = if !searching {crate::i18n::tr("right-no-snippets")} else {crate::i18n::tr("right-no-matching-snippets")