kizu 0.7.0

Realtime diff monitor + inline scar review TUI for AI coding agents (Claude Code, etc.)
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
use std::path::{Path, PathBuf};

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

use crate::git::{self, DiffContent, LineKind};
use crate::scar::{ScarKind, ScarRemove, insert_scar, remove_scar};

use super::{
    App, EditorInvocation, RowKind, TextInputKeyEffect, ViewMode, build_editor_invocation,
    edit_insert_str, handle_text_input_edit, hunk_fingerprint, seen_hunk_fingerprint,
};

#[derive(Debug, Clone)]
pub struct ScarUndoEntry {
    pub path: PathBuf,
    pub line_1indexed: usize,
    pub rendered: String,
}

/// Free-text scar input overlay. The `c` key enters this mode when the
/// cursor is on a scar-able row; `Enter` commits the accumulated
/// [`Self::body`] as a `@kizu[free]:` scar above the target line and
/// `Esc` cancels without touching the file. The target is captured at
/// entry time (not re-read on commit) so that a watcher-driven diff
/// recompute during typing cannot silently retarget the write.
#[derive(Debug, Clone)]
pub struct ScarCommentState {
    pub target_path: PathBuf,
    pub target_line: usize,
    pub body: String,
    /// Cursor position as a **char index** (not byte offset).
    pub cursor_pos: usize,
}

/// Confirmation overlay for hunk revert (`x` key). Holds the
/// `(file_idx, hunk_idx)` captured the moment the user pressed `x`
/// so a watcher-driven recompute while the dialog is open cannot
/// re-target the operation. `y`/`Y`/`Enter` confirms and runs
/// `git apply --reverse`; any other key closes the overlay without
/// touching the worktree. See plans/v0.2.md M4 Decision Log.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RevertConfirmState {
    pub file_idx: usize,
    pub hunk_idx: usize,
    pub file_path: PathBuf,
    /// Stable hunk identity captured when the confirmation overlay
    /// opened. Used in `confirm_revert` to re-resolve the hunk by
    /// content identity if watcher refreshes reorder or rebuild the
    /// layout while the overlay is visible.
    pub hunk_old_start: usize,
}

impl App {
    /// Drop any pending scar-focus target. Called from navigation
    /// key dispatch so the user's explicit movement "sticks" — the
    /// next watcher-driven recompute won't yank them back to the
    /// scar line they've since moved past.
    pub(crate) fn clear_scar_focus_on_nav(&mut self) {
        self.scar_focus = None;
    }

    /// Insert a scar of the given `kind` with `body` as the human
    /// text, at the cursor's current position. No-op when the
    /// cursor is not on a diff row (file header, hunk header,
    /// spacer, binary notice). Write failures from
    /// [`crate::scar::insert_scar`] are captured in `last_error` so
    /// the footer surfaces them instead of panicking. The watcher
    /// picks up the resulting write on its next tick and re-runs
    /// `compute_diff`, which shows the new scar line in place.
    pub fn insert_canned_scar(&mut self, kind: ScarKind, body: &str) {
        if self.view_mode == ViewMode::Stream {
            return;
        }
        let Some((path, line)) = self.scar_target_line() else {
            return;
        };
        match insert_scar(&path, line, kind, body) {
            Ok(Some(receipt)) => {
                let focus = Some((path.clone(), receipt.line_1indexed));
                self.scar_undo_stack.push(ScarUndoEntry {
                    path,
                    line_1indexed: receipt.line_1indexed,
                    rendered: receipt.rendered,
                });
                self.refresh_after_scar_write(focus);
            }
            Ok(None) => {}
            Err(err) => {
                self.last_error = Some(format!("scar: {err:#}"));
            }
        }
    }

    /// Enter free-text scar input mode. Captures the current
    /// cursor's target `(path, line)` so a watcher-driven recompute
    /// while the user is typing cannot retarget the write. No-op
    /// when the cursor is not on a scar-able row.
    pub fn open_scar_comment(&mut self) {
        if self.view_mode == ViewMode::Stream {
            return;
        }
        let Some((target_path, target_line)) = self.scar_target_line() else {
            return;
        };
        self.scar_comment = Some(ScarCommentState {
            target_path,
            target_line,
            body: String::new(),
            cursor_pos: 0,
        });
    }

