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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//! Three-pane miller browser over org → repo → dir → file (PLAN.md §1).
//! Yazi semantics: `h` moves focus left into the parent column, where
//! j/k then browse the parent — the child column rebuilds (cascades)
//! from the new selection. `l` drills back in / deeper.
//! Org repos and repo trees arrive asynchronously from the GitHub API;
//! no mock content is ever shown for trees (honest empty until loaded).
mod marks;
mod navigation;
mod presentation;
mod revision;
mod blobs;
pub(crate) use blobs::CachedBlob;
pub(crate) use lenses::{BlameState, History, HistoryDiagnostics};
pub(crate) mod lenses;
use super::Component;
use super::pane::{Entry, EntryKind, Pane};
use super::preview::Preview;
use super::vim_input::VimInput;
use crate::action::Action;
use crate::theme::Theme;
use ratatui::Frame;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use rootle_provider::TreeNode;
use std::collections::{HashMap, HashSet};
mod tree;
pub use tree::RepoTree;
pub struct Browser {
/// Level stack: levels[i] lists the children of the selection in
/// levels[i-1]. levels[0] = orgs, levels[1] = repos, 2 = repo root,
/// 3+ = dirs.
levels: Vec<Pane>,
/// Which level owns the keyboard. Default = deepest level.
focus: usize,
tree: Option<RepoTree>,
/// Blob content by sha (in-memory, session-scoped): sanitized raw
/// text + highlighted lines. Lines are cached so navigation never
/// re-highlights; the raw text lets a theme switch restyle
/// everything without a refetch.
blobs: HashMap<String, CachedBlob>,
/// Shas with an in-flight fetch (dedupe worker spawns).
pending_blobs: HashSet<String>,
/// Shas whose fetch failed, with the error — re-selecting re-shows
/// the error instead of a "loading…" placeholder nothing resolves
/// (0023 breaker). Cleared by explicit reload (the retry path).
failed_blobs: HashMap<String, String>,
/// Set by refresh when the selected file needs a fetch; the app
/// drains it via `take_blob_request` and routes `LoadBlob`.
blob_request: Option<(String, String)>,
pub preview: Preview,
/// `/` filter input, owned here, active in SEARCH mode.
pub filter_input: VimInput,
/// `␣ /` find-in-file input, active in FIND mode (plans/0007 §3).
pub find_input: VimInput,
/// VISUAL mode (plans/0004 §1): marked entries, keyed by
/// [`MarkKey`] so marks survive cascades.
visual: bool,
marks: std::collections::HashSet<MarkKey>,
/// plans/0016 M1a: the browsed revision (None = default branch).
current_ref: Option<String>,
/// plans/0016 M1b: the file-history lens over the preview pane.
/// Some(_) while active; the preview survives underneath.
history: Option<History>,
/// plans/0016 M1c: blame ranges for the previewed file.
blame: Option<BlameState>,
/// plans/0028: the commit viewer over the preview pane, entered
/// from the history lens (`d`).
commit: Option<crate::components::commit::CommitView>,
/// Viewing a file at a commit (history Enter): the restore point
/// is the present-day blob — (path, sha) — re-rendered from the
/// in-memory cache on the way back.
at_commit: Option<(String, String)>,
}
impl Default for Browser {
fn default() -> Self {
Self::new(&[], &[])
}
}
impl Browser {
pub fn new(recent_orgs: &[String], defaults: &[String]) -> Self {
let mut names: Vec<String> = recent_orgs.to_vec();
for d in defaults {
if !names.iter().any(|n| n == d) {
names.push(d.to_string());
}
}
let orgs = names
.iter()
.map(|n| Entry::new(n, EntryKind::Org))
.collect();
// Starts folded at the orgs level; the repos level arrives
// asynchronously via `org_repos_loaded`.
let mut browser = Browser {
levels: vec![Pane::new("orgs", orgs)],
focus: 0,
tree: None,
blobs: HashMap::new(),
pending_blobs: HashSet::new(),
failed_blobs: HashMap::new(),
blob_request: None,
preview: Preview::new(),
filter_input: VimInput::transient(),
find_input: VimInput::transient(),
visual: false,
marks: std::collections::HashSet::new(),
current_ref: None,
history: None,
commit: None,
blame: None,
at_commit: None,
};
browser.sync();
browser
}
/// Selected org at the top level, if any.
pub fn selected_org(&self) -> Option<String> {
self.levels[0].selected_entry().map(|e| e.name.clone())
}
/// Default branch of the open repo, if a tree is loaded.
pub fn branch(&self) -> Option<&str> {
self.tree.as_ref().map(|t| t.branch.as_str())
}
/// Repo-level entries as full names (`org/repo`), if loaded.
pub fn org_repo_full_names(&self) -> Vec<String> {
let Some(org) = self.selected_org() else {
return vec![];
};
let Some(repos) = self.levels.get(1) else {
return vec![];
};
repos
.entries
.iter()
.filter(|e| e.kind == EntryKind::Repo)
.map(|e| format!("{org}/{}", e.name))
.collect()
}
/// Ensure `org` exists in the orgs level and select it.
pub fn select_org(&mut self, org: &str) {
let pos = self.levels[0]
.entries
.iter()
.position(|e| e.name == org)
.unwrap_or_else(|| {
self.levels[0]
.entries
.insert(0, Entry::new(org, EntryKind::Org));
0
});
self.levels[0].select(pos);
self.focus = 0;
self.sync();
}
/// Org repos arrived from the API: install/replace the repos level.
/// Ignored if the user has since selected a different org.
pub fn org_repos_loaded(&mut self, org: &str, repos: Vec<rootle_provider::RepoInfo>) {
if !self.org_repos_would_accept(org) {
return;
}
let entries = repos
.iter()
.map(|r| Entry::new(&r.name, EntryKind::Repo))
.collect();
self.levels.truncate(1);
self.levels.push(Pane::new(org, entries));
self.focus = 1;
self.cascade();
self.sync();
}
/// Repo tree arrived: install it and rebuild the dir columns.
/// Ignored if it doesn't match the currently selected repo.
pub fn tree_loaded(
&mut self,
owner: &str,
name: &str,
entries: Vec<TreeNode>,
truncated: bool,
branch: String,
) {
if !self.tree_would_accept(owner, name) {
return;
}
self.tree = Some(RepoTree::new(
owner.to_string(),
name.to_string(),
truncated,
branch,
entries,
));
self.levels.truncate(2);
self.focus = 1;
self.cascade();
// Complete the interrupted drill: the tree load was requested by
// acting on this repo, so land the user in its root pane rather
// than leaving them on the repos pane to press `l` again.
if self.levels.len() > 2 {
self.focus = 2;
}
self.sync();
}
/// Shared identity guard for org-repo application and its observation.
pub(crate) fn org_repos_would_accept(&self, org: &str) -> bool {
self.selected_org().as_deref() == Some(org)
}
/// Shared identity guard for tree application and its observation.
pub(crate) fn tree_would_accept(&self, owner: &str, name: &str) -> bool {
self.levels.get(1).map(|p| p.title.as_str()) == Some(owner)
&& self
.levels
.get(1)
.and_then(|p| p.selected_entry())
.map(|e| e.name.as_str())
== Some(name)
}
/// Shared condition for applying arrived blame ranges to the open lens.
pub(crate) fn blame_awaiting(&self, path: &str) -> bool {
self.blame
.as_ref()
.is_some_and(|b| b.loading && b.path == path)
}
/// Column/marks/blob-cache summary for session traces (plans/0030):
/// indices and counts only, no entry text.
pub(crate) fn diagnostics(&self) -> BrowserDiagnostics {
BrowserDiagnostics {
focus: self.focus,
columns: self.levels.iter().map(|p| p.diagnostics()).collect(),
visual: self.visual,
marks: self.marks.len(),
cached_blobs: self.blobs.len(),
pending_blobs: self.pending_blobs.len(),
failed_blobs: self.failed_blobs.len(),
history: self.history.as_ref().map(|h| h.diagnostics()),
blame: self.blame.as_ref().map(|b| BlameDiagnostics {
loading: b.loading,
ranges: b.ranges.len(),
}),
}
}
}
/// Diagnostic summary of the browser (plans/0030 session traces).
#[derive(Debug, Clone)]
pub(crate) struct BrowserDiagnostics {
pub(crate) focus: usize,
pub(crate) columns: Vec<super::pane::PaneDiagnostics>,
pub(crate) visual: bool,
pub(crate) marks: usize,
pub(crate) cached_blobs: usize,
pub(crate) pending_blobs: usize,
pub(crate) failed_blobs: usize,
pub(crate) history: Option<HistoryDiagnostics>,
pub(crate) blame: Option<BlameDiagnostics>,
}
/// Blame-lens summary: is a fetch in flight, how many ranges landed.
#[derive(Debug, Clone, Copy)]
pub(crate) struct BlameDiagnostics {
pub(crate) loading: bool,
pub(crate) ranges: usize,
}
pub use marks::MarkKey;
#[cfg(test)]
mod tests {
use super::*;
use crate::highlight::Highlighter;
use crate::theme::Theme;
#[test]
fn restyle_blobs_recolors_cached_lines_without_refetch() {
let mut b = Browser::new(&[], &[]);
let mocha = Highlighter::default();
let lines = mocha.highlight("lib.rs", "fn main() {}\n");
b.blob_loaded("sha1", "lib.rs", "rust", "fn main() {}\n".into(), lines);
// Sanity: cached under mocha (mauve keyword).
assert_eq!(
b.blobs["sha1"].lines[0].spans[0].style.fg,
Some(ratatui::style::Color::Rgb(203, 166, 247))
);
let dracula = Highlighter::new(&Theme::embedded("dracula").unwrap());
b.restyle_blobs(&dracula);
assert_eq!(
b.blobs["sha1"].lines[0].spans[0].style.fg,
Some(ratatui::style::Color::Rgb(255, 121, 198)),
"cached lines should follow the new palette from raw text"
);
}
}