rs-hop 0.4.1

Fuzzy-finder TUI to jump between git repositories and folders
Documentation
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! Sort modes for the repository list and the pure ordering they imply.
//!
//! Four modes order by what an entry *is* (name, recency, frecency, the stored
//! order); the rest order by a statistic shown in the active column set. The
//! statistics arrive asynchronously, so an entry whose number has not been
//! computed yet sorts **last in either direction** rather than counting as
//! zero - otherwise the list would reshuffle as the background workers report.

use std::collections::HashMap;
use std::path::PathBuf;

use crate::domain::repo::Repo;
use crate::domain::stats::{CodeEntry, GitStats};

/// How the visible list is ordered.
///
/// Favourites are pinned to the top in [`SortMode::Name`], [`SortMode::
/// Frecency`] and [`SortMode::Custom`]; [`SortMode::Recent`] orders purely by
/// recency. The statistics modes do not pin favourites: they answer "which is
/// the biggest", and a pinned favourite would hide the answer.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SortMode {
    /// Favourites first, then case-insensitive by display name (the default).
    #[default]
    Name,
    /// Most recently used first; never-used entries last, then by name.
    /// Favourites are not pinned.
    Recent,
    /// Frequency weighted by recency (frecency); favourites pinned on top.
    Frecency,
    /// Manual order (the stored `[[repos]]` order), favourites still on top.
    Custom,
    /// Lines of code.
    Loc,
    /// Bytes on disk.
    Size,
    /// Recognised source files.
    Files,
    /// Commits reachable from `HEAD`.
    Commits,
    /// Timestamp of the most recent commit.
    LastCommit,
    /// Distinct commit authors.
    Authors,
    /// Local branches.
    Branches,
    /// How often the entry was opened from hop.
    Opens,
    /// When the entry was last opened from hop.
    LastUsed,
}

/// Which way a sort runs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SortDir {
    /// Smallest, earliest or alphabetically first at the top.
    #[default]
    Asc,
    /// Largest, latest or alphabetically last at the top.
    Desc,
}

impl SortDir {
    /// The opposite direction, for re-picking the active column.
    pub fn flip(self) -> Self {
        match self {
            SortDir::Asc => SortDir::Desc,
            SortDir::Desc => SortDir::Asc,
        }
    }

    /// The stable persisted value.
    pub fn label(self) -> &'static str {
        match self {
            SortDir::Asc => "asc",
            SortDir::Desc => "desc",
        }
    }

    /// Parses a stored [`label`](Self::label) value, defaulting to `Asc`.
    pub fn from_config_value(value: &str) -> Self {
        match value.trim().to_lowercase().as_str() {
            "desc" => SortDir::Desc,
            _ => SortDir::Asc,
        }
    }

    /// The arrow marking this direction in the sort picker.
    pub fn arrow(self) -> &'static str {
        match self {
            SortDir::Asc => "\u{2191}",
            SortDir::Desc => "\u{2193}",
        }
    }
}

impl SortMode {
    /// A short label for the header line (also the persisted value).
    pub fn label(self) -> &'static str {
        match self {
            SortMode::Name => "name",
            SortMode::Recent => "recent",
            SortMode::Frecency => "frecency",
            SortMode::Custom => "custom",
            SortMode::Loc => "loc",
            SortMode::Size => "size",
            SortMode::Files => "files",
            SortMode::Commits => "commits",
            SortMode::LastCommit => "last_commit",
            SortMode::Authors => "authors",
            SortMode::Branches => "branches",
            SortMode::Opens => "opens",
            SortMode::LastUsed => "last_used",
        }
    }

    /// The human name shown in the sort picker.
    pub fn title(self) -> &'static str {
        match self {
            SortMode::Name => "Name",
            SortMode::Recent => "Recently used",
            SortMode::Frecency => "Frecency",
            SortMode::Custom => "Custom order",
            SortMode::Loc => "Lines of code",
            SortMode::Size => "Size",
            SortMode::Files => "Files",
            SortMode::Commits => "Commits",
            SortMode::LastCommit => "Last commit",
            SortMode::Authors => "Authors",
            SortMode::Branches => "Branches",
            SortMode::Opens => "Opens",
            SortMode::LastUsed => "Last used",
        }
    }

    /// Parses a stored [`label`](Self::label) value, defaulting to `Name`.
    pub fn from_config_value(value: &str) -> Self {
        match value.trim().to_lowercase().as_str() {
            "recent" => SortMode::Recent,
            "frecency" => SortMode::Frecency,
            "custom" => SortMode::Custom,
            "loc" => SortMode::Loc,
            "size" => SortMode::Size,
            "files" => SortMode::Files,
            "commits" => SortMode::Commits,
            "last_commit" => SortMode::LastCommit,
            "authors" => SortMode::Authors,
            "branches" => SortMode::Branches,
            "opens" => SortMode::Opens,
            "last_used" => SortMode::LastUsed,
            _ => SortMode::Name,
        }
    }

    /// Whether the mode orders by a background-computed statistic. Those modes
    /// do not pin favourites and default to descending when first picked.
    pub fn is_statistic(self) -> bool {
        !matches!(
            self,
            SortMode::Name
                | SortMode::Recent
                | SortMode::Frecency
                | SortMode::Custom
        )
    }
}