    /// Abort free-text scar input without writing anything.
    pub fn close_scar_comment(&mut self) {
        self.scar_comment = None;
    }

    /// Commit the currently-composed free-text scar, if any. Empty
    /// body is treated as a cancel (so double-`Enter` on an empty
    /// input does not write a blank scar). Write failures land on
    /// `last_error` with the same `scar:` prefix used by the canned
    /// `a` / `r` dispatch.
    pub fn commit_scar_comment(&mut self) {
        let Some(state) = self.scar_comment.take() else {
            return;
        };
        let body = state.body.trim();
        if body.is_empty() {
            return;
        }
        match insert_scar(&state.target_path, state.target_line, ScarKind::Free, body) {
            Ok(Some(receipt)) => {
                let focus = Some((state.target_path.clone(), receipt.line_1indexed));
                self.scar_undo_stack.push(ScarUndoEntry {
                    path: state.target_path,
                    line_1indexed: receipt.line_1indexed,
                    rendered: receipt.rendered,
                });
                self.refresh_after_scar_write(focus);
            }
            Ok(None) => {}
            Err(err) => {
                self.last_error = Some(format!("scar: {err:#}"));
            }
        }
    }

    fn refresh_after_scar_write(&mut self, focus: Option<(PathBuf, usize)>) {
        self.scar_focus = focus;
        if let Ok(files) = git::compute_diff(&self.root, &self.baseline_sha) {
            self.apply_computed_files(files);
        }
        if let Some(fv) = self.file_view.as_mut() {
            let abs = self.root.join(&fv.path);
            if let Ok(content) = std::fs::read_to_string(&abs) {
                fv.lines = content.lines().map(String::from).collect();
                let max = fv.lines.len().saturating_sub(1);
                if fv.cursor > max {
                    fv.cursor = max;
                }
            }
        }
    }

    pub(crate) fn find_new_file_line_row(&self, abs: &Path, line_1indexed: usize) -> Option<usize> {
        let rel = abs.strip_prefix(&self.root).unwrap_or(abs);
        let file_idx = self.files.iter().position(|f| f.path == rel)?;
        let DiffContent::Text(hunks) = &self.files[file_idx].content else {
            return None;
        };
        for (hunk_idx, hunk) in hunks.iter().enumerate() {
            let mut new_line = hunk.new_start;
            for (offset, dl) in hunk.lines.iter().enumerate() {
                if matches!(dl.kind, LineKind::Deleted) {
                    continue;
                }
                if new_line == line_1indexed {
                    return self.layout.rows.iter().position(|r| {
                        matches!(
                            r,
                            RowKind::DiffLine {
                                file_idx: f,
                                hunk_idx: hi,
                                line_idx: li,
                            } if *f == file_idx && *hi == hunk_idx && *li == offset,
                        )
                    });
                }
                new_line += 1;
            }
        }
        None
    }

    pub(crate) fn scroll_cursor_new_line(&self) -> Option<(PathBuf, usize)> {
        let row = self.layout.rows.get(self.scroll)?;
        let RowKind::DiffLine {
            file_idx,
            hunk_idx,
            line_idx,
        } = *row
        else {
            return None;
        };
        let file = self.files.get(file_idx)?;
        let DiffContent::Text(hunks) = &file.content else {
            return None;
        };
        let hunk = hunks.get(hunk_idx)?;
        let mut new_line = hunk.new_start;
        for (i, dl) in hunk.lines.iter().enumerate() {
            if i == line_idx {
                return Some((self.root.join(&file.path), new_line));
            }
            if !matches!(dl.kind, LineKind::Deleted) {
                new_line += 1;
            }
        }
        None
    }

