sluuz 0.6.2

git, but it sleuths - a git superset with cross-repo search, secret scanning, and multi-repo management
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
//! `slu istatus` - interactive `git status` (TUI) for the current repo.
//!
//! Top pane: the changed files. Bottom pane: the selected file's diff
//! (syntax-highlighted, via the shared `tui` renderer). `h`/`l` (or `←`/`→`)
//! slide the scope between **staged**, **all**, and **unstaged**; `s`/`u`/Space
//! stage / unstage / toggle the selected file; Ctrl-↑/↓ (or Ctrl-j/k) and
//! Ctrl-d/u scroll the diff. `j`/`k` move the file list. `q` / `Esc` / `Ctrl-C`
//! quit.
//!
//! This is the interactive counterpart to `slu repos` (which is cross-repo).

use crate::git::{git_capture, git_capture_raw, git_run};
use crate::tui::difftool::{DiffTool, run_difftool};
use crate::tui::highlight::{RenderedDiff, prepare_diff, render_prepared};
use crate::tui::input::{
    CTRL_X_MOVE, CTRL_Y_MOVE, X_MOVE, Y_MOVE, is_down, is_left, is_right, is_up, norm_esc,
};
use crate::tui::widgets::{Modal, diff_hscrollbar, diff_scrollbar, list_scrollbar, pane_block};
use crate::tui::{
    clamp_hscroll, clamp_scroll, half_page, pane_height, pane_width, pop_keyboard_enhancement,
    push_keyboard_enhancement,
};
use ratatui::DefaultTerminal;
use ratatui::crossterm::event::{self, Event, KeyCode, KeyEventKind, KeyModifiers};
use ratatui::layout::{Constraint, Layout};
use ratatui::style::{Color, Modifier, Style};
use ratatui::text::{Line, Span, Text};
use ratatui::widgets::{List, ListItem, ListState, Paragraph};
use std::io::{self, IsTerminal};

/// Rows a Ctrl-j/k moves the diff, columns a Ctrl-h/l pans it, and the PgUp/PgDn
/// jump.
const SCROLL_STEP: u16 = 3;
const PAN_STEP: u16 = 8;
const PAGE_STEP: u16 = 10;

#[derive(clap::Args)]
pub struct Args {}

/// Which slice of the working tree the file list is showing.
#[derive(Clone, Copy, PartialEq)]
enum Scope {
    Staged,
    All,
    Unstaged,
}

/// Left→right order for the `h`/`l` slider; `All` sits in the middle.
const SCOPES: [Scope; 3] = [Scope::Staged, Scope::All, Scope::Unstaged];

impl Scope {
    fn label(self) -> &'static str {
        match self {
            Scope::Staged => "staged",
            Scope::All => "all",
            Scope::Unstaged => "unstaged",
        }
    }
}

/// One `git status` entry: index status `x`, worktree status `y`, and the path.
struct Entry {
    x: char,
    y: char,
    path: String,
}

impl Entry {
    /// Has staged (index) changes.
    fn staged(&self) -> bool {
        self.x != ' ' && self.x != '?'
    }
    /// Has unstaged (worktree) changes, including untracked files.
    fn unstaged(&self) -> bool {
        self.x == '?' || (self.y != ' ' && self.y != '?')
    }
    fn untracked(&self) -> bool {
        self.x == '?'
    }
    fn in_scope(&self, s: Scope) -> bool {
        match s {
            Scope::Staged => self.staged(),
            Scope::Unstaged => self.unstaged(),
            Scope::All => self.staged() || self.unstaged(),
        }
    }
}

/// Everything the view holds. `root` is on it because every git call needs it,
/// which is what used to make the helpers below take four arguments each.
struct App {
    root: String,
    enhanced: bool,
    width: u16,
    entries: Vec<Entry>,
    /// Indices into `entries` that the current scope keeps.
    visible: Vec<usize>,
    sel: usize,
    scope_idx: usize,
    state: ListState,
    /// Transient status line (a difftool result, mostly).
    msg: Option<String>,
    /// A message that has to be read before anything else happens: it owns
    /// every key until it is dismissed.
    modal: Option<Modal>,
    prepared: RenderedDiff,
    diff: Text<'static>,
    diff_scroll: u16,
    diff_hscroll: u16,
}

