-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwasm_tree_spreader.go
158 lines (131 loc) · 3.2 KB
/
wasm_tree_spreader.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
//go:build tinywasm
package gtree
import (
"bufio"
"encoding/json"
"fmt"
"io"
"github.com/fatih/color"
)
func newSpreader(encode encode) spreader {
switch encode {
case encodeJSON:
return &jsonSpreader{}
default:
return &defaultSpreader{}
}
}
func newColorizeSpreader(fileExtensions []string) spreader {
return &colorizeSpreader{
defaultSpreader: &defaultSpreader{},
fileConsiderer: newFileConsiderer(fileExtensions),
fileColor: color.New(color.Bold, color.FgHiCyan),
fileCounter: newCounter(),
dirColor: color.New(color.FgGreen),
dirCounter: newCounter(),
}
}
type encode int
const (
encodeDefault encode = iota
encodeJSON
encodeYAML
encodeTOML
)
type defaultSpreader struct{}
func (ds *defaultSpreader) spread(w io.Writer, roots []*Node) error {
branches := ""
for _, root := range roots {
branches += ds.spreadBranch(root)
}
return ds.write(w, branches)
}
func (*defaultSpreader) spreadBranch(current *Node) string {
ret := current.branch()
for _, child := range current.children {
ret += (*defaultSpreader)(nil).spreadBranch(child)
}
return ret
}
func (*defaultSpreader) write(w io.Writer, in string) error {
buf := bufio.NewWriter(w)
if _, err := buf.WriteString(in); err != nil {
return err
}
return buf.Flush()
}
type colorizeSpreader struct {
*defaultSpreader // NOTE: xxx
fileConsiderer *fileConsiderer
fileColor *color.Color
fileCounter *counter
dirColor *color.Color
dirCounter *counter
}
func (cs *colorizeSpreader) spread(w io.Writer, roots []*Node) error {
ret := ""
for _, root := range roots {
cs.fileCounter.reset()
cs.dirCounter.reset()
ret += fmt.Sprintf("%s\n%s", cs.spreadBranch(root), cs.summary())
}
return cs.write(w, ret)
}
func (cs *colorizeSpreader) spreadBranch(current *Node) string {
cs.colorize(current)
ret := current.branch()
for _, child := range current.children {
ret += cs.spreadBranch(child)
}
return ret
}
func (cs *colorizeSpreader) colorize(current *Node) {
if cs.fileConsiderer.isFile(current) {
_ = cs.fileCounter.next()
current.name = cs.fileColor.Sprint(current.name)
} else {
_ = cs.dirCounter.next()
current.name = cs.dirColor.Sprint(current.name)
}
}
func (cs *colorizeSpreader) summary() string {
return fmt.Sprintf(
"%d directories, %d files\n",
cs.dirCounter.current(),
cs.fileCounter.current(),
)
}
type jsonSpreader struct{}
func (*jsonSpreader) spread(w io.Writer, roots []*Node) error {
enc := json.NewEncoder(w)
for _, root := range roots {
jRoot := root.toJSONNode(nil)
if err := enc.Encode(jRoot); err != nil {
return err
}
}
return nil
}
type jsonNode struct {
Name string `json:"value"`
Children []*jsonNode `json:"children"`
}
func (parent *Node) toJSONNode(jParent *jsonNode) *jsonNode {
if jParent == nil {
jParent = &jsonNode{Name: parent.name}
}
if !parent.hasChild() {
return jParent
}
jParent.Children = make([]*jsonNode, len(parent.children))
for i := range parent.children {
jParent.Children[i] = &jsonNode{Name: parent.children[i].name}
_ = parent.children[i].toJSONNode(jParent.Children[i])
}
return jParent
}
var (
_ spreader = (*defaultSpreader)(nil)
_ spreader = (*colorizeSpreader)(nil)
_ spreader = (*jsonSpreader)(nil)
)