-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.go
190 lines (177 loc) · 4.6 KB
/
find.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
// Copyright (c) 2023, The Goki Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package filetree
import (
"fmt"
"path/filepath"
"sort"
"strings"
"time"
"goki.dev/fi"
"goki.dev/gi/v2/gi"
"goki.dev/ki/v2"
)
// FindDirNode finds directory node by given path.
// Must be a relative path already rooted at tree, or absolute path within tree.
func (fn *Node) FindDirNode(path string) (*Node, error) {
rp := fn.RelPath(gi.FileName(path))
if rp == "" {
return nil, fmt.Errorf("FindDirNode: path: %s is not relative to this node's path: %s", path, fn.FPath)
}
if rp == "." {
return fn, nil
}
dirs := filepath.SplitList(rp)
dir := dirs[0]
dni := fn.ChildByName(dir, 0)
if dni == nil {
return nil, fmt.Errorf("could not find child %q", dir)
}
dn := AsNode(dni)
if len(dirs) == 1 {
if dn.IsDir() {
return dn, nil
}
return nil, fmt.Errorf("FindDirNode: item at path: %s is not a Directory", path)
}
return dn.FindDirNode(filepath.Join(dirs[1:]...))
}
// FindFile finds first node representing given file (false if not found) --
// looks for full path names that have the given string as their suffix, so
// you can include as much of the path (including whole thing) as is relevant
// to disambiguate. See FilesMatching for a list of files that match a given
// string.
func (fn *Node) FindFile(fnm string) (*Node, bool) {
if fnm == "" {
return nil, false
}
fneff := fnm
if len(fneff) > 2 && fneff[:2] == ".." { // relative path -- get rid of it and just look for relative part
dirs := strings.Split(fneff, string(filepath.Separator))
for i, dr := range dirs {
if dr != ".." {
fneff = filepath.Join(dirs[i:]...)
break
}
}
}
if efn, err := fn.FRoot.ExtNodeByPath(fnm); err == nil {
return efn, true
}
if strings.HasPrefix(fneff, string(fn.FPath)) { // full path
ffn, err := fn.DirsTo(fneff)
if err == nil {
return ffn, true
}
return nil, false
}
var ffn *Node
found := false
fn.WidgetWalkPre(func(wi gi.Widget, wb *gi.WidgetBase) bool {
sfn := AsNode(wi)
if sfn == nil {
return ki.Continue
}
if strings.HasSuffix(string(sfn.FPath), fneff) {
ffn = sfn
found = true
return ki.Break
}
return ki.Continue
})
return ffn, found
}
// FilesMatching returns list of all nodes whose file name contains given
// string (no regexp). ignoreCase transforms everything into lowercase
func (fn *Node) FilesMatching(match string, ignoreCase bool) []*Node {
mls := make([]*Node, 0)
if ignoreCase {
match = strings.ToLower(match)
}
fn.WidgetWalkPre(func(wi gi.Widget, wb *gi.WidgetBase) bool {
sfn := AsNode(wi)
if sfn == nil {
return ki.Continue
}
if ignoreCase {
nm := strings.ToLower(sfn.Nm)
if strings.Contains(nm, match) {
mls = append(mls, sfn)
}
} else {
if strings.Contains(sfn.Nm, match) {
mls = append(mls, sfn)
}
}
return ki.Continue
})
return mls
}
// NodeNameCount is used to report counts of different string-based things
// in the file tree
type NodeNameCount struct {
Name string
Count int
}
func NodeNameCountSort(ecs []NodeNameCount) {
sort.Slice(ecs, func(i, j int) bool {
return ecs[i].Count > ecs[j].Count
})
}
// FileExtCounts returns a count of all the different file extensions, sorted
// from highest to lowest.
// If cat is != fi.Unknown then it only uses files of that type
// (e.g., fi.Code to find any code files)
func (fn *Node) FileExtCounts(cat fi.Cat) []NodeNameCount {
cmap := make(map[string]int, 20)
fn.WidgetWalkPre(func(wi gi.Widget, wb *gi.WidgetBase) bool {
sfn := AsNode(wi)
if sfn == nil {
return ki.Continue
}
if cat != fi.UnknownCat {
if sfn.Info.Cat != cat {
return ki.Continue
}
}
ext := strings.ToLower(filepath.Ext(sfn.Nm))
if ec, has := cmap[ext]; has {
cmap[ext] = ec + 1
} else {
cmap[ext] = 1
}
return ki.Continue
})
ecs := make([]NodeNameCount, len(cmap))
idx := 0
for key, val := range cmap {
ecs[idx] = NodeNameCount{Name: key, Count: val}
idx++
}
NodeNameCountSort(ecs)
return ecs
}
// LatestFileMod returns the most recent mod time of files in the tree.
// If cat is != fi.Unknown then it only uses files of that type
// (e.g., fi.Code to find any code files)
func (fn *Node) LatestFileMod(cat fi.Cat) time.Time {
tmod := time.Time{}
fn.WidgetWalkPre(func(wi gi.Widget, wb *gi.WidgetBase) bool {
sfn := AsNode(wi)
if sfn == nil {
return ki.Continue
}
if cat != fi.UnknownCat {
if sfn.Info.Cat != cat {
return ki.Continue
}
}
ft := (time.Time)(sfn.Info.ModTime)
if ft.After(tmod) {
tmod = ft
}
return ki.Continue
})
return tmod
}