/
filetree.go
643 lines (579 loc) · 17.3 KB
/
filetree.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
// Copyright (c) 2018, 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 giv
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"mime"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"github.com/goki/gi"
"github.com/goki/ki"
"github.com/goki/ki/bitflag"
"github.com/goki/ki/kit"
)
// FileTree is the root of a tree representing files in a given directory (and
// subdirectories thereof), and has some overall management state for how to
// view things. The FileTree can be viewed by a TreeView to provide a GUI
// interface into it.
type FileTree struct {
FileNode
OpenDirs OpenDirMap `desc:"records which directories within the tree (encoded using paths relative to root) are open (i.e., have been opened by the user) -- can persist this to restore prior view of a tree"`
DirsOnTop bool `desc:"if true, then all directories are placed at the top of the tree view -- otherwise everything is alpha sorted"`
}
var KiT_FileTree = kit.Types.AddType(&FileTree{}, FileTreeProps)
var FileTreeProps = ki.Props{}
// OpenPath opens a filetree at given directory path -- reads all the files at
// given path into this tree -- uses config children to preserve extra info
// already stored about files. Only paths listed in OpenDirs will be opened.
func (ft *FileTree) OpenPath(path string) {
ft.FRoot = ft // we are our own root..
ft.Kind = "Folder"
ft.OpenDirs.ClearFlags()
ft.ReadDir(path)
}
// UpdateNewFile should be called with path to a new file that has just been
// created -- will update view to show that file.
func (ft *FileTree) UpdateNewFile(filename gi.FileName) {
fpath, _ := filepath.Split(string(filename))
fpath = filepath.Clean(fpath)
if fn, ok := ft.FindFile(string(filename)); ok {
fn.UpdateNode()
} else if fn, ok := ft.FindFile(fpath); ok {
fn.UpdateNode()
}
}
// RelPath returns the relative path from root for given full path
func (ft *FileTree) RelPath(fpath gi.FileName) string {
rpath, err := filepath.Rel(string(ft.FPath), string(fpath))
if err != nil {
log.Printf("giv.FileTree RelPath error: %v\n", err.Error())
}
return rpath
}
// IsDirOpen returns true if given directory path is open (i.e., has been
// opened in the view)
func (ft *FileTree) IsDirOpen(fpath gi.FileName) bool {
return ft.OpenDirs.IsOpen(ft.RelPath(fpath))
}
// SetDirOpen sets the given directory path to be open
func (ft *FileTree) SetDirOpen(fpath gi.FileName) {
ft.OpenDirs.SetOpen(ft.RelPath(fpath))
}
// SetDirClosed sets the given directory path to be closed
func (ft *FileTree) SetDirClosed(fpath gi.FileName) {
ft.OpenDirs.SetClosed(ft.RelPath(fpath))
}
//////////////////////////////////////////////////////////////////////////////
// FileNode
// FileNode represents a file in the file system -- the name of the node is
// the name of the file. Folders have children containing further nodes.
type FileNode struct {
ki.Node
Ic gi.IconName `desc:"icon for this file"`
FPath gi.FileName `desc:"full path to this file"`
Size FileSize `desc:"size of the file in bytes"`
Kind string `width:"20" max-width:"20" desc:"type of file / directory -- including MIME type"`
Mode os.FileMode `desc:"file mode bits"`
ModTime FileTime `desc:"time that contents (only) were last modified"`
Buf *TextBuf `json:"-" xml:"-" desc:"file buffer for editing this file"`
FRoot *FileTree `json:"-" xml:"-" desc:"root of the tree -- has global state"`
}
var KiT_FileNode = kit.Types.AddType(&FileNode{}, nil)
func init() {
kit.Types.SetProps(KiT_FileNode, FileNodeProps)
}
// IsDir returns true if file is a directory (folder)
func (fn *FileNode) IsDir() bool {
return fn.Kind == "Folder"
}
// IsSymLink returns true if file is a symlink
func (fn *FileNode) IsSymLink() bool {
return bitflag.Has(fn.Flag, int(FileNodeSymLink))
}
// IsOpen returns true if file is flagged as open
func (fn *FileNode) IsOpen() bool {
return bitflag.Has(fn.Flag, int(FileNodeOpen))
}
// SetOpen sets the open flag
func (fn *FileNode) SetOpen() {
bitflag.Set(&fn.Flag, int(FileNodeOpen))
}
// IsChanged returns true if the file is open and has been changed (edited) since last save
func (fn *FileNode) IsChanged() bool {
if fn.Buf != nil && fn.Buf.Changed {
return true
}
return false
}
// ReadDir reads all the files at given directory into this directory node --
// uses config children to preserve extra info already stored about files. The
// root node represents the directory at the given path.
func (fn *FileNode) ReadDir(path string) {
_, fnm := filepath.Split(path)
fn.SetName(fnm)
fn.FPath = gi.FileName(filepath.Clean(path))
fn.SetOpen()
typ := fn.NodeType()
config := fn.ConfigOfFiles(path)
mods, updt := fn.ConfigChildren(config, true) // unique names
// always go through kids, regardless of mods
for _, sfk := range fn.Kids {
sf := sfk.Embed(KiT_FileNode).(*FileNode)
sf.FRoot = fn.FRoot
sf.SetChildType(typ) // propagate
fp := filepath.Join(path, sf.Nm)
sf.SetNodePath(fp)
}
if mods {
fn.UpdateEnd(updt)
}
}
// NodeType returns the type of nodes to create -- set ChildType property on
// NodeTree to seed this -- otherwise always FileNode
func (fn *FileNode) NodeType() reflect.Type {
if ntp, ok := fn.Prop("ChildType"); ok {
return ntp.(reflect.Type)
}
return KiT_FileNode
}
// ConfigOfFiles returns a type-and-name list for configuring nodes based on
// files immediately within given path
func (fn *FileNode) ConfigOfFiles(path string) kit.TypeAndNameList {
config1 := kit.TypeAndNameList{}
config2 := kit.TypeAndNameList{}
typ := fn.NodeType()
filepath.Walk(path, func(pth string, info os.FileInfo, err error) error {
if err != nil {
emsg := fmt.Sprintf("giv.FileNode ConfigFilesIn Path %q: Error: %v", path, err)
log.Println(emsg)
return nil // ignore
}
if pth == path { // proceed..
return nil
}
_, fnm := filepath.Split(pth)
if fn.FRoot.DirsOnTop {
if info.IsDir() {
config1.Add(typ, fnm)
} else {
config2.Add(typ, fnm)
}
} else {
config1.Add(typ, fnm)
}
if info.IsDir() {
return filepath.SkipDir
}
return nil
})
if fn.FRoot.DirsOnTop {
for _, tn := range config2 {
config1 = append(config1, tn)
}
}
return config1
}
// SetNodePath sets the path for given node and updates it based on associated file
func (fn *FileNode) SetNodePath(path string) error {
fn.FPath = gi.FileName(filepath.Clean(path))
return fn.UpdateNode()
}
// UpdateNode updates information in node based on its associated file in FPath
func (fn *FileNode) UpdateNode() error {
path := string(fn.FPath)
info, err := os.Lstat(path)
if err != nil {
emsg := fmt.Errorf("giv.FileNode UpdateNode Path %q: Error: %v", path, err)
log.Println(emsg)
return emsg
}
fn.Size = FileSize(info.Size())
fn.Mode = info.Mode()
fn.ModTime = FileTime(info.ModTime())
if info.IsDir() {
fn.Kind = "Folder"
} else {
ext := filepath.Ext(fn.Nm)
fn.Kind = mime.TypeByExtension(ext)
fn.Kind = strings.TrimPrefix(fn.Kind, "application/") // long and unnec
}
fn.Ic = FileKindToIcon(fn.Kind, fn.Nm)
if fn.IsDir() {
if fn.FRoot.IsDirOpen(fn.FPath) {
fn.ReadDir(path) // keep going down..
}
}
return nil
}
// OpenDir opens given directory node
func (fn *FileNode) OpenDir() {
fn.FRoot.SetDirOpen(fn.FPath)
fn.UpdateNode()
}
// CloseDir closes given directory node -- updates memory state
func (fn *FileNode) CloseDir() {
fn.FRoot.SetDirClosed(fn.FPath)
// todo: do anything with open files within directory??
}
// OpenBuf opens the file in its buffer if it is not already open
func (fn *FileNode) OpenBuf() error {
if fn.IsDir() {
err := fmt.Errorf("giv.FileNode cannot open directory in editor: %v", fn.FPath)
log.Println(err.Error())
return err
}
if fn.Buf != nil {
return nil
}
fn.Buf = &TextBuf{}
fn.Buf.InitName(fn.Buf, fn.Nm)
return fn.Buf.Open(fn.FPath)
}
// 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 *FileNode) FindFile(fnm string) (*FileNode, bool) {
var ffn *FileNode
found := false
fn.FuncDownMeFirst(0, fn, func(k ki.Ki, level int, d interface{}) bool {
sfn := k.Embed(KiT_FileNode).(*FileNode)
if strings.HasSuffix(string(sfn.FPath), fnm) {
ffn = sfn
found = true
return false
}
return true
})
return ffn, found
}
// FilesMatching returns list of all nodes whose file name contains given
// string (no regexp) -- ignoreCase transforms everything into lowercase
func (fn *FileNode) FilesMatching(match string, ignoreCase bool) []*FileNode {
mls := make([]*FileNode, 0)
if ignoreCase {
match = strings.ToLower(match)
}
fn.FuncDownMeFirst(0, fn, func(k ki.Ki, level int, d interface{}) bool {
sfn := k.Embed(KiT_FileNode).(*FileNode)
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 true
})
return mls
}
// FileNodeNameCount is used to report counts of different string-based things
// in the file tree
type FileNodeNameCount struct {
Name string
Count int
}
// FileExtCounts returns a count of all the different file extensions, sorted
// from highest to lowest
func (fn *FileNode) FileExtCounts() []FileNodeNameCount {
cmap := make(map[string]int, 20)
fn.FuncDownMeFirst(0, fn, func(k ki.Ki, level int, d interface{}) bool {
sfn := k.Embed(KiT_FileNode).(*FileNode)
ext := strings.ToLower(filepath.Ext(sfn.Nm))
if ec, has := cmap[ext]; has {
cmap[ext] = ec + 1
} else {
cmap[ext] = 1
}
return true
})
ecs := make([]FileNodeNameCount, len(cmap))
idx := 0
for key, val := range cmap {
ecs[idx] = FileNodeNameCount{key, val}
idx++
}
sort.Slice(ecs, func(i, j int) bool {
return ecs[i].Count > ecs[j].Count
})
return ecs
}
// DuplicateFile creates a copy of given file -- only works for regular files, not directories
func (fn *FileNode) DuplicateFile() {
if fn.IsDir() {
log.Printf("Duplicate: cannot copy directories\n")
return
}
path := string(fn.FPath)
ext := filepath.Ext(path)
noext := strings.TrimSuffix(path, ext)
dst := noext + "_Copy" + ext
CopyFile(dst, path, fn.Mode)
if fn.Par != nil {
fnp := fn.Par.Embed(KiT_FileNode).(*FileNode)
fnp.UpdateNode()
}
}
// DeleteFile deletes this file
func (fn *FileNode) DeleteFile() {
if fn.IsDir() {
log.Printf("FileNode Delete -- cannot delete directories!\n")
return
}
path := string(fn.FPath)
os.Remove(path)
fn.Delete(true) // we're done
}
// RenameFile renames file to new name
func (fn *FileNode) RenameFile(newpath string) {
if newpath == "" {
log.Printf("FileNode Rename: new name is empty!\n")
return
}
path := string(fn.FPath)
if newpath == path {
return
}
ndir, nfn := filepath.Split(newpath)
if ndir == "" {
if nfn == fn.Nm {
return
}
dir, _ := filepath.Split(path)
newpath = filepath.Join(dir, newpath)
}
os.Rename(path, newpath)
fn.FPath = gi.FileName(filepath.Clean(newpath))
fn.SetName(nfn)
fn.UpdateSig()
}
//////////////////////////////////////////////////////////////////////////
// Search
// FileSearch looks for a string (no regexp) within a file, in a
// case-sensitive way, returning number of occurences and specific match
// position list -- column positions are in bytes, not runes...
func FileSearch(filename string, find []byte) (int, []TextPos) {
fp, err := os.Open(filename)
if err != nil {
log.Printf("gide.FileSearch file open error: %v\n", err)
return 0, nil
}
defer fp.Close()
return BufSearch(fp, find)
}
// BufSearch looks for a string (no regexp) within a byte buffer, in a
// case-sensitive way, returning number of occurences and specific match
// position list -- column positions are in bytes, not runes...
func BufSearch(reader io.Reader, find []byte) (int, []TextPos) {
fsz := len(find)
if fsz == 0 {
return 0, nil
}
cnt := 0
var matches []TextPos
scan := bufio.NewScanner(reader)
ln := 0
for scan.Scan() {
b := scan.Bytes()
sz := len(b)
ci := 0
for ci < sz {
i := bytes.Index(b[ci:], find)
if i < 0 {
break
}
i += ci
ci = i + fsz
matches = append(matches, TextPos{ln, i})
cnt++
}
}
if err := scan.Err(); err != nil {
log.Printf("gide.FileSearch error: %v\n", err)
}
return cnt, matches
}
// FileSearchCI looks for a string (no regexp) within a file, in a
// case-INsensitive way, returning number of occurences -- column positions
// are in bytes, not runes...
func FileSearchCI(filename string, find []byte) (int, []TextPos) {
fp, err := os.Open(filename)
if err != nil {
log.Printf("gide.FileSearch file open error: %v\n", err)
return 0, nil
}
defer fp.Close()
return BufSearchCI(fp, find)
}
// BufSearchCI looks for a string (no regexp) within byte stream, in a
// case-INsensitive way, returning number of occurences -- column positions
// are in bytes, not runes...
func BufSearchCI(reader io.Reader, find []byte) (int, []TextPos) {
fsz := len(find)
if fsz == 0 {
return 0, nil
}
find = bytes.ToLower(find)
cnt := 0
var matches []TextPos
scan := bufio.NewScanner(reader)
ln := 0
for scan.Scan() {
b := bytes.ToLower(scan.Bytes())
sz := len(b)
ci := 0
for ci < sz {
i := bytes.Index(b[ci:], find)
if i < 0 {
break
}
i += ci
ci = i + fsz
matches = append(matches, TextPos{ln, i})
cnt++
}
}
if err := scan.Err(); err != nil {
log.Printf("gide.FileSearch error: %v\n", err)
}
return cnt, matches
}
// FileNodeFlags define bitflags for FileNode state -- these extend ki.Flags
// and storage is an int64
type FileNodeFlags int64
const (
// FileNodeOpen means file is open -- for directories, this means that
// sub-files should be / have been loaded -- for files, means that they
// have been opened e.g., for editing
FileNodeOpen FileNodeFlags = FileNodeFlags(ki.FlagsN) + iota
// FileNodeSymLink indicates that file is a symbolic link -- file info is
// all for the target of the symlink
FileNodeSymLink
FileNodeFlagsN
)
//go:generate stringer -type=FileNodeFlags
var KiT_FileNodeFlags = kit.Enums.AddEnum(FileNodeFlagsN, true, nil) // true = bitflags
var FileNodeProps = ki.Props{
"CtxtMenu": ki.PropSlice{
{"DuplicateFile", ki.Props{
"label": "Duplicate",
"updtfunc": func(fni interface{}, act *gi.Action) {
fn := fni.(ki.Ki).Embed(KiT_FileNode).(*FileNode)
act.SetInactiveStateUpdt(fn.IsDir())
},
}},
{"DeleteFile", ki.Props{
"label": "Delete",
"desc": "Ok to delete this file? This is not undoable and is not moving to trash / recycle bin",
"confirm": true,
"updtfunc": func(fni interface{}, act *gi.Action) {
fn := fni.(ki.Ki).Embed(KiT_FileNode).(*FileNode)
act.SetInactiveStateUpdt(fn.IsDir())
},
}},
{"RenameFile", ki.Props{
"label": "Rename",
"desc": "Rename file to new file name",
"Args": ki.PropSlice{
{"New Name", ki.Props{
"default-field": "Name",
}},
},
}},
{"sep-open", ki.BlankProp{}},
{"OpenDir", ki.Props{
"desc": "open given directory to see files within",
"updtfunc": func(fni interface{}, act *gi.Action) {
fn := fni.(ki.Ki).Embed(KiT_FileNode).(*FileNode)
act.SetActiveStateUpdt(fn.IsDir())
},
}},
},
}
//////////////////////////////////////////////////////////////////////////////
// OpenDirMap
// OpenDirMap is a map for encoding directories that are open in the file
// tree. The strings are typically relative paths. The bool value is used to
// mark active paths and inactive (unmarked) ones can be removed.
type OpenDirMap map[string]bool
// Init initializes the map
func (dm *OpenDirMap) Init() {
if *dm == nil {
*dm = make(OpenDirMap, 1000)
}
}
// IsOpen returns true if path is listed on the open map
func (dm *OpenDirMap) IsOpen(path string) bool {
dm.Init()
if _, ok := (*dm)[path]; ok {
(*dm)[path] = true // mark
return true
}
return false
}
// SetOpen adds the given path to the open map
func (dm *OpenDirMap) SetOpen(path string) {
dm.Init()
(*dm)[path] = true
}
// SetClosed removes given path from the open map
func (dm *OpenDirMap) SetClosed(path string) {
dm.Init()
delete(*dm, path)
}
// ClearFlags sets all the bool flags to false -- do this prior to traversing
// full set of active paths -- can then call RemoveStale to get rid of unused paths
func (dm *OpenDirMap) ClearFlags() {
dm.Init()
for key, _ := range *dm {
(*dm)[key] = false
}
}
// RemoveStale removes all entries with a bool = false value indicating that
// they have not been accessed since ClearFlags was called.
func (dm *OpenDirMap) RemoveStale() {
dm.Init()
for key, val := range *dm {
if !val {
delete(*dm, key)
}
}
}
//////////////////////////////////////////////////////////////////////////////
// FileTreeView
// FileTreeView is a TreeView that knows how to operate on FileNode nodes
type FileTreeView struct {
TreeView
}
var KiT_FileTreeView = kit.Types.AddType(&FileTreeView{}, TreeViewProps)
var fnFolderProps = ki.Props{
"icon": "folder-open",
"icon-off": "folder",
}
func (tv *FileTreeView) Style2D() {
fn := tv.SrcNode.Ptr.Embed(KiT_FileNode).(*FileNode)
if fn.IsDir() {
if fn.IsOpen() {
tv.Icon = gi.IconName("")
} else {
tv.Icon = gi.IconName("folder")
}
tv.SetProp("#branch", fnFolderProps)
} else {
tv.Icon = fn.Ic
}
tv.StyleTreeView()
tv.LayData.SetFromStyle(&tv.Sty.Layout) // also does reset
}