pub fn run(_args: Args) {
    if !io::stdout().is_terminal() {
        eprintln!("slu istatus needs an interactive terminal - use `git status` instead");
        return;
    }
    // Anchor every git call at the repo root. `git status` reports paths
    // relative to the root, so if we ran diff/add from a subdirectory the
    // pathspecs wouldn't resolve ("Could not access '…'").
    let root = match git_capture(".", &["rev-parse", "--show-toplevel"]) {
        Some(r) if !r.is_empty() => r,
        _ => {
            eprintln!("slu istatus: not inside a git repository");
            return;
        }
    };

    let mut app = App {
        root,
        enhanced: false,
        width: 120,
        entries: Vec::new(),
        visible: Vec::new(),
        sel: 0,
        scope_idx: 1, // default: All
        state: ListState::default(),
        msg: None,
        modal: None,
        prepared: RenderedDiff::default(),
        diff: Text::default(),
        diff_scroll: 0,
        diff_hscroll: 0,
    };

    let mut terminal = ratatui::init();
    app.enhanced = push_keyboard_enhancement();
    app.width = pane_width(&terminal);
    app.reload();
    let result = app.event_loop(&mut terminal);
    if app.enhanced {
        pop_keyboard_enhancement();
    }
    ratatui::restore();

    if let Err(e) = result {
        eprintln!("slu istatus: {e}");
    }
}

impl App {
    fn scope(&self) -> Scope {
        SCOPES[self.scope_idx]
    }

    /// The entry the cursor is on.
    fn current(&self) -> Option<&Entry> {
        self.visible.get(self.sel).map(|&i| &self.entries[i])
    }

    /// Re-read the working tree, then re-filter and re-diff.
    fn reload(&mut self) {
        self.entries = load_status(&self.root);
        self.rescope();
    }

    /// Re-filter for the current scope, keep the cursor in range, and refresh
    /// the diff pane under it.
    fn rescope(&mut self) {
        self.visible = self
            .entries
            .iter()
            .enumerate()
            .filter(|(_, e)| e.in_scope(self.scope()))
            .map(|(i, _)| i)
            .collect();
        if self.sel >= self.visible.len() {
            self.sel = self.visible.len().saturating_sub(1);
        }
        self.state
            .select((!self.visible.is_empty()).then_some(self.sel));
        self.diff_scroll = 0;
        self.diff_hscroll = 0;
        self.refresh_diff();
    }

    /// Highlight the selected file's diff. This is the expensive syntect pass,
    /// so it runs on selection changes only, never on a scroll.
    fn refresh_diff(&mut self) {
        match self.current() {
            Some(entry) => {
                let raw = diff_for(&self.root, entry, self.scope());
                self.prepared = prepare_diff(&raw);
                self.diff = render_prepared(&self.prepared, self.width, 0);
            }
            None => {
                self.prepared = RenderedDiff::default();
                self.diff = Text::default();
            }
        }
    }

    /// Run a git command against the selected file, reporting whether the
    /// working tree changed.
    fn on_current(&self, args: &[&str]) -> bool {
        match self.current() {
            Some(e) => {
                let mut argv = args.to_vec();
                argv.extend_from_slice(&["--", &e.path]);
                git_run(&self.root, &argv).0
            }
            None => false,
        }
    }

    /// Space: stage a file that has unstaged changes, else unstage it.
    fn toggle(&self) -> bool {
        match self.current() {
            Some(e) if e.unstaged() => self.on_current(&["add"]),
            Some(_) => self.on_current(&["restore", "--staged"]),
            None => false,
        }
    }

    /// Open the selected file in the user's difftool, matching the comparison
    /// the pane shows. Returns whether the tree may have changed under it.
    fn difftool(&mut self, terminal: &mut DefaultTerminal) -> bool {
        let Some(e) = self.current() else {
            return false;
        };
        let scope = self.scope();
        let cached = scope == Scope::Staged || (scope == Scope::All && !e.unstaged());
        let (path, untracked) = (e.path.clone(), e.untracked());

        if untracked {
            self.msg = Some("untracked - nothing to compare".to_string());
            return false;
        }
        let args: &[&str] = if cached { &["--cached", "--"] } else { &["--"] };
        let mut argv = args.to_vec();
        argv.push(&path);
        let outcome = run_difftool(terminal, self.enhanced, &self.root, &argv);
        self.width = pane_width(terminal);
        match outcome {
            DiffTool::Quiet => {}
            DiffTool::Note(m) => self.msg = Some(m),
            DiffTool::Failed(modal) => self.modal = Some(modal),
        }
        true // a difftool edit may have changed the file
    }