    pub(crate) fn find_nearest_new_file_line_row(
        &self,
        abs: &Path,
        target_line: usize,
    ) -> Option<usize> {
        let rel = abs.strip_prefix(&self.root).unwrap_or(abs);
        let file_idx = self.files.iter().position(|f| f.path == rel)?;
        let DiffContent::Text(hunks) = &self.files[file_idx].content else {
            return None;
        };
        let mut best: Option<(usize, usize, usize)> = None;
        for (hunk_idx, hunk) in hunks.iter().enumerate() {
            let mut new_line = hunk.new_start;
            for (offset, dl) in hunk.lines.iter().enumerate() {
                if matches!(dl.kind, LineKind::Deleted) {
                    continue;
                }
                let distance = new_line.abs_diff(target_line);
                if best.is_none_or(|(d, _, _)| distance < d) {
                    best = Some((distance, hunk_idx, offset));
                }
                new_line += 1;
            }
        }
        let (_, hunk_idx, line_idx) = best?;
        self.layout.rows.iter().position(|r| {
            matches!(
                r,
                RowKind::DiffLine {
                    file_idx: f,
                    hunk_idx: h,
                    line_idx: l,
                } if *f == file_idx && *h == hunk_idx && *l == line_idx,
            )
        })
    }

    pub fn undo_scar(&mut self) {
        let Some(entry) = self.scar_undo_stack.pop() else {
            return;
        };
        match remove_scar(&entry.path, entry.line_1indexed, &entry.rendered) {
            Ok(ScarRemove::Removed) => {
                self.refresh_after_scar_write(Some((entry.path.clone(), entry.line_1indexed)));
            }
            Ok(ScarRemove::Mismatch) => {
                self.last_error = Some(format!(
                    "undo: line {} in {} was edited — skipped",
                    entry.line_1indexed,
                    entry.path.display(),
                ));
            }
            Ok(ScarRemove::OutOfRange) => {
                self.last_error = Some(format!(
                    "undo: {} has fewer than {} lines — skipped",
                    entry.path.display(),
                    entry.line_1indexed,
                ));
            }
            Err(err) => {
                self.last_error = Some(format!("undo: {err:#}"));
            }
        }
    }

    pub fn toggle_seen_current_hunk(&mut self) {
        let Some((file_idx, hunk_idx)) = self.current_hunk() else {
            return;
        };
        let Some(file) = self.files.get(file_idx) else {
            return;
        };
        let DiffContent::Text(hunks) = &file.content else {
            return;
        };
        let Some(hunk) = hunks.get(hunk_idx) else {
            return;
        };
        let key = (file.path.clone(), hunk.old_start);
        if self.seen_hunks.remove(&key).is_none() {
            let fp = hunk_fingerprint(hunk);
            self.seen_hunks.insert(key, fp);
        }
        let target_hunk = (file_idx, hunk_idx);
        self.build_layout();
        let cursor_hunk = match self.layout.rows.get(self.scroll) {
            Some(RowKind::HunkHeader { file_idx, hunk_idx }) => Some((*file_idx, *hunk_idx)),
            Some(RowKind::DiffLine {
                file_idx, hunk_idx, ..
            }) => Some((*file_idx, *hunk_idx)),
            _ => None,
        };
        if cursor_hunk != Some(target_hunk)
            && let Some(row) = self.layout.rows.iter().position(|r| {
                matches!(
                    r,
                    RowKind::HunkHeader { file_idx: f, hunk_idx: h }
                        if (*f, *h) == target_hunk
                )
            })
        {
            self.scroll_to(row);
        }
    }

    pub fn hunk_is_seen(&self, file_idx: usize, hunk_idx: usize) -> bool {
        let Some(file) = self.files.get(file_idx) else {
            return false;
        };
        let DiffContent::Text(hunks) = &file.content else {
            return false;
        };
        let Some(hunk) = hunks.get(hunk_idx) else {
            return false;
        };
        let Some(marked_fp) = seen_hunk_fingerprint(&self.seen_hunks, &file.path, hunk.old_start)
        else {
            return false;
        };
        let current_fp = self
            .layout
            .hunk_fingerprints
            .get(file_idx)
            .and_then(|fps| fps.get(hunk_idx))
            .copied()
            .flatten()
            .unwrap_or_else(|| hunk_fingerprint(hunk));
        marked_fp == current_fp
    }

    pub fn open_in_editor(&self, editor_env: Option<&str>) -> Option<EditorInvocation> {
        if self.view_mode == ViewMode::Stream {
            return None;
        }
        let (path, line) = self.scar_target_line()?;
        build_editor_invocation(editor_env, line, &path)
    }