/// The computed statistics a sort may order by, borrowed from the app's caches.
#[derive(Debug, Clone, Copy)]
pub struct StatsLookup<'a> {
    /// Lines of code and size on disk, per entry path.
    pub code: &'a HashMap<PathBuf, CodeEntry>,
    /// Repository history, per entry path.
    pub git: &'a HashMap<PathBuf, GitStats>,
}

/// Everything a sort needs, grouped so the entry point keeps three parameters.
#[derive(Debug, Clone, Copy)]
pub struct SortContext<'a> {
    /// What to order by.
    pub mode: SortMode,
    /// Which way round.
    pub dir: SortDir,
    /// Whether favourites float to the top (and are then ordered among
    /// themselves by the mode); off orders every entry purely by the mode.
    pub float_favs: bool,
    /// The reference time for frecency, in unix seconds.
    pub now: i64,
    /// The statistics the column modes read.
    pub stats: StatsLookup<'a>,
}

/// Sorts `indices` (positions into `repos`) according to `ctx`, leaving the
/// entries themselves untouched. Used by the view, which keeps service indices.
pub fn sort_indices(repos: &[Repo], indices: &mut [usize], ctx: &SortContext) {
    indices.sort_by(|a, b| compare(&repos[*a], &repos[*b], ctx));
}

/// Compares two entries under `ctx`. Favourites float to the top in every mode
/// when `float_favs` is on (and are then ordered among themselves by the mode);
/// the float sits outside the direction reversal, so favourites stay on top in
/// both directions.
fn compare(a: &Repo, b: &Repo, ctx: &SortContext) -> std::cmp::Ordering {
    let base = if ctx.mode.is_statistic() {
        compare_statistic(a, b, ctx)
    } else {
        apply_dir(compare_identity(a, b, ctx), ctx.dir)
    };
    if ctx.float_favs {
        b.fav.cmp(&a.fav).then(base)
    } else {
        base
    }
}

/// Applies the sort direction to an ascending-oriented ordering.
fn apply_dir(ordering: std::cmp::Ordering, dir: SortDir) -> std::cmp::Ordering {
    match dir {
        SortDir::Asc => ordering,
        SortDir::Desc => ordering.reverse(),
    }
}

/// Compares by what an entry is: name, recency, frecency or the stored order.
/// Favourite floating is applied by [`compare`], not here.
fn compare_identity(
    a: &Repo,
    b: &Repo,
    ctx: &SortContext,
) -> std::cmp::Ordering {
    match ctx.mode {
        // Case-insensitive by name.
        SortMode::Name => name_cmp(a, b),
        // Recency, name as tiebreak.
        SortMode::Recent => recency_key(b)
            .cmp(&recency_key(a))
            .then_with(|| name_cmp(a, b)),
        // Highest frecency, name as tiebreak.
        SortMode::Frecency => frecency(b, ctx.now)
            .partial_cmp(&frecency(a, ctx.now))
            .unwrap_or(std::cmp::Ordering::Equal)
            .then_with(|| name_cmp(a, b)),
        // Stable on the stored index order.
        _ => std::cmp::Ordering::Equal,
    }
}

