1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//! Terminal session restore (iTerm2 / WezTerm / Zellij's headline
//! "never lose my workspace" feature, scoped to croft's terminal panel):
//! the pane layout — count, order, per-pane cwd and manual name, and which
//! pane was active — is persisted per workspace root and resurrected on
//! the next croft launch in that workspace. Panes come back as fresh
//! shells in their directories (running programs are not re-run).
//!
//! One JSON file, `~/.config/croft/terminal_sessions.json`, maps workspace
//! root → [`SessionRecord`]. Saves happen on structural changes (split /
//! close / rename / reorder / undo close) and at quit; a trivial record
//! (one unnamed pane sitting at the workspace root — the default layout)
//! is pruned so plain workspaces never accumulate entries.
use std::collections::HashMap;
use std::path::{Path, PathBuf};
/// One pane to bring back.
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct PaneRecord {
pub cwd: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
}
/// A workspace's terminal panel layout.
#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct SessionRecord {
pub panes: Vec<PaneRecord>,
#[serde(default)]
pub active: usize,
}
impl SessionRecord {
/// The default layout every workspace starts with; storing it would be
/// noise, so [`save_for_root`] prunes it instead.
pub fn is_trivial(&self, root: &str) -> bool {
match self.panes.as_slice() {
[] => true,
[only] => only.name.is_none() && same_dir(&only.cwd, root),
_ => false,
}
}
}
/// Directory equality through symlinks: a pane cwd sampled from the
/// process table comes back canonicalised (`/private/var/…` on macOS)
/// while the workspace root string may not be.
fn same_dir(a: &str, b: &str) -> bool {
if a == b {
return true;
}
match (std::fs::canonicalize(a), std::fs::canonicalize(b)) {
(Ok(x), Ok(y)) => x == y,
_ => false,
}
}
/// The real store path. The App caches it in a field so tests can point
/// saves and restores at a tempdir.
pub fn path() -> PathBuf {
crate::prefs::config_dir().join("terminal_sessions.json")
}
/// Load the whole map, tolerantly: unreadable or unparsable files are an
/// empty map, never an error.
pub fn load(path: &Path) -> HashMap<String, SessionRecord> {
let raw = std::fs::read_to_string(path).unwrap_or_default();
serde_json::from_str(&raw).unwrap_or_default()
}
/// Read-modify-write one workspace's record; a trivial record removes the
/// key instead.
pub fn save_for_root(path: &Path, root: &str, record: SessionRecord) {
let mut map = load(path);
if record.is_trivial(root) {
map.remove(root);
} else {
map.insert(root.to_string(), record);
}
if let Some(dir) = path.parent() {
let _ = std::fs::create_dir_all(dir);
}
if let Ok(json) = serde_json::to_string_pretty(&map) {
let _ = std::fs::write(path, json);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn save_round_trips_and_trivial_records_prune() {
let tmp = tempfile::tempdir().unwrap();
let p = tmp.path().join("s.json");
let rec = SessionRecord {
panes: vec![
PaneRecord {
cwd: String::from("/repo"),
name: None,
},
PaneRecord {
cwd: String::from("/repo/sub"),
name: Some(String::from("srv")),
},
],
active: 1,
};
save_for_root(&p, "/repo", rec.clone());
let map = load(&p);
assert_eq!(map.get("/repo"), Some(&rec));
// Back to the default single-pane layout: the key is pruned.
save_for_root(
&p,
"/repo",
SessionRecord {
panes: vec![PaneRecord {
cwd: String::from("/repo"),
name: None,
}],
active: 0,
},
);
assert!(!load(&p).contains_key("/repo"));
// Corrupt files load as empty, never panic.
std::fs::write(&p, "not json").unwrap();
assert!(load(&p).is_empty());
}
}