diff --git a/docs/guides/tui.md b/docs/guides/tui.md index c9413853..8d9a8ba6 100644 --- a/docs/guides/tui.md +++ b/docs/guides/tui.md @@ -2451,10 +2451,11 @@ Inside the form: | Key | Does | |---|---| | `↑` / `↓` | Move between rows | -| `enter` | Edit the row, cycle its values, or descend into a nested body | +| `enter` | Edit the row, open its values, or descend into a nested body | | `t` | In an open list, type a value it does not offer | | `a` | Add a step, lane or declared field after the one under the cursor | -| `d` | Remove the step, lane or declared field under the cursor — asks first | +| `d` | Remove the step, lane or declared field under the cursor — asks first, and only `y` removes | +| `u` | Undo the last write: put back the value it replaced | | `K` / `J` | Move it up or down. Capitals: `k` and `j` still move the cursor | | `ctrl+s` | Save, in the multi-line pane and the key/value sub-form below — the two places `enter` does something else | | `R` | Re-read the file — the reload a refused save offers | @@ -2464,7 +2465,8 @@ There is no save key for a row, because there is nothing to save: committing a row with `enter` **is** the write, and so is answering `a`, `d` or `K`/`J`. Each one becomes a single edit operation carrying the version the last read handed back, and a value the daemon refuses stays on screen beside its error rather -than being reverted under you. +than being reverted under you. After a write the form says what the row was and +that `u` puts it back; the undo is itself a write, and is not undoable. `a` on a list of steps asks which type first, offering only the types that are legal where the cursor is, and writes a skeleton with that type's required @@ -2478,7 +2480,8 @@ Some rows open something bigger than a text field: | `prompt:`, `run:`, `instructions:` | A full-pane multi-line editor. `enter` inserts a newline, `ctrl+s` saves it back as a `\|` block scalar, `esc` abandons it. Opening one and closing it again writes nothing | | `agent:`, `model:`, `effort:` | The same filterable picker the new-task form uses, listing the agents the daemon detected and the selected agent's own catalog | | `env:`, a lane's `fields:` | A key/value sub-form: `enter` edits or adds a key, `d` drops one, `ctrl+s` writes the mapping | -| An enum, a boolean | Nothing — `enter` cycles it in place | +| An enum | Its members as a list, `(unset)` too when the field is optional — the pick is the one write | +| A boolean | Nothing — `enter` flips it in place | A whole-number row and a duration row are checked before the write, so `timeout: 2 minutes` is refused on the row that typed it rather than after a diff --git a/internal/keymap/fixed.go b/internal/keymap/fixed.go index d4998610..3e890321 100644 --- a/internal/keymap/fixed.go +++ b/internal/keymap/fixed.go @@ -264,7 +264,8 @@ var fixed = []Fixed{ {"workflows", "f", "fork the entry into another scope, where it shadows the original"}, {"workflow editor", "up", "move between rows"}, {"workflow editor", "down", "move between rows"}, - {"workflow editor", "enter", "edit the row, cycle its values, or descend into a nested body"}, + {"workflow editor", "enter", "edit the row, open its values, or descend into a nested body"}, + {"workflow editor", "u", "undo the last write: put back the value it replaced"}, {"workflow editor", "esc", "leave the nested body, then the editor"}, {"workflow editor", "K", "move the item up"}, {"workflow editor", "J", "move the item up"}, diff --git a/internal/tui/bindings.go b/internal/tui/bindings.go index d455ac77..f3874028 100644 --- a/internal/tui/bindings.go +++ b/internal/tui/bindings.go @@ -1006,7 +1006,7 @@ var bindings = []binding{ // The structured editor sub-layer. {key: "up/down", label: "move between rows", scope: scopePanel, context: ctxWorkflowEditor, hint: "↑↓ rows", priority: 1}, - {key: "enter", label: "edit the row, cycle its values, or descend into a nested body", scope: scopePanel, context: ctxWorkflowEditor, hint: "enter edit", priority: 2}, + {key: "enter", label: "edit the row, open its values, or descend into a nested body", scope: scopePanel, context: ctxWorkflowEditor, hint: "enter edit", priority: 2}, {op: keymap.Refresh, key: "R", label: "re-read the file (what a stale-write 409 offers)", scope: scopePanel, context: ctxWorkflowEditor, hint: "R reload", priority: 3, term: termRefresh}, {key: "esc", label: "leave the nested body, then the editor", scope: scopePanel, context: ctxWorkflowEditor, hint: "esc back", priority: 4}, // The structural keys (issue #320). They sit behind the takeover, so they @@ -1016,6 +1016,8 @@ var bindings = []binding{ {op: keymap.Add, key: "a", label: "add a step, lane or declared field after the one under the cursor", scope: scopePanel, context: ctxWorkflowEditor, hint: "a add", priority: 5, term: termAdd}, {op: keymap.DraftRemove, key: "d", label: "remove the item under the cursor (asks first)", scope: scopePanel, context: ctxWorkflowEditor, hint: "d remove", priority: 6, term: termDraftDrop}, {key: "K/J", label: "move the item up (K) or down (J); k and j still move the cursor", scope: scopePanel, context: ctxWorkflowEditor, hint: "K/J move", priority: 7}, + // #1056: the last write, taken back — the value it replaced, written. + {key: "u", label: "undo the last write: put back the value it replaced", scope: scopePanel, context: ctxWorkflowEditor, hint: "u undo", priority: 7}, {key: "ctrl+s", label: "in the multi-line pane: save (enter inserts a newline there)", scope: scopePanel, context: ctxWorkflowEditor, hint: "ctrl+s save", priority: 8}, {op: keymap.FreeText, key: "t", label: "in an open list: type a value it does not offer", scope: scopePanel, context: ctxWorkflowEditor, priority: 9, term: termFreeText}, diff --git a/internal/tui/bindings_test.go b/internal/tui/bindings_test.go index 6e5f591a..fc770171 100644 --- a/internal/tui/bindings_test.go +++ b/internal/tui/bindings_test.go @@ -2450,6 +2450,12 @@ var panelKeyProbes = map[bindingContext]map[string]func(*testing.T){ }, ctxWorkflowEditor: { + "u": func(t *testing.T) { + w := editorFixture(t) + if _, cmd := w.updateKey(registryKey(t, "u")); cmd != nil || w.editor.err != "nothing to undo" { + t.Fatalf("u with nothing written: err %q", w.editor.err) + } + }, "up/down": func(t *testing.T) { w := editorFixture(t) w.updateKey(registryKey(t, "down")) diff --git a/internal/tui/workfloweditor.go b/internal/tui/workfloweditor.go index 807daba0..8feb333a 100644 --- a/internal/tui/workfloweditor.go +++ b/internal/tui/workfloweditor.go @@ -117,6 +117,17 @@ type wfEditorLayer struct { // stale is a 409: the file changed on disk. The pending edit stays on // screen and `R` re-reads it, which is the offer the brief asks for. stale bool + // undo is the last write's row and the value it replaced, which `u` puts + // back (#1056); pending is the write in flight, promoted to undo when the + // daemon accepts it. One level: an undo is itself not undoable. + undo, pending *wfUndo +} + +// wfUndo is one write the editor can take back: the row as it was, whose +// value is what the write replaced. +type wfUndo struct { + row wfEditRow + undoing bool } // openEditor opens the structured editor on the entry under the cursor. diff --git a/internal/tui/workfloweditorkeys.go b/internal/tui/workfloweditorkeys.go index 5c2a638f..020ac4a7 100644 --- a/internal/tui/workfloweditorkeys.go +++ b/internal/tui/workfloweditorkeys.go @@ -55,6 +55,8 @@ func (w *workflowsView) updateEditorKey(msg tea.KeyPressMsg) (panel, tea.Cmd) { // so nothing is sent from here. w.editorRemove() return w, nil + case "u": + return w, w.editorUndo() case "K": return w, w.editorMove(-1) case "J": @@ -110,10 +112,14 @@ func (w *workflowsView) editorActivate() tea.Cmd { } switch row.field.Control { case apiclient.WorkflowControlEnum: - // An enum row cycles rather than opening a text field (task 058's - // enum rows): the members are known, so there is nothing to type and - // nothing to get wrong. - return w.commitRow(row, cycleEnum(row.field, row.value)) + // An enum row opens its members as a list and writes once, on the + // pick (#1056): cycling wrote every value on the way to the one + // wanted, possibly into a global workflow. There is nothing to type, + // so the list takes no free text. + e.editing = e.cursor + e.overlay = newWFEditorPicker(row.field.Name, row.field.Control, row.value, + enumOptions(row.field), false, func(v string) tea.Cmd { return w.commitRow(row, v) }) + return nil case apiclient.WorkflowControlBool: next := "true" if row.value == "true" { @@ -150,6 +156,19 @@ func (w *workflowsView) editorActivate() tea.Cmd { // cycleEnum steps through the members and then past the end to (unset), // which an optional row needs and a required one does not get. +// enumOptions are an enum field's members as picker options, with `(unset)` +// when the field is optional. +func enumOptions(f apiclient.WorkflowSchemaField) []pickerOption { + out := make([]pickerOption, 0, len(f.Values)+1) + for _, v := range f.Values { + out = append(out, pickerOption{value: v, label: v}) + } + if !f.Required { + out = append(out, pickerOption{value: unsetMarker, label: unsetMarker}) + } + return out +} + func cycleEnum(f apiclient.WorkflowSchemaField, value string) string { values := f.Values if !f.Required { @@ -203,9 +222,38 @@ func (w *workflowsView) commitRow(row wfEditRow, value string) tea.Cmd { return nil } e.saving = true + e.pending = &wfUndo{row: row} return w.editorPatchCmd([]apiclient.WorkflowOp{op}) } +// editorUndo is `u`: the last accepted write's row back to the value it +// replaced (#1056), written the way any edit is. Nothing to undo, or a write +// still in flight, says so. +func (w *workflowsView) editorUndo() tea.Cmd { + e := w.editor + switch { + case e.saving: + e.err = "a write is still in flight" + return nil + case e.undo == nil: + e.err = "nothing to undo" + return nil + } + u := e.undo + for i, r := range e.rows { + if r.path == u.row.path { + e.cursor = i + cmd := w.commitRow(r, u.row.value) + if e.pending != nil { + e.pending.undoing = true + } + return cmd + } + } + e.err = "the row the last write changed is not on screen — esc up to it, or reload" + return nil +} + // rowOp is the one translation from a committed row to the edit operation // that writes it, shared by every form that edits a daemon-owned YAML file by // ops — this editor and the triggers form (task 096.6) — so the two cannot @@ -350,6 +398,7 @@ func (w *workflowsView) updateEditorMsg(msg tea.Msg) (panel, tea.Cmd, bool) { case wfEditorSavedMsg: e.saving = false if msg.err != nil { + e.pending = nil var apiErr *apiclient.Error if errors.As(msg.err, &apiErr) && apiErr.Status == 409 { e.stale = true @@ -362,6 +411,13 @@ func (w *workflowsView) updateEditorMsg(msg tea.Msg) (panel, tea.Cmd, bool) { } e.err, e.stale = "", false e.version = msg.result.Version + // The accepted write is what `u` takes back; an undo clears it. + if p := e.pending; p != nil && !p.undoing { + e.undo = p + } else { + e.undo = nil + } + e.pending = nil // The registry reload the write triggered brings the new definition // back through the same path an external edit takes. return w, w.editorLoadCmd(e.key), true diff --git a/internal/tui/workfloweditorpicker_test.go b/internal/tui/workfloweditorpicker_test.go index 00ea4107..d984abc2 100644 --- a/internal/tui/workfloweditorpicker_test.go +++ b/internal/tui/workfloweditorpicker_test.go @@ -176,17 +176,21 @@ func TestEditorHandOffRowCyclesTheClosedSet(t *testing.T) { t.Errorf("%s: wrote %q", tc.value, op.Value) } } - // enter on the row commits the next member rather than opening a field. + // enter on the row opens its members and writes nothing (#1056); the + // pick is the one write. w.editor.cursor = editorRowIndex(t, w, "hand_off") - _, cmd := w.updateKey(registryKey(t, "enter")) - if cmd == nil { - t.Error("enter on hand_off did not commit") + if _, cmd := w.updateKey(registryKey(t, "enter")); cmd != nil || w.editor.overlay == nil { + t.Fatal("enter on hand_off wrote, or opened no list") } - if got := w.editor.rows[w.editor.cursor].value; got != "auto" { - t.Errorf("hand_off row after enter = %q, want auto", got) + w.updateKey(registryKey(t, "down")) + if _, cmd := w.updateKey(registryKey(t, "enter")); cmd == nil { + t.Error("the pick did not commit") } - if w.editor.input != nil || w.editor.overlay != nil { - t.Error("an enum row opened a field or an overlay") + if got := w.editor.rows[w.editor.cursor].value; got != "explicit" { + t.Errorf("hand_off row after the pick = %q, want explicit", got) + } + if w.editor.overlay != nil { + t.Error("the list stayed open after the pick") } } @@ -205,10 +209,10 @@ func TestEditorHandOffRowReadsTheFile(t *testing.T) { if next := cycleEnum(row.field, row.value); next != unsetMarker { t.Errorf("next after explicit = %q, want unset", next) } - if _, cmd := w.updateKey(registryKey(t, "enter")); cmd == nil { - t.Error("enter on hand_off did not commit") + if _, cmd := w.updateKey(registryKey(t, "enter")); cmd != nil { + t.Error("enter on hand_off wrote before a pick") } - if got := w.editor.rows[w.editor.cursor].value; got == "auto" { - t.Errorf("enter on explicit wrote auto") + if got := w.editor.rows[w.editor.cursor].value; got != "explicit" { + t.Errorf("enter on explicit changed the row to %q", got) } } diff --git a/internal/tui/workfloweditorrender.go b/internal/tui/workfloweditorrender.go index 7b43f7db..6369b23e 100644 --- a/internal/tui/workfloweditorrender.go +++ b/internal/tui/workfloweditorrender.go @@ -35,6 +35,14 @@ func (w *workflowsView) renderEditor(width, height int) string { var foot []string if e.saving { foot = append(foot, "", styleDim.Render(" saving…")) + } else if u := e.undo; u != nil && e.err == "" { + // What the last write replaced, and the key that puts it back (#1056). + was := u.row.value + if was == "" { + was = unsetMarker + } + line := "saved " + u.row.field.Name + " · it was " + oneLine(was) + " · u puts it back" + foot = append(foot, "", styleDim.Render(" "+ansi.Truncate(line, max(width-2, 1), "…"))) } if e.err != "" { // The refused value is still on its row above; the error names the @@ -55,7 +63,7 @@ func (w *workflowsView) renderEditor(width, height int) string { foot = append(foot, "", styleDim.Render(" enter commit · esc cancel")) default: foot = append(foot, "", styleDim.Render( - " enter edit · "+opKey(keymap.Add)+" add · "+opKey(keymap.DraftRemove)+" remove · K/J move · "+ + " enter edit · "+opKey(keymap.Add)+" add · "+opKey(keymap.DraftRemove)+" remove · K/J move · u undo · "+ opKey(keymap.Refresh)+" reload · esc back · "+opKey(keymap.Editor)+" $EDITOR from the list")) } foot = truncateRows(foot, max(width, 1)) diff --git a/internal/tui/workfloweditorstructure.go b/internal/tui/workfloweditorstructure.go index fd99744c..387f32cd 100644 --- a/internal/tui/workfloweditorstructure.go +++ b/internal/tui/workfloweditorstructure.go @@ -42,7 +42,9 @@ func newWFEditorConfirm(prompt string, act func() tea.Cmd) *wfEditorConfirm { func (o *wfEditorConfirm) Update(msg tea.KeyPressMsg) (wfEditorOverlay, tea.Cmd) { switch msg.String() { - case "y", "enter": + case "y", "Y": + // y only (#1056): enter is the editor's edit key, so `d` then a + // reflexive enter would have removed the item. return nil, o.act() default: // Anything else is "no". A confirmation whose only refusal was one diff --git a/internal/tui/workfloweditorundo_test.go b/internal/tui/workfloweditorundo_test.go new file mode 100644 index 00000000..5c2ca6e0 --- /dev/null +++ b/internal/tui/workfloweditorundo_test.go @@ -0,0 +1,64 @@ +package tui + +import ( + "strings" + "testing" + + "github.com/charmbracelet/x/ansi" + + "github.com/lezli01/vincent/internal/apiclient" +) + +// #1056: an accepted write can be taken back with u, which writes the value +// it replaced; the screen says what that value was. +func TestEditorUndoPutsTheValueBack(t *testing.T) { + w := editorFixture(t) + e := w.editor + i := editorRowIndex(t, w, "description") + e.cursor = i + was := e.rows[i].value + if cmd := w.commitRow(e.rows[i], "a new description"); cmd == nil || e.pending == nil { + t.Fatal("the write was not sent") + } + w.updateEditorMsg(wfEditorSavedMsg{result: apiclient.WorkflowWriteResult{Version: "v2"}}) + if e.undo == nil || e.undo.row.value != was { + t.Fatalf("undo = %+v, want the replaced value %q", e.undo, was) + } + if got := ansi.Strip(w.render(100, 40)); !strings.Contains(got, "u puts it back") { + t.Errorf("the screen does not offer the undo:\n%s", got) + } + e.cursor = 0 + if _, cmd := w.updateKey(registryKey(t, "u")); cmd == nil { + t.Fatal("u sent nothing") + } + if e.cursor != i || e.rows[i].value != was { + t.Fatalf("u wrote %q at row %d, want %q at %d", e.rows[i].value, e.cursor, was, i) + } + w.updateEditorMsg(wfEditorSavedMsg{result: apiclient.WorkflowWriteResult{Version: "v3"}}) + if e.undo != nil { + t.Fatal("an undo is itself undoable") + } +} + +// #1056: the remove confirmation takes y only — enter, the editor's edit +// key, cancels it. +func TestEditorRemoveTakesYOnly(t *testing.T) { + w := editorFixture(t) + for i, r := range w.editor.rows { + if r.list != "" { + w.editor.cursor = i + break + } + } + w.updateKey(registryKey(t, "d")) + if w.editor.overlay == nil { + t.Fatal("d asked nothing") + } + if _, cmd := w.updateKey(registryKey(t, "enter")); cmd != nil || w.editor.saving { + t.Fatal("enter removed the item") + } + w.updateKey(registryKey(t, "d")) + if _, cmd := w.updateKey(registryKey(t, "y")); cmd == nil { + t.Fatal("y did not remove the item") + } +}