Repository navigation
Expand file tree
/
Copy pathcommandmanager.go
More file actions
273 lines (235 loc) · 6.17 KB
/
Copy pathcommandmanager.go
File metadata and controls
273 lines (235 loc) · 6.17 KB
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
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"mc/shutil"
)
type command interface {
execute() error
undo() error
undoable() bool // false commands never reach the history
String() string
getDir() string
sel() *string // select is a keyword
}
type commandManager struct {
history []command
redoStack []command
}
func newCommandManager() *commandManager {
return &commandManager{
history: make([]command, 0),
redoStack: make([]command, 0),
}
}
func (cm *commandManager) pushHistory(cmd command) {
cm.history = append(cm.history, cmd)
cm.redoStack = cm.redoStack[:0]
}
func (cm *commandManager) canUndo() bool {
return len(cm.history) > 0
}
func (cm *commandManager) canRedo() bool {
return len(cm.redoStack) > 0
}
func (cm *commandManager) peekUndo() (command, error) {
if len(cm.history) == 0 {
return nil, fmt.Errorf("nothing to undo")
}
return cm.history[len(cm.history)-1], nil
}
func (cm *commandManager) commitUndo() {
lastCmd := cm.history[len(cm.history)-1]
cm.history = cm.history[:len(cm.history)-1]
cm.redoStack = append(cm.redoStack, lastCmd)
}
func (cm *commandManager) peekRedo() (command, error) {
if len(cm.redoStack) == 0 {
return nil, fmt.Errorf("nothing to redo")
}
return cm.redoStack[len(cm.redoStack)-1], nil
}
func (cm *commandManager) commitRedo() {
lastCmd := cm.redoStack[len(cm.redoStack)-1]
cm.redoStack = cm.redoStack[:len(cm.redoStack)-1]
cm.history = append(cm.history, lastCmd)
}
type deleteCommand struct {
dir string
paths []string
}
func (c *deleteCommand) String() string {
return fmt.Sprintf("delete %d paths", len(c.paths))
}
func (c *deleteCommand) getDir() string {
return c.dir
}
func (c *deleteCommand) execute() error {
for i := range c.paths {
err := os.RemoveAll(c.paths[i])
if err != nil {
return err
}
}
return nil
}
func (c *deleteCommand) undo() error {
return fmt.Errorf("can't be undone")
}
func (c *deleteCommand) undoable() bool {
return false
}
func (c *deleteCommand) sel() *string {
return nil
}
type pathPair struct {
src string
dst string
}
type fileAction int
const (
copyFileAction fileAction = iota
cutFileAction
renameFileAction
)
// buildRenamePairs maps every new name onto a destination path.
//
// Names that weren't edited are left exactly as they are: such a path already
// exists - as itself - so putting it through UniquePath would turn "a.txt"
// into "a1.txt" and rename a file the user never touched. Windows paths are
// case-insensitive, so a case-only edit is compared the same way, which lets
// "foo.txt" -> "Foo.txt" through as a real rename instead of "Foo1.txt".
func buildRenamePairs(paths []string, names []string) []pathPair {
pairs := make([]pathPair, 0, len(names))
reserved := make([]string, 0, len(names))
for i := range names {
src := paths[i]
dst := filepath.Join(filepath.Dir(src), names[i])
if !strings.EqualFold(dst, src) {
dst = shutil.UniquePath(reserved, paths, dst)
}
reserved = append(reserved, dst)
pairs = append(pairs, pathPair{src, dst})
}
return pairs
}
type fileActionCommand struct {
action fileAction
dir string
pairs []pathPair
collision bool
overwrite bool
overwriteExisting map[string]bool
journal []shutil.Record
redoJournal []shutil.Record
}
func newFileActionCommand(action fileAction, paths []string, dst string, override bool) *fileActionCommand {
var pairs []pathPair
collision := false
existing := map[string]bool{}
var reserved []string
for i := range paths {
name := filepath.Base(paths[i])
dstPath := filepath.Join(dst, name)
if override {
if shutil.PathExists(dstPath) {
collision = true
existing[dstPath] = true
}
pairs = append(pairs, pathPair{paths[i], dstPath})
} else {
path := shutil.UniquePath(reserved, paths, dstPath)
reserved = append(reserved, path)
pairs = append(pairs, pathPair{paths[i], path})
}
}
return &fileActionCommand{action: action, dir: dst, pairs: pairs, collision: collision, overwrite: override, overwriteExisting: existing}
}
func (c *fileActionCommand) String() string {
switch c.action {
case copyFileAction:
return fmt.Sprintf("copy %d items", len(c.pairs))
case cutFileAction:
return fmt.Sprintf("move %d items", len(c.pairs))
case renameFileAction:
return fmt.Sprintf("rename %d items", len(c.pairs))
}
return "unknown command"
}
func (c *fileActionCommand) getDir() string {
return c.dir
}
func (c *fileActionCommand) execute() error { return c.executeTask(context.Background(), nil) }
func (c *fileActionCommand) undo() error {
return shutil.UndoJournal(context.Background(), &c.journal, &c.redoJournal, nil)
}
func (c *fileActionCommand) undoable() bool {
return !c.collision
}
func (c *fileActionCommand) sel() *string {
if len(c.pairs) > 0 {
return &c.pairs[0].dst
}
return nil
}
type createCommand struct {
snapshot map[string]shutil.Stamp
path string
isDir bool
dir string
}
func newCreateCommand(name string, dir string) *createCommand {
isDir := false
runes := []rune(name)
if len(runes) > 0 &&
(runes[len(runes)-1] == '\\' || runes[len(runes)-1] == '/') {
isDir = true
runes = runes[:len(runes)-1]
}
path := shutil.UniquePath(nil, nil, filepath.Join(dir, string(runes)))
return &createCommand{path: path, isDir: isDir, dir: dir}
}
func (c *createCommand) execute() error {
if shutil.PathExists(c.path) {
return fmt.Errorf("path already exists: %s", c.path)
}
if err := shutil.CheckDestination(c.path); err != nil {
return err
}
var err error
if c.isDir {
err = os.MkdirAll(c.path, 0755)
} else {
var f *os.File
f, err = os.OpenFile(c.path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
if err == nil {
err = f.Close()
}
}
if err != nil {
return err
}
c.snapshot, err = shutil.Snapshot(c.path)
return err
}
func (c *createCommand) undo() error {
if err := shutil.Unchanged(c.path, c.snapshot); err != nil {
return err
}
return os.Remove(c.path)
}
func (c *createCommand) undoable() bool {
return true
}
func (c *createCommand) getDir() string {
return c.dir
}
func (c *createCommand) String() string {
return fmt.Sprintf("create %s", c.path)
}
func (c *createCommand) sel() *string {
return &c.path
}