/// Compares by a computed statistic. An entry whose value is not (yet) known
/// sorts last in **both** directions, so a running background pass does not
/// make the list jump; the direction only orders the entries that do have one.
fn compare_statistic(
    a: &Repo,
    b: &Repo,
    ctx: &SortContext,
) -> std::cmp::Ordering {
    match (statistic(a, ctx), statistic(b, ctx)) {
        (None, None) => name_cmp(a, b),
        (None, Some(_)) => std::cmp::Ordering::Greater,
        (Some(_), None) => std::cmp::Ordering::Less,
        (Some(left), Some(right)) => {
            let ordering = match ctx.dir {
                SortDir::Asc => left.cmp(&right),
                SortDir::Desc => right.cmp(&left),
            };
            ordering.then_with(|| name_cmp(a, b))
        }
    }
}

/// The value `ctx.mode` orders by, or `None` when it is not known for `repo`.
fn statistic(repo: &Repo, ctx: &SortContext) -> Option<i64> {
    let code = || ctx.stats.code.get(&repo.path);
    let git = || ctx.stats.git.get(&repo.path);
    match ctx.mode {
        SortMode::Loc => code().map(|e| e.code.total().code as i64),
        SortMode::Files => code().map(|e| e.code.total().files as i64),
        SortMode::Size => code().map(|e| e.disk.total_bytes as i64),
        SortMode::Commits => git().map(|g| g.commits as i64),
        SortMode::Authors => git().map(|g| g.contributors as i64),
        SortMode::Branches => git().map(|g| g.branches as i64),
        SortMode::LastCommit => git().and_then(|g| g.last_commit),
        SortMode::Opens => Some(repo.open_count as i64),
        SortMode::LastUsed => repo.last_used,
        _ => None,
    }
}

/// Case-insensitive comparison of two repos by display name.
fn name_cmp(a: &Repo, b: &Repo) -> std::cmp::Ordering {
    a.display_name()
        .to_lowercase()
        .cmp(&b.display_name().to_lowercase())
}

/// The recency key: the last-used timestamp, or `i64::MIN` when never used so
/// it sorts last under the "most recent first" comparison.
fn recency_key(repo: &Repo) -> i64 {
    repo.last_used.unwrap_or(i64::MIN)
}