    fn event_loop(&mut self, terminal: &mut DefaultTerminal) -> io::Result<()> {
        loop {
            terminal.draw(|frame| draw(frame, self))?;

            match event::read()? {
                Event::Resize(_, _) => {
                    let w = pane_width(terminal);
                    if w != self.width {
                        self.width = w;
                        self.diff = render_prepared(&self.prepared, self.width, self.diff_hscroll);
                    }
                }
                Event::Key(key) if key.kind == KeyEventKind::Press => {
                    let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
                    let code = norm_esc(key.code, ctrl);

                    // Ctrl-C quits from anywhere, even out from under a modal.
                    if ctrl && code == KeyCode::Char('c') {
                        break;
                    }
                    // A modal owns every key until it is dismissed.
                    if let Some(modal) = &mut self.modal {
                        if modal.on_key(code) {
                            self.modal = None;
                        }
                        continue;
                    }

                    self.msg = None; // any keypress clears a stale status message
                    if matches!(code, KeyCode::Char('q') | KeyCode::Esc) {
                        break;
                    }
                    self.on_key(code, ctrl, terminal);
                    self.diff_scroll = clamp_scroll(
                        self.diff_scroll,
                        self.diff.lines.len(),
                        pane_height(terminal),
                    );
                }
                _ => {}
            }
        }
        Ok(())
    }

    fn on_key(&mut self, code: KeyCode, ctrl: bool, terminal: &mut DefaultTerminal) {
        let half = half_page(terminal);
        let mut moved = false; // selection or scope changed → re-diff
        let mut reload = false; // working tree changed → re-read status

        if ctrl && is_down(code) {
            self.diff_scroll = self.diff_scroll.saturating_add(SCROLL_STEP);
        } else if ctrl && is_up(code) {
            self.diff_scroll = self.diff_scroll.saturating_sub(SCROLL_STEP);
        } else if ctrl && code == KeyCode::Char('d') {
            self.diff_scroll = self.diff_scroll.saturating_add(half);
        } else if ctrl && code == KeyCode::Char('u') {
            self.diff_scroll = self.diff_scroll.saturating_sub(half);
        } else if ctrl && is_right(code) {
            self.diff_hscroll = clamp_hscroll(
                self.diff_hscroll.saturating_add(PAN_STEP),
                self.prepared.max_line(),
                self.prepared.cell_width(self.width),
            );
            self.diff = render_prepared(&self.prepared, self.width, self.diff_hscroll);
        } else if ctrl && is_left(code) {
            self.diff_hscroll = self.diff_hscroll.saturating_sub(PAN_STEP);
            self.diff = render_prepared(&self.prepared, self.width, self.diff_hscroll);
        } else if code == KeyCode::PageDown {
            self.diff_scroll = self.diff_scroll.saturating_add(PAGE_STEP);
        } else if code == KeyCode::PageUp {
            self.diff_scroll = self.diff_scroll.saturating_sub(PAGE_STEP);
        } else if is_down(code) && self.sel + 1 < self.visible.len() {
            self.sel += 1;
            moved = true;
        } else if is_up(code) && self.sel > 0 {
            self.sel -= 1;
            moved = true;
        } else if !ctrl && is_left(code) && self.scope_idx > 0 {
            self.scope_idx -= 1;
            self.sel = 0;
            moved = true;
        } else if !ctrl && is_right(code) && self.scope_idx + 1 < SCOPES.len() {
            self.scope_idx += 1;
            self.sel = 0;
            moved = true;
        } else if code == KeyCode::Char('s') {
            reload = self.on_current(&["add"]);
        } else if code == KeyCode::Char('u') {
            reload = self.on_current(&["restore", "--staged"]);
        } else if code == KeyCode::Char(' ') {
            reload = self.toggle();
        } else if code == KeyCode::Char('r') {
            reload = true;
        } else if code == KeyCode::Enter {
            reload = self.difftool(terminal);
        }

        if reload {
            self.reload();
        } else if moved {
            self.rescope();
        }
    }
}

/// The raw diff for one entry. Staged scope shows the index-vs-HEAD diff;
/// unstaged shows worktree-vs-index; `All` prefers the worktree diff when the
/// file has unstaged changes, else the staged one. Untracked files are shown as
/// an all-added diff against the null device.
fn diff_for(root: &str, entry: &Entry, scope: Scope) -> String {
    if entry.untracked() {
        let nul = if cfg!(windows) { "NUL" } else { "/dev/null" };
        // `--no-index` exits non-zero when files differ, so read it via git_run.
        let (_, out) = git_run(root, &["diff", "--no-index", "--", nul, &entry.path]);
        return out;
    }
    let cached = match scope {
        Scope::Staged => true,
        Scope::Unstaged => false,
        Scope::All => !entry.unstaged(),
    };
    let args: &[&str] = if cached {
        &["diff", "--cached", "--", &entry.path]
    } else {
        &["diff", "--", &entry.path]
    };
    git_capture(root, args).unwrap_or_default()
}

