forked from KurokuLabs/margo
/
vfs.go
66 lines (56 loc) · 1.08 KB
/
vfs.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
package mg
import (
"margo.sh/vfs"
"path/filepath"
)
var (
vFS = vfs.New()
)
type vfsCmd struct{ ReducerType }
func (vc *vfsCmd) Reduce(mx *Ctx) *State {
switch mx.Action.(type) {
case ViewSaved:
go vc.sync(mx)
case RunCmd:
return mx.AddBuiltinCmds(BuiltinCmd{
Name: ".vfs",
Desc: "Print a tree representing the default VFS",
Run: vc.run,
})
}
return mx.State
}
func (vc *vfsCmd) run(cx *CmdCtx) *State {
go vc.cmd(cx)
return cx.State
}
func (vc *vfsCmd) cmd(cx *CmdCtx) {
defer cx.Output.Close()
if len(cx.Args) == 0 {
vFS.Print(cx.Output)
return
}
for _, p := range cx.Args {
nd, pat := &vFS.Node, p
if filepath.IsAbs(p) {
nd, pat = vFS.Peek(filepath.Dir(p)), filepath.Base(p)
}
nd.PrintWithFilter(cx.Output, func(nd *vfs.Node) string {
if nd.IsBranch() {
return nd.String()
}
if ok, _ := filepath.Match(pat, nd.Name()); ok {
return nd.String()
}
return ""
})
}
}
func (vc *vfsCmd) sync(mx *Ctx) {
v := mx.View
vFS.Invalidate(v.Filename())
vFS.Invalidate(v.Dir())
}
func init() {
DefaultReducers.Before(&vfsCmd{})
}