-
Notifications
You must be signed in to change notification settings - Fork 0
/
undo.go
318 lines (266 loc) · 7.43 KB
/
undo.go
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
package main
import (
"bytes"
"compress/flate"
"fmt"
"io"
"slices"
"gopkg.in/yaml.v3"
)
func initializeUndo(b []byte, noGz bool) {
if noGz {
FP.UndoBuffer = [][]byte{b}
} else {
var err error
bgz, err := compress(b)
if err != nil {
FP.ProfileStatusText.SetText(fmt.Sprintf(
"%v%v%v",
FP.Colors["ProfileStatusTextError"],
FP.T["UndoBufferConfigCompressionError"],
Reset,
))
}
FP.UndoBuffer = [][]byte{bgz}
}
FP.UndoBufferPos = 0
}
// Sets the FP.SelectedProfile & config to the value specified by the current
// undo buffer
//
// warning: naively assumes that the FP.UndoBufferPos has already been set to a
// valid value and updates the currently selected config & profile accordingly.
func pushUndoBufferChangeToConfig() {
n := FP.SelectedProfile.Name
b := FP.UndoBuffer[FP.UndoBufferPos]
if !FP.Config.DisableGzipCompressionInUndoBuffer {
var err error
b, err = decompress(b)
if err != nil {
FP.ProfileStatusText.SetText(fmt.Sprintf(
"%v%v%v",
FP.Colors["ProfileStatusTextError"],
FP.T["UndoBufferConfigCompressionError"],
Reset,
))
}
}
err := yaml.Unmarshal(b, &FP.Config)
if err != nil {
FP.ProfileStatusText.SetText(fmt.Sprintf(
"%v%v%v",
FP.Colors["ProfileStatusTextError"],
FP.T["UndoBufferPushValueConfigUnmarshalFailure"],
Reset,
))
}
// set the FP.SelectedProfile to the latest FP.UndoBuffer's config
for i := range FP.Config.Profiles {
if FP.Config.Profiles[i].Name == n {
FP.SelectedProfile = &(FP.Config.Profiles[i])
return
}
}
}
// Moves 1 step backward in the FP.UndoBuffer.
func undo() {
undoBufferLen := len(FP.UndoBuffer)
newUndoBufferPos := FP.UndoBufferPos - 1
if newUndoBufferPos < 0 {
// nothing to undo - at beginning of FP.UndoBuffer
FP.ProfileStatusText.SetText(fmt.Sprintf(
"%v%v [%v/%v]%v",
FP.Colors["ProfileStatusTextPassive"],
FP.T["UndoBufferNothingToUndo"],
FP.UndoBufferPos+1,
undoBufferLen,
Reset,
))
return
}
FP.UndoBufferPos = newUndoBufferPos
pushUndoBufferChangeToConfig()
FP.ProfileStatusText.SetText(fmt.Sprintf(
"%v%v: [%v/%v]%v",
FP.Colors["ProfileStatusTextPassive"],
FP.T["UndoBufferUndoAction"],
FP.UndoBufferPos+1,
undoBufferLen,
Reset,
))
populateProfilesPage()
getTransactionsTable()
FP.TransactionsTable.Select(FP.SelectedProfile.SelectedRow, FP.SelectedProfile.SelectedColumn)
FP.App.SetFocus(FP.TransactionsTable)
}
// Moves 1 step forward in the FP.UndoBuffer.
func redo() {
undoBufferLen := len(FP.UndoBuffer)
undoBufferLastPos := undoBufferLen - 1
newUndoBufferPos := FP.UndoBufferPos + 1
if newUndoBufferPos > undoBufferLastPos {
// nothing to redo - at end of FP.UndoBuffer
FP.ProfileStatusText.SetText(fmt.Sprintf(
"%v%v [%v/%v]%v",
FP.Colors["ProfileStatusTextPassive"],
FP.T["UndoBufferNothingToRedo"],
FP.UndoBufferPos+1,
undoBufferLen,
Reset,
))
return
}
FP.UndoBufferPos = newUndoBufferPos
pushUndoBufferChangeToConfig()
FP.ProfileStatusText.SetText(fmt.Sprintf(
"%v%v: [%v/%v]%v",
FP.Colors["ProfileStatusTextPassive"],
FP.T["UndoBufferRedoAction"],
FP.UndoBufferPos+1,
undoBufferLen,
Reset,
))
populateProfilesPage()
getTransactionsTable()
FP.TransactionsTable.Select(FP.SelectedProfile.SelectedRow, FP.SelectedProfile.SelectedColumn)
FP.App.SetFocus(FP.TransactionsTable)
}
// Uses gzip to compress bytes.
func compress(input []byte) ([]byte, error) {
var b bytes.Buffer
w, err := flate.NewWriter(&b, 9) // TODO: make this 9 value configurable
if err != nil {
return []byte{}, fmt.Errorf("%v: %w", FP.T["UndoBufferCompressionWriteError"], err)
}
_, err = w.Write(input)
if err != nil {
return []byte{}, fmt.Errorf("%v: %w", FP.T["UndoBufferCompressionWriteError"], err)
}
w.Close()
return b.Bytes(), nil
}
// Uses gzip to decompress bytes.
func decompress(input []byte) ([]byte, error) {
var b bytes.Buffer
b.Write(input)
r := flate.NewReader(&b)
defer r.Close()
data, err := io.ReadAll(r)
if err != nil {
return []byte{}, fmt.Errorf("%v: %w", FP.T["UndoBufferCompressionWriteError"], err)
}
return data, nil
}
// attempts to place the current config at FP.UndoBuffer[FP.UndoBufferPos+1] but
// only if there were actual changes.
//
// also updates the status text accordingly
//
// TODO: This needs to be refactored and it needs to have better error handling.
// Specifically, it needs to better alert the user when saving fails in a way
// that is extremely invasive. Currently the small status text cannot show the
// entire error. As for the refactoring - since this function is run very often,
// the fewer operations the better.
func modified() {
if FP.SelectedProfile == nil {
return
}
FP.SelectedProfile.Modified = true
cr, cc := FP.TransactionsTable.GetSelection()
FP.SelectedProfile.SelectedColumn = cc
FP.SelectedProfile.SelectedRow = cr
// marshal to detect differences between this config and the latest
// config in the undo buffer
if len(FP.UndoBuffer) >= 1 {
b, err := yaml.Marshal(FP.Config)
if err != nil {
FP.ProfileStatusText.SetText(fmt.Sprintf(
"%v%v%v",
FP.Colors["ProfileStatusTextError"],
FP.T["UndoBufferCannotMarshalConfigError"],
Reset,
))
}
var bo []byte
if FP.Config.DisableGzipCompressionInUndoBuffer {
bo = FP.UndoBuffer[FP.UndoBufferPos]
} else {
bo, err = decompress(FP.UndoBuffer[FP.UndoBufferPos])
if err != nil {
FP.ProfileStatusText.SetText(fmt.Sprintf(
"%v%v%v",
FP.Colors["ProfileStatusTextError"],
FP.T["UndoBufferConfigDecompressionError"],
Reset,
))
}
}
sbo := string(bo)
sb := string(b)
if sbo == sb {
// no difference between this config and previous one
FP.ProfileStatusText.SetText(fmt.Sprintf(
"%v%v [%v/%v]%v",
FP.Colors["ProfileStatusTextError"],
FP.T["UndoBufferNoChange"],
FP.UndoBufferPos+1,
len(FP.UndoBuffer),
Reset,
))
return
}
}
// if the FP.UndoBufferPos is not at the end of the FP.UndoBuffer, then all
// values after FP.UndoBufferPos need to be deleted
if FP.UndoBufferPos != len(FP.UndoBuffer)-1 {
FP.UndoBuffer = slices.Delete(FP.UndoBuffer, FP.UndoBufferPos, len(FP.UndoBuffer))
}
getTransactionsTable()
// now that we've ensured that we are actually at the end of the buffer,
// proceed to insert this config into the FP.UndoBuffer
b, err := yaml.Marshal(FP.Config)
if err != nil {
FP.ProfileStatusText.SetText(fmt.Sprintf(
"%v%v%v",
FP.Colors["ProfileStatusTextError"],
FP.T["UndoBufferCannotMarshalConfigError"],
Reset,
))
}
var bgz []byte
if FP.Config.DisableGzipCompressionInUndoBuffer {
bgz = b
} else {
// push compressed bytes into the undo buffer to save on RAM :)
bgz, err = compress(b)
if err != nil {
FP.ProfileStatusText.SetText(fmt.Sprintf(
"%v%v%v",
FP.Colors["ProfileStatusTextError"],
FP.T["UndoBufferConfigCompressionError"],
Reset,
))
}
}
FP.UndoBuffer = append(FP.UndoBuffer, bgz)
FP.UndoBufferPos = len(FP.UndoBuffer) - 1
totalUndoBufferSize := 0
for i := range FP.UndoBuffer {
totalUndoBufferSize += len(FP.UndoBuffer[i])
}
// TODO: restrict the length of the buffer based on the configured max
pushUndoBufferChangeToConfig()
FP.ProfileStatusText.SetText(fmt.Sprintf(
"%v%v*%v[%v/%v %vkB]%v",
FP.Colors["ProfileStatusTextModifiedMarker"],
Reset,
FP.Colors["ProfileStatusTextPassive"],
// FP.FlagConfigFile,
FP.UndoBufferPos+1,
len(FP.UndoBuffer),
// float64(len(bgz)/1000),
// float64(len(b)/1000),
float64(totalUndoBufferSize/1000),
Reset,
))
}