/// Parse `git status --porcelain -z` into entries. `-z` NUL-separates records
/// (so paths with spaces/newlines are safe) and, for renames/copies, follows the
/// record with an extra NUL-terminated original path, which we skip.
fn load_status(root: &str) -> Vec<Entry> {
    // `git_capture_raw`, not `git_capture`: the porcelain's first column is a
    // SPACE when a file has no staged change, and trimming would eat it on the
    // first record - shifting the status codes and the path by one char.
    let raw = match git_capture_raw(root, &["status", "--porcelain", "-z"]) {
        Some(r) => r,
        None => return Vec::new(),
    };
    let mut tokens = raw.split('\0').filter(|t| !t.is_empty());
    let mut entries = Vec::new();
    while let Some(tok) = tokens.next() {
        let bytes = tok.as_bytes();
        if bytes.len() < 3 {
            continue;
        }
        let x = bytes[0] as char;
        let y = bytes[1] as char;
        if x == 'R' || x == 'C' {
            tokens.next(); // consume the original path of a rename/copy
        }
        entries.push(Entry {
            x,
            y,
            path: tok[3..].to_string(),
        });
    }
    entries
}

fn draw(frame: &mut ratatui::Frame, app: &mut App) {
    let areas = Layout::vertical([Constraint::Percentage(40), Constraint::Percentage(60)])
        .split(frame.area());

    // ── top: file list ──
    let items: Vec<ListItem> = app
        .visible
        .iter()
        .map(|&i| status_item(&app.entries[i]))
        .collect();
    let top_title = if app.visible.is_empty() {
        format!(" {}  clean   {X_MOVE} scope · q quit ", app.scope().label())
    } else {
        format!(
            " {}  {}/{}   {Y_MOVE} · {X_MOVE} scope · s/u/space stage · q quit ",
            app.scope().label(),
            app.sel + 1,
            app.visible.len()
        )
    };
    let list = List::new(items)
        .block(pane_block(top_title, true))
        .highlight_style(Style::default().add_modifier(Modifier::REVERSED))
        .highlight_symbol("› ");
    frame.render_stateful_widget(list, areas[0], &mut app.state);
    list_scrollbar(frame, areas[0], app.visible.len(), app.state.offset());

    // ── bottom: diff of the selected file ──
    let (path, tag) = match app.current() {
        Some(e) => (e.path.as_str(), diff_tag(e, app.scope())),
        None => ("", ""),
    };
    let title = match &app.msg {
        Some(m) => format!(" {path}  ⚠ {m} "),
        None if path.is_empty() => " (nothing to show) ".to_string(),
        None => {
            format!(" {path} {tag}  enter difftool · {CTRL_Y_MOVE} scroll · {CTRL_X_MOVE} pan ")
        }
    };
    let diff = Paragraph::new(app.diff.clone())
        .block(pane_block(title, true))
        .scroll((app.diff_scroll, 0));
    frame.render_widget(diff, areas[1]);
    diff_scrollbar(frame, areas[1], app.diff.lines.len(), app.diff_scroll);
    let cell = app.prepared.cell_width(areas[1].width.saturating_sub(2));
    diff_hscrollbar(
        frame,
        areas[1],
        app.prepared.max_line(),
        cell,
        app.diff_hscroll,
    );

    if let Some(modal) = &mut app.modal {
        modal.draw(frame);
    }
}

/// Which side of the diff the bottom pane is showing.
fn diff_tag(e: &Entry, scope: Scope) -> &'static str {
    if e.untracked() {
        "[untracked]"
    } else if scope == Scope::Staged || (scope == Scope::All && !e.unstaged()) {
        "[staged]"
    } else {
        "[worktree]"
    }
}

/// `git status`-style two-column code (staged left, unstaged right) + path.
fn status_item(e: &Entry) -> ListItem<'static> {
    let staged = Style::default().fg(Color::Green);
    let unstaged = Style::default().fg(Color::Red);
    let none = Style::default().fg(Color::DarkGray);

    let (xc, xs) = if e.staged() {
        (e.x, staged)
    } else {
        (' ', none)
    };
    let (yc, ys) = if e.untracked() {
        ('?', unstaged)
    } else if e.y != ' ' {
        (e.y, unstaged)
    } else {
        (' ', none)
    };

    ListItem::new(Line::from(vec![
        Span::styled(xc.to_string(), xs),
        Span::styled(yc.to_string(), ys),
        Span::raw("  "),
        Span::raw(e.path.clone()),
    ]))
}