    pub fn open_revert_confirm(&mut self) {
        if self.view_mode == ViewMode::Stream {
            return;
        }
        let Some((file_idx, hunk_idx)) = self.current_hunk() else {
            return;
        };
        let Some(file) = self.files.get(file_idx) else {
            return;
        };
        let DiffContent::Text(hunks) = &file.content else {
            return;
        };
        let Some(hunk) = hunks.get(hunk_idx) else {
            return;
        };
        self.revert_confirm = Some(RevertConfirmState {
            file_idx,
            hunk_idx,
            file_path: file.path.clone(),
            hunk_old_start: hunk.old_start,
        });
    }

    pub fn close_revert_confirm(&mut self) {
        self.revert_confirm = None;
    }

    pub fn confirm_revert(&mut self) {
        let Some(state) = self.revert_confirm.take() else {
            return;
        };
        let hunk = self
            .files
            .iter()
            .find(|f| f.path == state.file_path)
            .and_then(|f| match &f.content {
                DiffContent::Text(hunks) => {
                    hunks.iter().find(|h| h.old_start == state.hunk_old_start)
                }
                _ => None,
            });
        let Some(hunk) = hunk else {
            self.last_error = Some("revert: hunk no longer present".into());
            return;
        };
        let patch = git::build_hunk_patch(&state.file_path, hunk);
        if let Err(err) = git::revert_hunk(&self.root, &patch) {
            self.last_error = Some(format!("revert: {err:#}"));
        }
    }

    pub(crate) fn handle_revert_confirm_key(&mut self, key: KeyEvent) {
        if key.modifiers.contains(KeyModifiers::CONTROL) {
            self.close_revert_confirm();
            return;
        }
        match key.code {
            KeyCode::Char('y') | KeyCode::Char('Y') | KeyCode::Enter => self.confirm_revert(),
            _ => self.close_revert_confirm(),
        }
    }

    pub fn handle_paste(&mut self, text: &str) {
        if let Some(state) = self.scar_comment.as_mut() {
            edit_insert_str(&mut state.body, &mut state.cursor_pos, text);
        } else if let Some(state) = self.search_input.as_mut() {
            edit_insert_str(&mut state.query, &mut state.cursor_pos, text);
        }
    }

    pub(crate) fn handle_scar_comment_key(&mut self, key: KeyEvent) {
        let Some(s) = self.scar_comment.as_mut() else {
            return;
        };
        match handle_text_input_edit(key, &mut s.body, &mut s.cursor_pos) {
            TextInputKeyEffect::Continue => {}
            TextInputKeyEffect::Commit => self.commit_scar_comment(),
            TextInputKeyEffect::Cancel => self.close_scar_comment(),
        }
    }

    pub fn scar_target_line(&self) -> Option<(PathBuf, usize)> {
        if let Some(fv) = &self.file_view {
            return Some((self.root.join(&fv.path), fv.cursor + 1));
        }
        let row = self.layout.rows.get(self.scroll)?;
        let (file_idx, hunk_idx, diff_line_idx) = match *row {
            RowKind::DiffLine {
                file_idx,
                hunk_idx,
                line_idx,
            } => (file_idx, hunk_idx, Some(line_idx)),
            RowKind::HunkHeader { file_idx, hunk_idx } => (file_idx, hunk_idx, None),
            _ => return None,
        };
        let file = self.files.get(file_idx)?;
        let DiffContent::Text(hunks) = &file.content else {
            return None;
        };
        let hunk = hunks.get(hunk_idx)?;

        let Some(line_idx) = diff_line_idx else {
            for (offset, dl) in hunk.lines.iter().enumerate() {
                if !matches!(dl.kind, LineKind::Context) {
                    return Some((self.root.join(&file.path), hunk.new_start + offset));
                }
            }
            return Some((self.root.join(&file.path), hunk.new_start));
        };

        let mut offset: usize = 0;
        for (i, line) in hunk.lines.iter().enumerate() {
            if i > line_idx {
                break;
            }
            let is_deleted = matches!(line.kind, LineKind::Deleted);
            if i == line_idx {
                return Some((self.root.join(&file.path), hunk.new_start + offset));
            }
            if !is_deleted {
                offset += 1;
            }
        }
        None
    }
}