rs-hop 0.2.0

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
//! The add/edit form for an entry: path, name, section, slug, kind, favourite.
//!
//! Which fields show depends on the current kind: a git entry shows the
//! Favourite toggle, a folder/file shows the Section dropdown; changing the
//! Kind field updates this live. `Tab`/`BackTab` (or `Up`/`Down`) step between
//! the visible fields, the text fields edit inline, `Left`/`Right` cycle the
//! section and the kind, `Space` toggles the favourite, `^O` opens the path
//! picker, `Enter`/`Ctrl+S` save and `Esc` cancels. When the name is still
//! blank, it is auto-filled from the path's basename (after picking a path, or
//! when the name field is focused).

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratada::input::InputField;
use ratatui::Frame;
use ratatui::layout::Rect;
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Clear, Paragraph};

use crate::domain::repo::{Repo, RepoKind};
use crate::domain::sections::UNGROUPED;
use crate::domain::slug::slugify;
use crate::theme::Skin;
use crate::tui::presentation::{FieldView, field_spans};
use crate::tui::skin::Colors;
use crate::tui::widgets::centered_rect;

/// Display columns reserved for a field's label, before its value.
const LABEL_WIDTH: usize = 8;

/// What a field line needs to draw itself, bundled to keep the parameter count
/// low.
struct LineCtx<'a> {
    /// The colour roles resolved from the active theme.
    colors: &'a Colors,
    /// The active theme, whose palette colours the caret of a text field.
    skin: &'a Skin,
    /// The display columns a field's value may occupy, after its label.
    value_width: usize,
}

/// One editable field of the form.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Field {
    Path,
    Name,
    Section,
    Slug,
    Kind,
    Fav,
    Backup,
}

/// The values captured by the form on save.
pub struct RepoDraft {
    /// Explicit name, or `None` to fall back to the path's basename.
    pub name: Option<String>,
    /// The entry path as typed.
    pub path: String,
    /// Slug, or `None` when blank.
    pub slug: Option<String>,
    /// Section name, or `None` for Ungrouped.
    pub section: Option<String>,
    /// The entry kind.
    pub kind: RepoKind,
    /// Whether the entry is a favourite.
    pub fav: bool,
    /// Whether the "backup all" (`Z`) run includes this entry.
    pub include_in_backup: bool,
}

/// Outcome of feeding a key to the form.
pub enum FormResult {
    /// Still editing.
    Pending,
    /// The user saved these values.
    Save(RepoDraft),
    /// The user asked to pick the path with the filesystem picker.
    PickPath,
    /// The user cancelled.
    Cancel,
}

/// Add/edit form state.
pub struct RepoForm {
    title: String,
    name: InputField,
    path: InputField,
    slug: InputField,
    kind: RepoKind,
    fav: bool,
    include_in_backup: bool,
    /// The section options offered by the dropdown: `None` (Ungrouped) first,
    /// then each known section.
    section_options: Vec<Option<String>>,
    /// The selected index into `section_options`.
    section_choice: usize,
    /// The original section, kept for entries whose Section field is hidden.
    seed_section: Option<String>,
    /// The focused field's position in the currently visible fields.
    focus: usize,
}

impl RepoForm {
    /// A blank add form, seeded with a `path` and guessed `kind`. The backup
    /// toggle defaults per kind: on for git repos, off for file/folder entries.
    pub fn for_add(path: &str, kind: RepoKind, sections: &[String]) -> Self {
        Self::build(
            "Add entry",
            "",
            path,
            "",
            kind,
            false,
            kind == RepoKind::Git,
            None,
            sections,
        )
    }

    /// An edit form seeded from an existing entry's fields.
    pub fn for_edit(repo: &Repo, sections: &[String]) -> Self {
        Self::build(
            "Edit entry",
            repo.name.as_deref().unwrap_or(""),
            &repo.path.to_string_lossy(),
            repo.slug.as_deref().unwrap_or(""),
            repo.kind,
            repo.fav,
            repo.include_in_backup,
            repo.section.clone(),
            sections,
        )
    }

    /// Builds a form from explicit seed values and the known section names.
    #[allow(clippy::too_many_arguments)]
    fn build(
        title: &str,
        name: &str,
        path: &str,
        slug: &str,
        kind: RepoKind,
        fav: bool,
        include_in_backup: bool,
        section: Option<String>,
        sections: &[String],
    ) -> Self {
        let section_options = section_options(sections);
        let section_choice =
            section_choice(&section_options, section.as_deref());
        RepoForm {
            title: title.to_string(),
            name: InputField::new(name),
            path: InputField::new(path),
            slug: InputField::new(slug),
            kind,
            fav,
            include_in_backup,
            section_options,
            section_choice,
            seed_section: section,
            focus: 0,
        }
    }

