diff options
| author | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-09-14 16:48:53 +0200 |
|---|---|---|
| committer | Lukasz Kasprzak <lukas@labunix.xyz> | 2026-09-14 16:48:53 +0200 |
| commit | e64f162bffa6ced845bac4d3015954c41c4b4a3e (patch) | |
| tree | 87b8fb52b476644ec99cd091bb34ac6521ae9b33 /cmd/krino | |
| parent | 0b5d0eb92c5be2f0ddb2fa73990f31e5654e57fe (diff) | |
| download | krino-0.0.6.tar.gz krino-0.0.6.zip | |
krino: 0.0.6 — w applies and quits, choices echoed in redv0.0.6
Diffstat (limited to 'cmd/krino')
| -rw-r--r-- | cmd/krino/review.go | 79 | ||||
| -rw-r--r-- | cmd/krino/review_test.go | 71 | ||||
| -rw-r--r-- | cmd/krino/sort.go | 15 | ||||
| -rw-r--r-- | cmd/krino/undo.go | 4 |
4 files changed, 141 insertions, 28 deletions
diff --git a/cmd/krino/review.go b/cmd/krino/review.go index 29b3915..36231be 100644 --- a/cmd/krino/review.go +++ b/cmd/krino/review.go @@ -49,13 +49,16 @@ func (k keyReader) Read(p []byte) (int, error) { // // [y] yes [n] no [a] yes to this and all remaining [t] trash [d] delete permanently [w] write, apply chosen so far [q] quit, apply nothing // -// prompt. action is always one of 'a', 'c', 's' or 'q': a [c] session's own -// [q] ("quit, apply nothing") folds into the same 'q' the caller already -// handles for the top-level menu, and approved is emptied to match - even a -// file already marked yes in that session is discarded, per spec §8.3's -// wording ("apply nothing"), unlike [w] ("apply chosen so far"), which -// keeps it. replaced holds the files [t] or [d] chose to trash or delete -// instead of what the rules planned; replaceChains applies it. root is the directory being reviewed - passed only to +// prompt. action is always one of 'a', 'c', 's', 'w' or 'q': a [c] +// session's own [q] ("quit, apply nothing") folds into the same 'q' the +// caller already handles for the top-level menu, and approved is emptied to +// match - even a file already marked yes in that session is discarded, per +// spec §8.3's wording ("apply nothing"), unlike [w] ("write, apply chosen so +// far"), which keeps it and is returned as 'w': the caller applies what was +// decided and stops. approved holds every file decided, true for yes and +// false for no; a file [w] left unreviewed is absent. replaced holds the +// files [t] or [d] chose to trash or delete instead of what the rules +// planned; replaceChains applies it. Enter is ignored at both prompts. root is the directory being reviewed - passed only to // reviewPerFile's destination rendering (review finding 1, fix round // 2026-09-12); nothing here uses it directly. func reviewChains(in io.Reader, out io.Writer, chains []plan.Chain, root string, p palette) (map[string]bool, map[string]plan.Kind, rune, error) { @@ -69,6 +72,8 @@ func reviewChains(in io.Reader, out io.Writer, chains []plan.Chain, root string, return nil, nil, 0, err } switch key { + case '\r', '\n': + continue case 'a': return approveAll(chains), nil, 'a', nil case 's': @@ -76,12 +81,15 @@ func reviewChains(in io.Reader, out io.Writer, chains []plan.Chain, root string, case 'q': return map[string]bool{}, nil, 'q', nil case 'c': - approved, replaced, quit, err := reviewPerFile(in, out, chains, root, p) + approved, replaced, end, err := reviewPerFile(in, out, chains, root, p) if err != nil { return nil, nil, 0, err } - if quit { + switch end { + case 'q': return map[string]bool{}, nil, 'q', nil + case 'w': + return approved, replaced, 'w', nil } return approved, replaced, 'c', nil default: @@ -97,14 +105,14 @@ func reviewChains(in io.Reader, out io.Writer, chains []plan.Chain, root string, // just that file; [a] approves it and every remaining file without asking // again; [t] and [d] approve it with its chain replaced by one trash or // permanent delete step, [d] only after a y to its own confirmation (any -// other key asks about the same file again); [w] stops asking and applies -// whatever was already chosen, -// declining the rest; [q] aborts the review entirely, discarding even files -// already marked yes - reported back to reviewChains via quit=true. root is +// other key asks about the same file again); every choice is echoed in red +// on its own line; [w] stops asking and applies whatever was already chosen, +// leaving the rest unreviewed - reported back as end 'w'; [q] aborts the +// review entirely, discarding even files already marked yes - end 'q'. root is // passed down so a destination inside root renders root-relative and one // outside it renders ~-abbreviated, exactly as in the plan (review finding // 1, fix round 2026-09-12). -func reviewPerFile(in io.Reader, out io.Writer, chains []plan.Chain, root string, p palette) (approved map[string]bool, replaced map[string]plan.Kind, quit bool, err error) { +func reviewPerFile(in io.Reader, out io.Writer, chains []plan.Chain, root string, p palette) (approved map[string]bool, replaced map[string]plan.Kind, end rune, err error) { approved = map[string]bool{} replaced = map[string]plan.Kind{} yesRest := false @@ -128,24 +136,31 @@ func reviewPerFile(in io.Reader, out io.Writer, chains []plan.Chain, root string } key, kerr := readKey(in) if kerr != nil { - return nil, nil, false, kerr + return nil, nil, 0, kerr } + var choice string switch key { + case '\r', '\n': + continue case 'y': approved[c.File.Rel] = true + choice = "yes" case 'n': - // leave unapproved + approved[c.File.Rel] = false + choice = "no" case 'a': approved[c.File.Rel] = true yesRest = true + choice = "yes, and all remaining" case 't': approved[c.File.Rel] = true replaced[c.File.Rel] = plan.Trash + choice = "trash" case 'd': fmt.Fprintf(out, " delete %s permanently? [y/N] ", c.File.Rel) confirm, kerr := readKey(in) if kerr != nil { - return nil, nil, false, kerr + return nil, nil, 0, kerr } fmt.Fprintln(out) if confirm != 'y' { @@ -155,18 +170,42 @@ func reviewPerFile(in io.Reader, out io.Writer, chains []plan.Chain, root string } approved[c.File.Rel] = true replaced[c.File.Rel] = plan.DeletePermanent + choice = "DELETE permanently" case 'w': - return approved, replaced, false, nil + return approved, replaced, 'w', nil case 'q': - return nil, nil, true, nil + return nil, nil, 'q', nil default: fmt.Fprintf(out, "%q is not y, n, a, t, d, w or q\n", key) continue } + fmt.Fprintln(out, " "+p.bad("→ "+choice)) break } } - return approved, replaced, false, nil + return approved, replaced, 0, nil +} + +// reviewedChains returns the chains of the files decided holds, yes or no, +// in order: after [w], only these go to Apply, so a file never reviewed is +// neither applied nor logged as declined. +func reviewedChains(chains []plan.Chain, decided map[string]bool) []plan.Chain { + var out []plan.Chain + for _, c := range chains { + if _, ok := decided[c.File.Rel]; ok { + out = append(out, c) + } + } + return out +} + +// withNotReviewed appends to an outcome line how many files [w] left +// unreviewed, when any were. +func withNotReviewed(line string, n int) string { + if n == 0 { + return line + } + return fmt.Sprintf("%s · %d not reviewed", line, n) } // perFileKeys is the per-file prompt of spec §8.3, wrapped to the terminal diff --git a/cmd/krino/review_test.go b/cmd/krino/review_test.go index 19a1d01..a1fbba7 100644 --- a/cmd/krino/review_test.go +++ b/cmd/krino/review_test.go @@ -46,10 +46,17 @@ func TestApplyAllAndSkip(t *testing.T) { } func TestPerFileWriteStopsAsking(t *testing.T) { - // c, y for the first, then w: apply what was chosen so far. - approved, _, _, _ := reviewChains(strings.NewReader("cyw"), new(strings.Builder), chains("a", "b", "c"), "", palette{}) - if !approved["a"] || approved["b"] || approved["c"] { - t.Errorf("approved = %v; want only a", approved) + // c, y for the first, n for the second, then w: apply what was chosen so + // far and quit. Only the files reached are decided; c was never reviewed. + decided, _, action, _ := reviewChains(strings.NewReader("cynw"), new(strings.Builder), chains("a", "b", "c"), "", palette{}) + if action != 'w' { + t.Errorf("action = %q, want 'w'", action) + } + if got, ok := decided["b"]; !ok || got { + t.Errorf("b should be decided as declined: %v", decided) + } + if _, ok := decided["c"]; ok || !decided["a"] { + t.Errorf("decided = %v; want a approved, b declined, c not reviewed", decided) } } @@ -162,7 +169,7 @@ func TestPerFileWrapsToTheTerminal(t *testing.T) { if n := utf8.RuneCountInString(l); n > 60 { t.Errorf("line of %d runes exceeds width 60: %q", n, l) } - if strings.HasPrefix(l, " [y] yes") || (prompt != "" && strings.HasPrefix(l, " ") && !strings.HasPrefix(l, " ")) { + if strings.HasPrefix(l, " [y] yes") || (prompt != "" && strings.HasPrefix(l, " ") && !strings.HasPrefix(l, " ") && !strings.HasPrefix(l, " →")) { prompt += strings.TrimPrefix(l, " ") } } @@ -234,3 +241,57 @@ func TestReplaceChains(t *testing.T) { t.Errorf("replaceChains changed its input: %+v", cs[0].Steps) } } + +// TestPerFileEchoesChoiceInRed: every choice is confirmed on its own line, +// red when colour is on. +func TestPerFileEchoesChoiceInRed(t *testing.T) { + out := new(strings.Builder) + if _, _, _, err := reviewChains(strings.NewReader("cyntdya"), out, chains("a", "b", "c", "d", "e", "f"), "", palette{on: true}); err != nil { + t.Fatal(err) + } + for _, want := range []string{"yes", "no", "trash", "DELETE permanently", "yes, and all remaining"} { + if line := " \x1b[31m→ " + want + "\x1b[0m\n"; !strings.Contains(out.String(), line) { + t.Errorf("no red %q line in:\n%q", want, out) + } + } + plain := new(strings.Builder) + reviewChains(strings.NewReader("cn"), plain, chains("a"), "", palette{}) + if !strings.Contains(plain.String(), "\n → no\n") { + t.Errorf("without colour the choice should still be shown:\n%s", plain) + } +} + +// TestEnterIsIgnored: Enter, at either prompt, is not a key krino +// complains about. +func TestEnterIsIgnored(t *testing.T) { + out := new(strings.Builder) + decided, _, action, err := reviewChains(strings.NewReader("\rc\ry\n"), out, chains("a"), "", palette{}) + if err != nil { + t.Fatal(err) + } + if action != 'c' || !decided["a"] { + t.Errorf("decided = %v action = %q", decided, action) + } + if strings.Contains(out.String(), "is not") { + t.Errorf("Enter was reported as a bad key:\n%s", out) + } +} + +// TestReviewedChains: after [w], only the files decided in review go to +// Apply; the rest are neither applied nor logged. +func TestReviewedChains(t *testing.T) { + got := reviewedChains(chains("a", "b", "c"), map[string]bool{"a": true, "b": false}) + if len(got) != 2 || got[0].File.Rel != "a" || got[1].File.Rel != "b" { + t.Errorf("reviewedChains = %+v; want a and b", got) + } +} + +// TestNotReviewedOutcome: the outcome line counts files [w] left unreviewed. +func TestNotReviewedOutcome(t *testing.T) { + if got := withNotReviewed("2 applied · 0 failed · 1 declined", 139); got != "2 applied · 0 failed · 1 declined · 139 not reviewed" { + t.Errorf("got %q", got) + } + if got := withNotReviewed("2 applied · 0 failed · 1 declined", 0); got != "2 applied · 0 failed · 1 declined" { + t.Errorf("got %q", got) + } +} diff --git a/cmd/krino/sort.go b/cmd/krino/sort.go index 3e5d951..04fd3e8 100644 --- a/cmd/krino/sort.go +++ b/cmd/krino/sort.go @@ -247,7 +247,16 @@ func cmdSort(g *globals, names []string, stdout, stderr io.Writer) int { // [t] and [d] in review: the file gets the one step chosen // there instead of the chain its rules planned. dp.Chains = replaceChains(dp.Chains, replaced) - res, aerr := e.Apply(ctx, dp, approved, j, run) + // [w]: apply what was decided, log nothing for the files never + // reached, and stop krino once this directory is applied. + toApply, notReviewed := dp, 0 + if action == 'w' { + reviewed := *dp + reviewed.Chains = reviewedChains(dp.Chains, approved) + toApply = &reviewed + notReviewed = len(actionable) - len(reviewedChains(actionable, approved)) + } + res, aerr := e.Apply(ctx, toApply, approved, j, run) if aerr != nil { if errors.Is(aerr, context.Canceled) || errors.Is(aerr, context.DeadlineExceeded) { // Interrupted mid-apply (fix round 2026-09-12/item 2): @@ -263,13 +272,13 @@ func cmdSort(g *globals, names []string, stdout, stderr io.Writer) int { exit = 1 return false } - fmt.Fprintln(stdout, outcome(p, res.Applied, res.Failed, res.Declined)) + fmt.Fprintln(stdout, withNotReviewed(outcome(p, res.Applied, res.Failed, res.Declined), notReviewed)) // Ruling 1: only an actual step failure makes the run exit 1 // here - a directory the user declined or skipped must not. if res.Failed > 0 { exit = 1 } - return false + return action == 'w' }() if quit { diff --git a/cmd/krino/undo.go b/cmd/krino/undo.go index 036e3c0..5e18a7e 100644 --- a/cmd/krino/undo.go +++ b/cmd/krino/undo.go @@ -343,6 +343,8 @@ func reviewUndoFiles(in io.Reader, out io.Writer, files []engine.UndoFile, p pal return nil, 0, err } switch key { + case '\r', '\n': + continue case 'a': return approveAllUndo(files), 'a', nil case 's': @@ -395,6 +397,8 @@ func reviewUndoPerFile(in io.Reader, out io.Writer, files []engine.UndoFile, p p return nil, false, kerr } switch key { + case '\r', '\n': + continue case 'y': approved[i] = true case 'n': |
