aboutsummaryrefslogtreecommitdiff
path: root/gui/internal/model/history.go
blob: 7c166bb71ccc0001f2fdfc0c8d88bff70326ce91 (plain) (blame)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package model

import (
	"context"
	"fmt"
	"sort"
	"strings"
	"time"

	"git.labunix.xyz/krino/internal/engine"
	"git.labunix.xyz/krino/internal/journal"
	"git.labunix.xyz/krino/internal/lock"
)

// Run is one line of the History tab's run list: what a run did, and
// whether it has since been reversed (GUI design §4).
type Run struct {
	ID      string
	Start   time.Time
	Dirs    []string
	Summary string // "3 moved · 1 trashed", or "nothing applied"
	Note    string // "(undone)", "(partly undone)", or ""
	UndoOf  string // for an undo run, the run it reverses
}

// Runs lists the most recent runs, newest first. It reads the log only.
func Runs(e *engine.Engine, n int) ([]Run, error) {
	rs, err := journal.Runs(e.Config.LogFile(), n)
	if err != nil {
		return nil, err
	}
	out := make([]Run, 0, len(rs))
	for _, r := range rs {
		out = append(out, Run{
			ID:      r.ID,
			Start:   r.Start,
			Dirs:    r.Dirs,
			Summary: countsText(r.Counts),
			Note:    note(r),
			UndoOf:  r.UndoOf,
		})
	}
	return out, nil
}

// note is what the list says about a run that has been reversed.
func note(r journal.Run) string {
	switch {
	case r.PartlyUndone:
		return "(partly undone)"
	case r.Undone:
		return "(undone)"
	}
	return ""
}

// pastTense and countOrder are krino log's own display words and order
// (cmd/krino/log.go): the log's action names are the wire format and are
// never renamed, so both front ends translate them the same way.
var pastTense = map[string]string{
	"copy": "copied", "move": "moved", "rename": "renamed",
	"trash": "trashed", "delete": "deleted", "displace": "displaced",
	"undo-copy": "undo-copied", "undo-move": "undo-moved",
	"undo-rename": "undo-renamed", "undo-trash": "undo-trashed",
	"undo-displace": "undo-displaced",
}

var countOrder = []string{
	"copy", "move", "rename", "trash", "delete", "displace",
	"undo-copy", "undo-move", "undo-rename", "undo-trash", "undo-displace",
}

// countsText renders a run's counts in a fixed order, skipping actions with
// no successful entries.
func countsText(counts map[string]int) string {
	var parts []string
	for _, action := range countOrder {
		if n := counts[action]; n > 0 {
			parts = append(parts, fmt.Sprintf("%d %s", n, pastTense[action]))
		}
	}
	if len(parts) == 0 {
		return "nothing applied"
	}
	return strings.Join(parts, " · ")
}

// UndoRow is one file of a run's reversal.
type UndoRow struct {
	File    string // the Rel the original run logged
	Dir     string
	Steps   []engine.UndoStep
	Refused string // non-empty: nothing here is reversed, and why
	// Selected is the checkbox. A refused file has none.
	Selected bool
	Actable  bool
	Outcome  string // "", "done", "failed: ...", "declined"
}

// UndoCounts is the header "N files · N to reverse · N refused".
type UndoCounts struct {
	Files, ToReverse, Refused int
}

// UndoTab is one run's reversal, ready to show and approve.
type UndoTab struct {
	Run     string
	Rows    []UndoRow
	Counts  UndoCounts
	Applied bool

	sess  *engine.Session
	up    *engine.UndoPlan
	locks []*lock.Lock
}

// PlanUndo builds the reversal of runID and locks every directory it
// touches, in name order, so nothing moves under the user while they
// choose; a directory another krino holds stops the whole undo, named
// (spec §10, GUI design §4). Nothing is touched until Apply.
//
// Like a plan, an undo gets its own session: reversing a run is a run of
// its own, logged under a new id that says which run it undoes.
func PlanUndo(ctx context.Context, e *engine.Engine, runID string) (*UndoTab, error) {
	sess, err := e.NewSession(false)
	if err != nil {
		return nil, err
	}
	up, err := sess.PlanUndo(runID)
	if err != nil {
		sess.Close()
		return nil, err
	}
	locks, err := sess.LockDirs(ctx, undoDirNames(up.Files), false)
	if err != nil {
		sess.Close()
		return nil, err
	}
	t := &UndoTab{Run: up.Run, sess: sess, up: up, locks: locks}
	t.fill()
	return t, nil
}