    /// The fields visible for the current kind, in focus order: a git entry
    /// shows Fav, a folder/file shows Section.
    fn fields(&self) -> Vec<Field> {
        let mut fields = vec![Field::Path, Field::Name];
        match self.kind {
            RepoKind::Git => {
                fields.push(Field::Slug);
                fields.push(Field::Fav);
            }
            RepoKind::Path => {
                fields.push(Field::Section);
                fields.push(Field::Slug);
            }
        }
        // The backup toggle shows for both kinds; Kind sits at the very bottom.
        fields.push(Field::Backup);
        fields.push(Field::Kind);
        fields
    }

    /// Handles a key, returning a save draft, a cancel, or pending.
    pub fn handle_key(&mut self, key: KeyEvent) -> FormResult {
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
        match key.code {
            KeyCode::Esc => return FormResult::Cancel,
            KeyCode::Enter => return FormResult::Save(self.draft()),
            KeyCode::Char('s') if ctrl => {
                return FormResult::Save(self.draft());
            }
            KeyCode::Char('o') if ctrl => return FormResult::PickPath,
            KeyCode::Tab | KeyCode::Down => self.move_focus(1),
            KeyCode::BackTab | KeyCode::Up => {
                self.move_focus(self.fields().len() - 1)
            }
            _ => self.edit_focused(key),
        }
        FormResult::Pending
    }

    /// Moves focus by `delta` (wrapping), auto-filling the name on arrival.
    fn move_focus(&mut self, delta: usize) {
        let fields = self.fields();
        self.focus = (self.focus + delta) % fields.len();
        if fields[self.focus] == Field::Name {
            self.autofill_name();
        }
    }

    /// Routes an editing key to the focused field.
    fn edit_focused(&mut self, key: KeyEvent) {
        match self.focused_field() {
            Field::Path => {
                self.path.handle_key(key);
            }
            Field::Name => {
                self.name.handle_key(key);
            }
            Field::Slug => {
                self.slug.handle_key(key);
            }
            Field::Section => match key.code {
                KeyCode::Left => self.cycle_section(-1),
                KeyCode::Right => self.cycle_section(1),
                _ => {}
            },
            Field::Kind => self.edit_kind(key),
            Field::Fav => {
                if key.code == KeyCode::Char(' ') {
                    self.fav = !self.fav;
                }
            }
            Field::Backup => {
                if key.code == KeyCode::Char(' ') {
                    self.include_in_backup = !self.include_in_backup;
                }
            }
        }
    }

    /// Cycles the kind on `Left`/`Right`, keeping focus on the Kind field even
    /// though the visible field set changes with the kind.
    fn edit_kind(&mut self, key: KeyEvent) {
        let changed = match key.code {
            KeyCode::Left => {
                self.kind = prev_kind(self.kind);
                true
            }
            KeyCode::Right => {
                self.kind = next_kind(self.kind);
                true
            }
            _ => false,
        };
        if changed {
            self.focus = self.field_index(Field::Kind);
        }
    }

    /// The field currently focused.
    fn focused_field(&self) -> Field {
        self.fields()[self.focus]
    }

    /// The position of `field` in the current layout, or 0 when absent.
    fn field_index(&self, field: Field) -> usize {
        self.fields()
            .iter()
            .position(|candidate| *candidate == field)
            .unwrap_or(0)
    }

    /// Cycles the section choice by `delta` (wrapping).
    fn cycle_section(&mut self, delta: isize) {
        let len = self.section_options.len();
        if len == 0 {
            return;
        }
        let next =
            (self.section_choice as isize + delta).rem_euclid(len as isize);
        self.section_choice = next as usize;
    }

    /// The path field's current value (used to seed the path picker).
    pub fn path_value(&self) -> String {
        self.path.value().to_string()
    }

    /// Replaces the path field (after picking a path) and auto-fills the name
    /// from its basename when the name is still blank.
    pub fn set_path(&mut self, path: &str) {
        self.path = InputField::new(path);
        self.autofill_name();
        self.focus = 0;
    }

