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
308
309
//! Moving through a branch's history (#371).
//!
//! TIMELINE lists a file's history and the COMMITS graph lists the repo's;
//! neither lets you *move* through time. This is the cursor that does: it
//! sits at a commit, steps between them, and knows how to get back.
//!
//! # The invariant
//!
//! **Leaving the scrubber must restore the live buffer exactly, including
//! unsaved changes.** Scrubbing is a way of looking, not of editing, and a
//! feature that loses a user's uncommitted work to answer a question about
//! history would be worse than not having the feature. So the working tree
//! is a POSITION in the cursor rather than a thing the scrubber replaces:
//! there is no state in which the live buffer has been discarded and the
//! scrubber is responsible for putting it back.
//!
//! That is why [`Position::Working`] is a variant rather than `Option::None`
//! over a commit index. An `Option` invites "no commit selected" and "back at
//! the working tree" to be the same value, and they are not — the second is
//! where the user started and must be reachable from anywhere.
/// Where the scrubber is looking.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Position {
/// The live buffer, with whatever unsaved edits it holds. The scrubber
/// starts here and `Home` returns here.
Working,
/// A commit, by index into the loaded list — 0 is the newest.
At(usize),
}
/// The scrubber's cursor over a branch's commits.
#[derive(Clone, Debug)]
pub struct Scrubber {
/// The branch's first-parent history, newest first, so index 0 is HEAD.
///
/// Whole `GraphCommit`s rather than hashes: the slider needs `short_hash`
/// for its labels and `parents` for the per-commit gutter delta, and
/// re-fetching them once the widget exists would mean two sources for
/// one list.
commits: Vec<crate::git::GraphCommit>,
position: Position,
}
impl Scrubber {
/// A scrubber over `commits` (newest first), parked at the working tree.
pub fn new(commits: Vec<crate::git::GraphCommit>) -> Self {
Self {
commits,
position: Position::Working,
}
}
#[cfg_attr(not(test), allow(dead_code))]
pub fn position(&self) -> Position {
self.position
}
#[cfg_attr(not(test), allow(dead_code))]
pub fn is_empty(&self) -> bool {
self.commits.is_empty()
}
/// The commit the cursor is on, or `None` at the working tree.
///
/// The whole record, so a caller can label the slider with git's own
/// `short_hash` rather than byte-slicing the full one — that slice is
/// safe only while the string is ASCII hex, and the width git shows is
/// per-repo rather than always seven.
pub fn commit(&self) -> Option<&crate::git::GraphCommit> {
match self.position {
Position::Working => None,
Position::At(i) => self.commits.get(i),
}
}
/// Step one commit toward the PAST.
///
/// From the working tree that is HEAD (index 0), not index 1: the
/// working tree and HEAD are different views — one has your unsaved
/// edits — so stepping back from the tree must show HEAD rather than
/// skipping it.
pub fn older(&mut self) {
if self.commits.is_empty() {
return;
}
self.position = match self.position {
Position::Working => Position::At(0),
Position::At(i) if i + 1 < self.commits.len() => Position::At(i + 1),
// Already at the oldest loaded commit: stay rather than wrap.
// Wrapping to the present would look like the drag "slipped".
other => other,
};
}
/// Step one commit toward the PRESENT, arriving at the working tree from
/// HEAD.
pub fn newer(&mut self) {
self.position = match self.position {
Position::Working => Position::Working,
Position::At(0) => Position::Working,
Position::At(i) => Position::At(i - 1),
};
}
/// Jump to the working tree — `Home`, and the only way back that a
/// caller ever needs.
pub fn home(&mut self) {
self.position = Position::Working;
}
/// Jump to a commit by index, clamped to what is loaded.
///
/// Not reached from the keyboard — this is what a DRAG on the slider
/// calls, and the slider widget is the next layer of #371. Kept here
/// with its tests because the clamping rule is the part with a right
/// answer, and deciding it alongside the painting would bury it.
///
/// Clamped rather than refused because this is what a DRAG calls: a
/// pointer beyond the last commit means "as far as it goes", and
/// refusing would make the slider stick short of its own end.
#[cfg_attr(not(test), allow(dead_code))]
pub fn seek(&mut self, index: usize) {
if self.commits.is_empty() {
self.position = Position::Working;
return;
}
self.position = Position::At(index.min(self.commits.len() - 1));
}
/// Where the slider's handle sits, as a fraction from 0.0 (oldest loaded)
/// to 1.0 (the working tree).
///
/// Same as [`Self::seek`]: the widget that reads this does not exist
/// yet.
///
/// The working tree is its own stop at the far right rather than sharing
/// HEAD's position, because they are different views and a slider that
/// showed them at the same place would give the user no way to tell
/// which one they are looking at.
#[cfg_attr(not(test), allow(dead_code))]
pub fn fraction(&self) -> f32 {
let stops = self.commits.len();
if stops == 0 {
return 1.0;
}
match self.position {
Position::Working => 1.0,
Position::At(i) => {
// `stops` intervals between `stops + 1` positions.
(stops - i - 1) as f32 / stops as f32
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn commit(i: usize) -> crate::git::GraphCommit {
crate::git::GraphCommit {
hash: format!("c{i}"),
short_hash: format!("c{i}"),
parents: Vec::new(),
refs: Vec::new(),
summary: format!("commit {i}"),
author: String::from("t"),
age_secs: 0,
}
}
fn scrubber(n: usize) -> Scrubber {
Scrubber::new((0..n).map(commit).collect())
}
/// The working tree is always reachable, from every position and by
/// stepping as well as by `Home`.
///
/// This is the module's invariant in its testable form: the live buffer
/// with its unsaved edits is a POSITION, so there is no state the
/// scrubber can reach from which the user cannot get back to what they
/// were editing.
#[test]
fn the_working_tree_is_reachable_from_everywhere() {
let mut s = scrubber(5);
assert_eq!(s.position(), Position::Working, "starts at the live buffer");
// From the oldest commit, by Home.
s.seek(4);
assert_eq!(s.position(), Position::At(4));
s.home();
assert_eq!(s.position(), Position::Working);
// And by stepping forward, which must ARRIVE rather than stop at
// HEAD — a scrubber that stranded the user one step short of their
// own edits would be the invariant failing quietly.
s.seek(3);
for _ in 0..4 {
s.newer();
}
assert_eq!(
s.position(),
Position::Working,
"stepping forward from any commit reaches the live buffer"
);
// Already there: stepping forward again is a no-op, not a wrap.
s.newer();
assert_eq!(s.position(), Position::Working);
}
/// Stepping back from the working tree lands on HEAD, not past it.
///
/// The working tree and HEAD are different views — one carries unsaved
/// edits — so skipping HEAD would hide the commit the user most wants to
/// compare against.
#[test]
fn stepping_back_from_the_tree_shows_head_first() {
let mut s = scrubber(3);
s.older();
assert_eq!(
s.position(),
Position::At(0),
"HEAD, not the commit below it"
);
assert_eq!(s.commit().map(|c| c.hash.as_str()), Some("c0"));
s.older();
assert_eq!(s.position(), Position::At(1));
}
/// The far end stops rather than wrapping.
///
/// A drag that ran off the oldest commit and reappeared at the present
/// would read as the slider slipping, and the user would not know which
/// end they were at.
#[test]
fn the_oldest_commit_is_a_wall_not_a_wrap() {
let mut s = scrubber(3);
for _ in 0..10 {
s.older();
}
assert_eq!(s.position(), Position::At(2), "stopped at the oldest");
assert_eq!(s.commit().map(|c| c.hash.as_str()), Some("c2"));
}
/// A drag beyond the end clamps, because a pointer past the last commit
/// means "as far as it goes".
#[test]
fn a_seek_past_the_end_clamps_to_the_oldest() {
let mut s = scrubber(3);
s.seek(99);
assert_eq!(s.position(), Position::At(2));
assert_eq!(s.commit().map(|c| c.hash.as_str()), Some("c2"));
}
/// An empty history has nowhere to go, and says so by staying put.
///
/// A repo with no commits is ordinary — a fresh `git init` — so this is
/// the empty state, not an error case.
#[test]
fn an_empty_history_stays_at_the_working_tree() {
let mut s = scrubber(0);
assert!(s.is_empty());
s.older();
assert_eq!(s.position(), Position::Working, "nothing to step back to");
s.seek(3);
assert_eq!(s.position(), Position::Working);
assert_eq!(s.commit().map(|c| c.hash.as_str()), None);
assert_eq!(s.fraction(), 1.0);
}
/// The handle's position is monotonic across every stop, and the working
/// tree has its own place at the far right.
///
/// Asserted as an ordering over the whole range rather than at a few
/// points: a formula that is right at the ends and wrong in the middle
/// would pass spot checks, and the middle is where a drag spends its
/// time.
#[test]
fn the_handle_moves_monotonically_from_oldest_to_the_tree() {
let mut s = scrubber(4);
let mut seen = Vec::new();
for i in (0..4).rev() {
s.seek(i);
seen.push(s.fraction());
}
s.home();
seen.push(s.fraction());
assert_eq!(
seen.first().copied(),
Some(0.0),
"oldest sits at the left edge"
);
assert_eq!(
seen.last().copied(),
Some(1.0),
"the tree sits at the right"
);
for pair in seen.windows(2) {
assert!(pair[1] > pair[0], "the handle went backwards: {seen:?}");
}
// HEAD and the working tree are DIFFERENT stops, so the user can see
// which of the two they are looking at.
s.seek(0);
assert!(s.fraction() < 1.0, "HEAD must not sit on top of the tree");
}
}