// fill turns the engine's undo plan into rows and counts.
func (t *UndoTab) fill() {
	t.Rows = nil
	t.Counts = UndoCounts{Files: len(t.up.Files)}
	for _, f := range t.up.Files {
		row := UndoRow{File: f.File, Dir: f.Dir, Steps: f.Steps, Refused: f.Refused}
		row.Actable = f.Refused == ""
		row.Selected = row.Actable
		if row.Actable {
			t.Counts.ToReverse++
		} else {
			t.Counts.Refused++
		}
		t.Rows = append(t.Rows, row)
	}
}

// undoDirNames is every directory the plan touches, once, in name order.
func undoDirNames(files []engine.UndoFile) []string {
	seen := map[string]bool{}
	var out []string
	for _, f := range files {
		if f.Dir != "" && !seen[f.Dir] {
			seen[f.Dir] = true
			out = append(out, f.Dir)
		}
	}
	sort.Strings(out)
	return out
}

// SelectAll checks every file that can be reversed; SelectNone clears them.
func (t *UndoTab) SelectAll()  { t.setAll(true) }
func (t *UndoTab) SelectNone() { t.setAll(false) }

func (t *UndoTab) setAll(on bool) {
	for i := range t.Rows {
		t.Rows[i].Selected = on && t.Rows[i].Actable
	}
}

// Toggle flips row i's checkbox; a refused row stays off.
func (t *UndoTab) Toggle(i int) {
	if i < 0 || i >= len(t.Rows) || !t.Rows[i].Actable {
		return
	}
	t.Rows[i].Selected = !t.Rows[i].Selected
}

// SelectedCount is how many files Undo would reverse.
func (t *UndoTab) SelectedCount() int {
	n := 0
	for _, r := range t.Rows {
		if r.Selected {
			n++
		}
	}
	return n
}

// Apply reverses the checked files. An unchecked one is not dropped: it is
// logged as declined, exactly as the terminal review logs a file the user
// said no to (spec §9). Refused files ride along unchanged, as they do on
// the command line. The locks are released afterwards: the plan is history.
func (t *UndoTab) Apply(ctx context.Context) (*engine.ApplyResult, error) {
	toApply := &engine.UndoPlan{Run: t.up.Run, Cleanup: t.up.Cleanup}
	for i, f := range t.up.Files {
		if f.Refused == "" && !t.Rows[i].Selected {
			f.Declined = true
		}
		toApply.Files = append(toApply.Files, f)
	}
	res, err := t.sess.ApplyUndo(ctx, toApply)
	t.Applied = true
	t.Close()
	if res != nil {
		t.record(res)
	}
	return res, err
}

// record writes each file's outcome onto its row, matching on the file name
// the undo result carries.
func (t *UndoTab) record(res *engine.ApplyResult) {
	rows := map[string]int{}
	for i, r := range t.Rows {
		rows[r.File] = i
	}
	for _, fr := range res.Files {
		if i, ok := rows[fr.File.Rel]; ok {
			t.Rows[i].Outcome = undoOutcome(t.Rows[i], fr)
		}
	}
}

// undoOutcome is what happened to one file. Removing a directory the
// original run made is tidiness, not a restoration - it fails whenever
// something else still lives there, a declined file of this very undo
// included - so a failed undo-mkdir does not make the file itself a
// failure, exactly as the engine's own counts treat it.
func undoOutcome(row UndoRow, fr engine.FileResult) string {
	out := "done"
	for i, sr := range fr.Steps {
		action := ""
		if i < len(row.Steps) {
			action = row.Steps[i].Action
		}
		switch sr.Status {
		case "failed":
			if action != "undo-mkdir" {
				return "failed: " + sr.Detail
			}
		case "declined":
			out = "declined"
		}
	}
	return out
}

// Close releases the directories' locks and ends the run, which Apply also
// does. One lock's failure never stops the rest from being released, or the
// session from being closed. Closing twice is not an error.
func (t *UndoTab) Close() error {
	var first error
	for _, l := range t.locks {
		if err := l.Release(); err != nil && first == nil {
			first = err
		}
	}
	t.locks = nil
	if err := t.sess.Close(); err != nil && first == nil {
		first = err
	}
	return first
}