    /// Fills the name from the path's basename when the name is still blank.
    fn autofill_name(&mut self) {
        if !self.name.value().trim().is_empty() {
            return;
        }
        if let Some(base) = basename(self.path.value()) {
            self.name = InputField::new(&base);
        }
    }

    /// Builds the draft from the current field values.
    fn draft(&self) -> RepoDraft {
        let name = non_empty(self.name.value().to_string());
        let slug = non_empty(slugify(self.slug.value()));
        let section = if self.fields().contains(&Field::Section) {
            self.section_options
                .get(self.section_choice)
                .cloned()
                .flatten()
        } else {
            self.seed_section.clone()
        };
        RepoDraft {
            name,
            path: self.path.value().to_string(),
            slug,
            section,
            kind: self.kind,
            fav: self.fav,
            include_in_backup: self.include_in_backup,
        }
    }

    /// Renders the form centred in `area`.
    pub fn render(&self, frame: &mut Frame, area: Rect, skin: &Skin) {
        let colors = Colors::from_palette(&skin.palette);
        let fields = self.fields();
        let height = fields.len() as u16 + 4;
        let rect = centered_rect(70, height, area);
        frame.render_widget(Clear, rect);
        let block = ratada::chrome::modal_block(skin, &self.title);
        let inner_width = block.inner(rect).width as usize;
        let ctx = LineCtx {
            colors: &colors,
            skin,
            value_width: inner_width.saturating_sub(LABEL_WIDTH),
        };
        let mut lines: Vec<Line> = fields
            .iter()
            .enumerate()
            .map(|(index, field)| self.field_line(*field, index, &ctx))
            .collect();
        lines.push(Line::raw(""));
        lines.push(Line::from(Span::styled(
            "Tab field · \u{2190}\u{2192} change · Space fav · ^O pick path · \
             Enter save · Esc cancel",
            Style::default().fg(colors.dim),
        )));
        frame.render_widget(Paragraph::new(lines).block(block), rect);
    }

    /// Builds the rendered line for `field` at focus position `index`.
    fn field_line(
        &self,
        field: Field,
        index: usize,
        ctx: &LineCtx,
    ) -> Line<'static> {
        let colors = ctx.colors;
        match field {
            Field::Path => self.text_line("Path", &self.path, index, ctx),
            Field::Name => self.text_line("Name", &self.name, index, ctx),
            Field::Slug => self.text_line("Slug", &self.slug, index, ctx),
            Field::Section => {
                let label = self.section_label();
                self.choice_line("Section", label, index, colors)
            }
            Field::Kind => {
                self.choice_line("Kind", kind_label(self.kind), index, colors)
            }
            Field::Fav => self.fav_line(index, colors),
            Field::Backup => self.backup_line(index, colors),
        }
    }

    /// A labelled text field line, highlighting it when focused. The value is
    /// drawn by the toolkit, so a long path scrolls under the caret instead of
    /// spilling out of the box.
    fn text_line(
        &self,
        label: &str,
        input: &InputField,
        index: usize,
        ctx: &LineCtx,
    ) -> Line<'static> {
        let mut spans = vec![self.label_span(label, index, ctx.colors)];
        spans.extend(field_spans(FieldView {
            field: input,
            palette: &ctx.skin.palette,
            width: ctx.value_width,
            focused: index == self.focus,
        }));
        self.styled(spans, index, ctx.colors)
    }

    /// A `< value >` selector line (section, kind).
    fn choice_line(
        &self,
        label: &str,
        value: &str,
        index: usize,
        colors: &Colors,
    ) -> Line<'static> {
        let spans = vec![
            self.label_span(label, index, colors),
            Span::styled(
                format!("< {value} >"),
                Style::default().fg(colors.accent),
            ),
        ];
        self.styled(spans, index, colors)
    }

    /// The favourite toggle line.
    fn fav_line(&self, index: usize, colors: &Colors) -> Line<'static> {
        let mark = if self.fav { "[x]" } else { "[ ]" };
        let spans = vec![
            self.label_span("Fav", index, colors),
            Span::raw(mark.to_string()),
        ];
        self.styled(spans, index, colors)
    }

    /// The "include in backup all (`Z`)" toggle line.
    fn backup_line(&self, index: usize, colors: &Colors) -> Line<'static> {
        let mark = if self.include_in_backup { "[x]" } else { "[ ]" };
        let spans = vec![
            self.label_span("Backup", index, colors),
            Span::raw(mark.to_string()),
        ];
        self.styled(spans, index, colors)
    }

    /// The label of the currently selected section option.
    fn section_label(&self) -> &str {
        match self.section_options.get(self.section_choice) {
            Some(Some(name)) => name,
            _ => UNGROUPED,
        }
    }

    /// The fixed-width field label, accented when focused.
    fn label_span(
        &self,
        label: &str,
        index: usize,
        colors: &Colors,
    ) -> Span<'static> {
        let style = if index == self.focus {
            Style::default()
                .fg(colors.accent)
                .add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(colors.dim)
        };
        Span::styled(format!("{label:<LABEL_WIDTH$}"), style)
    }

    /// Applies the focus background tint to a field line.
    fn styled(
        &self,
        spans: Vec<Span<'static>>,
        index: usize,
        colors: &Colors,
    ) -> Line<'static> {
        let line = Line::from(spans);
        if index == self.focus {
            line.style(Style::default().bg(colors.selection_bg))
        } else {
            line
        }
    }
}

