-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
117 lines (103 loc) · 2.47 KB
/
file.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
package main
import (
"fmt"
"io/fs"
"strconv"
)
// File represents a file within the target file system
type File struct {
Depth int
Index int
Name string
NewName string
Parent string
NewParent string
IsDir bool
Result Match
DescendantsMatched int
DescendantsNotMatched int
DescendantActions int
Contents []File
}
// NewFile returns a file with it static properties set.
func NewFile(patterns []Pattern, depth int, index int, entry fs.DirEntry, oldParent, newParent string) File {
name := entry.Name()
result, newName := ApplyPattern(patterns, depth, name)
return File{
Depth: depth,
Index: index,
Name: name,
NewName: newName,
Result: result,
Parent: oldParent,
NewParent: newParent,
IsDir: entry.IsDir(),
}
}
// Actionable returns true if action is needed for f.
func (f File) Actionable() bool {
return f.NewName != f.Name
}
// VerboseString returns a more verbose string representation of f.
func (f File) VerboseString() string {
return fmt.Sprintf("%6s [%s] [%s] %s", strconv.Itoa(f.Index)+":", f.kind(), f.result(), f.name())
}
// String returns a string representation of f.
func (f File) String() string {
return fmt.Sprintf("[%s] %s", f.kindShort(), f.name())
}
func (f File) name() string {
if f.NewName == "" {
return f.Name
}
if f.NewName != f.Name {
return fmt.Sprintf("%s → %s", f.Name, f.NewName)
}
return f.Name
}
func (f File) kind() string {
if f.IsDir {
return "DIR"
}
return "FILE"
}
func (f File) kindShort() string {
if f.IsDir {
return "D"
}
return "F"
}
func (f File) result() string {
switch f.Result {
case Matched:
return "*"
case NotMatched:
return " "
default:
return "~"
}
}
// FileIter iterates through a set of files.
type FileIter struct {
files []File
index int
}
// NewFileIter returns a new file iterator for files.
func NewFileIter(files []File) *FileIter {
return &FileIter{files, 0}
}
// Next returns the index and a pointer to the next file.
//
// If the iterator has reached the end of the series, it returns -1 and nil.
func (iter *FileIter) Next() (int, *File) {
if iter.Done() {
return -1, nil
}
i, file := iter.index, &iter.files[iter.index]
iter.index++
return i, file
}
// Done returns true if the iterator has reached the end of the series.
func (iter *FileIter) Done() bool {
return iter.index >= len(iter.files)
}