/// A Firefox-style frecency score: the open count weighted by how recently the
/// entry was used. Never-used entries score 0.
fn frecency(repo: &Repo, now: i64) -> f64 {
    let Some(last_used) = repo.last_used else {
        return 0.0;
    };
    let age = now.saturating_sub(last_used);
    let weight = if age < 86_400 {
        4.0
    } else if age < 7 * 86_400 {
        2.0
    } else if age < 30 * 86_400 {
        1.0
    } else {
        0.5
    };
    repo.open_count as f64 * weight
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use super::*;
    use crate::domain::stats::{CodeStats, DiskStats, LangCount};

    const NOW: i64 = 1_000_000;

    fn repo(name: &str, fav: bool, last_used: Option<i64>) -> Repo {
        let mut repo = Repo::new(PathBuf::from(format!("/r/{name}")));
        repo.name = Some(name.to_string());
        repo.fav = fav;
        repo.last_used = last_used;
        repo
    }

    /// An empty lookup, for the modes that do not read statistics.
    fn no_stats() -> (HashMap<PathBuf, CodeEntry>, HashMap<PathBuf, GitStats>) {
        (HashMap::new(), HashMap::new())
    }

    fn context<'a>(
        mode: SortMode,
        dir: SortDir,
        maps: &'a (HashMap<PathBuf, CodeEntry>, HashMap<PathBuf, GitStats>),
    ) -> SortContext<'a> {
        floated(mode, dir, maps, true)
    }

    fn floated<'a>(
        mode: SortMode,
        dir: SortDir,
        maps: &'a (HashMap<PathBuf, CodeEntry>, HashMap<PathBuf, GitStats>),
        float_favs: bool,
    ) -> SortContext<'a> {
        SortContext {
            mode,
            dir,
            float_favs,
            now: NOW,
            stats: StatsLookup {
                code: &maps.0,
                git: &maps.1,
            },
        }
    }

    /// The display names of `repos` after sorting under `ctx`.
    fn ordered(repos: &[Repo], ctx: &SortContext) -> Vec<String> {
        let mut indices: Vec<usize> = (0..repos.len()).collect();
        sort_indices(repos, &mut indices, ctx);
        indices.iter().map(|&i| repos[i].display_name()).collect()
    }

    /// A code entry with `code` lines in one file of `bytes` bytes.
    fn code_entry(code: usize, bytes: u64) -> CodeEntry {
        let languages = std::collections::BTreeMap::from([(
            "Rust".to_string(),
            LangCount {
                code,
                comments: 0,
                blanks: 0,
                files: 1,
            },
        )]);
        CodeEntry {
            code: CodeStats { languages },
            disk: DiskStats {
                total_bytes: bytes,
                ..DiskStats::default()
            },
        }
    }

    #[test]
    fn name_puts_favourites_first_then_alphabetical() {
        let repos = vec![
            repo("zebra", false, None),
            repo("alpha", false, None),
            repo("beta", true, None),
        ];
        let maps = no_stats();
        let names =
            ordered(&repos, &context(SortMode::Name, SortDir::Asc, &maps));
        assert_eq!(names, vec!["beta", "alpha", "zebra"]);
    }

    #[test]
    fn name_sorts_case_insensitively_within_a_group() {
        let repos =
            vec![repo("Banana", false, None), repo("apple", false, None)];
        let maps = no_stats();
        let names =
            ordered(&repos, &context(SortMode::Name, SortDir::Asc, &maps));
        assert_eq!(names, vec!["apple", "Banana"]);
    }

    #[test]
    fn descending_reverses_an_identity_sort() {
        let repos =
            vec![repo("alpha", false, None), repo("zebra", false, None)];
        let maps = no_stats();
        let names =
            ordered(&repos, &context(SortMode::Name, SortDir::Desc, &maps));
        assert_eq!(names, vec!["zebra", "alpha"]);
    }

    #[test]
    fn recent_without_float_ignores_favourites() {
        let repos = vec![
            repo("old-fav", true, Some(100)),
            repo("fresh", false, Some(900)),
            repo("never", false, None),
        ];
        let maps = no_stats();
        let ctx = floated(SortMode::Recent, SortDir::Asc, &maps, false);
        assert_eq!(ordered(&repos, &ctx), vec!["fresh", "old-fav", "never"]);
    }

    #[test]
    fn float_favs_pins_favourites_in_every_mode() {
        let repos = vec![
            repo("old-fav", true, Some(100)),
            repo("fresh", false, Some(900)),
            repo("never", false, None),
        ];
        let maps = no_stats();
        // Recency would rank "fresh" first, but the favourite floats on top
        // and non-favourites keep their recency order below it.
        let ctx = floated(SortMode::Recent, SortDir::Asc, &maps, true);
        assert_eq!(ordered(&repos, &ctx), vec!["old-fav", "fresh", "never"]);
    }

    #[test]
    fn float_favs_off_gives_pure_order() {
        let repos = vec![repo("zebra", true, None), repo("alpha", false, None)];
        let maps = no_stats();
        let ctx = floated(SortMode::Name, SortDir::Asc, &maps, false);
        // The favourite is not pinned; pure alphabetical order.
        assert_eq!(ordered(&repos, &ctx), vec!["alpha", "zebra"]);
    }

    #[test]
    fn float_favs_pins_in_a_statistics_mode() {
        let repos =
            vec![repo("small-fav", true, None), repo("big", false, None)];
        let code = HashMap::from([
            (repos[0].path.clone(), code_entry(10, 100)),
            (repos[1].path.clone(), code_entry(900, 5)),
        ]);
        let maps = (code, HashMap::new());
        // Largest first would rank "big", but the favourite floats on top.
        let ctx = floated(SortMode::Loc, SortDir::Desc, &maps, true);
        assert_eq!(ordered(&repos, &ctx), vec!["small-fav", "big"]);
    }

    #[test]
    fn frecency_weights_count_by_recency() {
        let mut recent_few = repo("recent-few", false, Some(NOW - 3600));
        recent_few.open_count = 2; // weight 4 -> 8
        let mut old_many = repo("old-many", false, Some(NOW - 60 * 86_400));
        old_many.open_count = 10; // weight 0.5 -> 5
        let never = repo("never", false, None); // 0
        let repos = vec![old_many, never, recent_few];
        let maps = no_stats();
        let names =
            ordered(&repos, &context(SortMode::Frecency, SortDir::Asc, &maps));
        assert_eq!(names, vec!["recent-few", "old-many", "never"]);
    }

    #[test]
    fn custom_keeps_input_order_with_favourites_on_top() {
        let repos = vec![
            repo("zebra", false, None),
            repo("alpha", true, None),
            repo("mid", false, None),
            repo("beta", true, None),
        ];
        let maps = no_stats();
        let names =
            ordered(&repos, &context(SortMode::Custom, SortDir::Asc, &maps));
        assert_eq!(names, vec!["alpha", "beta", "zebra", "mid"]);
    }

    #[test]
    fn loc_orders_by_lines_without_float() {
        let repos = vec![
            repo("small-fav", true, None),
            repo("big", false, None),
            repo("mid", false, None),
        ];
        let code = HashMap::from([
            (repos[0].path.clone(), code_entry(10, 100)),
            (repos[1].path.clone(), code_entry(900, 5)),
            (repos[2].path.clone(), code_entry(100, 50)),
        ]);
        let maps = (code, HashMap::new());
        let desc = floated(SortMode::Loc, SortDir::Desc, &maps, false);
        assert_eq!(ordered(&repos, &desc), vec!["big", "mid", "small-fav"]);
        let asc = floated(SortMode::Loc, SortDir::Asc, &maps, false);
        assert_eq!(ordered(&repos, &asc), vec!["small-fav", "mid", "big"]);
        // Size orders independently of the line count.
        let size = floated(SortMode::Size, SortDir::Desc, &maps, false);
        assert_eq!(ordered(&repos, &size), vec!["small-fav", "mid", "big"]);
    }

    #[test]
    fn entries_without_a_computed_value_sort_last_in_both_directions() {
        // The background worker has only reported "known" so far.
        let repos =
            vec![repo("pending", false, None), repo("known", false, None)];
        let code = HashMap::from([(repos[1].path.clone(), code_entry(5, 5))]);
        let maps = (code, HashMap::new());
        for dir in [SortDir::Asc, SortDir::Desc] {
            assert_eq!(
                ordered(&repos, &context(SortMode::Loc, dir, &maps)),
                vec!["known", "pending"],
                "an uncomputed entry must never jump to the top"
            );
        }
    }

    #[test]
    fn commits_read_the_git_half_of_the_lookup() {
        let repos = vec![repo("few", false, None), repo("many", false, None)];
        let git = HashMap::from([
            (
                repos[0].path.clone(),
                GitStats {
                    commits: 3,
                    ..GitStats::default()
                },
            ),
            (
                repos[1].path.clone(),
                GitStats {
                    commits: 300,
                    ..GitStats::default()
                },
            ),
        ]);
        let maps = (HashMap::new(), git);
        assert_eq!(
            ordered(&repos, &context(SortMode::Commits, SortDir::Desc, &maps)),
            vec!["many", "few"]
        );
    }

    #[test]
    fn labels_round_trip_and_unknown_falls_back() {
        for mode in [
            SortMode::Name,
            SortMode::Recent,
            SortMode::Frecency,
            SortMode::Custom,
            SortMode::Loc,
            SortMode::Size,
            SortMode::Files,
            SortMode::Commits,
            SortMode::LastCommit,
            SortMode::Authors,
            SortMode::Branches,
            SortMode::Opens,
            SortMode::LastUsed,
        ] {
            assert_eq!(SortMode::from_config_value(mode.label()), mode);
        }
        assert_eq!(SortMode::from_config_value("bogus"), SortMode::Name);
        assert_eq!(SortDir::from_config_value("desc"), SortDir::Desc);
        assert_eq!(SortDir::from_config_value("bogus"), SortDir::Asc);
        assert_eq!(SortDir::Asc.flip(), SortDir::Desc);
    }

    #[test]
    fn only_the_column_modes_are_statistics() {
        assert!(!SortMode::Name.is_statistic());
        assert!(!SortMode::Custom.is_statistic());
        assert!(SortMode::Loc.is_statistic());
        assert!(SortMode::LastUsed.is_statistic());
    }
}