/// The Section dropdown options: Ungrouped (`None`) first, then each section.
fn section_options(sections: &[String]) -> Vec<Option<String>> {
    let mut options = vec![None];
    options.extend(sections.iter().cloned().map(Some));
    options
}

/// The option index matching `section` (case-insensitive), or 0 (Ungrouped).
fn section_choice(options: &[Option<String>], section: Option<&str>) -> usize {
    let Some(name) = section else {
        return 0;
    };
    options
        .iter()
        .position(|option| {
            option
                .as_deref()
                .is_some_and(|o| o.eq_ignore_ascii_case(name))
        })
        .unwrap_or(0)
}

/// The final path component of `path`, or `None` when it has none.
fn basename(path: &str) -> Option<String> {
    let trimmed = path.trim();
    if trimmed.is_empty() {
        return None;
    }
    std::path::Path::new(trimmed)
        .file_name()
        .map(|name| name.to_string_lossy().into_owned())
}

/// Returns `Some(value)` when non-empty after trimming, else `None`.
fn non_empty(value: String) -> Option<String> {
    if value.trim().is_empty() {
        None
    } else {
        Some(value)
    }
}

/// The next kind in the cycle (git <-> file/folder).
fn next_kind(kind: RepoKind) -> RepoKind {
    toggle_kind(kind)
}

/// The previous kind in the cycle (git <-> file/folder).
fn prev_kind(kind: RepoKind) -> RepoKind {
    toggle_kind(kind)
}

/// Toggles between the two kinds.
fn toggle_kind(kind: RepoKind) -> RepoKind {
    match kind {
        RepoKind::Git => RepoKind::Path,
        RepoKind::Path => RepoKind::Git,
    }
}

/// The display label for a kind.
fn kind_label(kind: RepoKind) -> &'static str {
    match kind {
        RepoKind::Git => "git",
        RepoKind::Path => "file/folder",
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn basename_takes_final_component() {
        assert_eq!(basename("/code/hop").as_deref(), Some("hop"));
        assert_eq!(basename("/code/hop/").as_deref(), Some("hop"));
        assert_eq!(basename("  ").as_deref(), None);
    }

    #[test]
    fn section_choice_matches_case_insensitively() {
        let options = section_options(&["Work".to_string()]);
        assert_eq!(section_choice(&options, Some("work")), 1);
        assert_eq!(section_choice(&options, Some("Misc")), 0);
        assert_eq!(section_choice(&options, None), 0);
    }

    #[test]
    fn visible_fields_follow_the_kind() {
        let git = RepoForm::for_add("/p", RepoKind::Git, &[]);
        assert!(git.fields().contains(&Field::Fav));
        assert!(!git.fields().contains(&Field::Section));

        let folder = RepoForm::for_add("/p", RepoKind::Path, &[]);
        assert!(folder.fields().contains(&Field::Section));
        assert!(!folder.fields().contains(&Field::Fav));

        // The backup toggle shows for both kinds.
        assert!(git.fields().contains(&Field::Backup));
        assert!(folder.fields().contains(&Field::Backup));
    }

    #[test]
    fn for_add_seeds_backup_toggle_per_kind() {
        // Git repos default to included, file/folder entries to excluded.
        let git = RepoForm::for_add("/p", RepoKind::Git, &[]);
        assert!(git.draft().include_in_backup);
        let folder = RepoForm::for_add("/p", RepoKind::Path, &[]);
        assert!(!folder.draft().include_in_backup);
    }
}