-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
158 lines (122 loc) · 3.57 KB
/
node.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
// proptree handles nested node tree with properties `Name`, `Icon`, `Tag` and `Description`.
package proptree
import (
"github.com/fatih/color"
)
type prop struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
NameColor *color.Color
Icon string `json:"icon,omitempty" yaml:"icon,omitempty"`
IconColor *color.Color
Tag string `json:"tag,omitempty" yaml:"tag,omitempty"`
TagColor *color.Color
Descriptions []string `json:"description,omitempty" yaml:"description,omitempty"`
DescriptionColor *color.Color
}
// N stores node data.
type N struct {
Prop *prop `json:"prop,omitempty" yaml:"prop,omitempty"`
Children []*N `json:"children,omitempty" yaml:"children,omitempty"`
ancestors []*N
isLast bool
}
// Node returns struct `N` with `Name` and `color.Attribute`.
func Node(name string, c ...color.Attribute) *N {
n := &N{
isLast: true,
Prop: &prop{
Name: name,
},
}
if len(c) > 0 {
n.Prop.NameColor = color.New(c...)
}
return n
}
// NameColor is a setter for a color attribute for a name property.
func (n *N) NameColor(c ...color.Attribute) *N {
n.Prop.NameColor = color.New(c...)
return n
}
func (n *N) RenderName() string {
if n.Prop.NameColor != nil {
return n.Prop.NameColor.Sprint(n.Prop.Name)
}
return n.Prop.Name
}
// Icon is a setter to set a string for icon attribute.
// 2nd- args are set for `IconColor` attribute.
func (n *N) Icon(icon string, c ...color.Attribute) *N {
n.Prop.Icon = icon
if len(c) > 0 {
n.Prop.IconColor = color.New(c...)
}
return n
}
// IconColor is a setter for a color attribute for an icon property.
func (n *N) IconColor(c ...color.Attribute) *N {
n.Prop.IconColor = color.New(c...)
return n
}
func (n *N) iconLen() int {
return len(n.Prop.Icon)
}
// Tag is a setter to set a string for tag attribute.
// 2nd- args are set for `TagColor` attribute.
func (n *N) Tag(tag string, c ...color.Attribute) *N {
n.Prop.Tag = tag
if len(c) > 0 {
n.Prop.TagColor = color.New(c...)
}
return n
}
// TagColor is a setter for a color attribute for a tag property.
func (n *N) TagColor(c ...color.Attribute) *N {
n.Prop.TagColor = color.New(c...)
return n
}
// Description is a setter to set a string for description attribute.
// 2nd- args are set for `DescriptionColor` attribute.
func (n *N) Description(description string, c ...color.Attribute) *N {
return n.Descriptions([]string{description}, c...)
}
// Descriptions method is a setter to set multiple lines for description attribute.
// 2nd- args are set for `DescriptionColor` attribute.
func (n *N) Descriptions(descriptions []string, c ...color.Attribute) *N {
n.Prop.Descriptions = append(n.Prop.Descriptions, descriptions...)
if len(c) > 0 {
n.Prop.DescriptionColor = color.New(c...)
}
return n
}
// DescriptionColor is a setter for a color attribute for a description property.
func (n *N) DescriptionColor(c ...color.Attribute) *N {
n.Prop.DescriptionColor = color.New(c...)
return n
}
func (n *N) hasDescription() bool {
return len(n.Prop.Descriptions) > 0
}
func (n *N) hasChild() bool {
return len(n.Children) > 0
}
func (n *N) isRoot() bool {
return len(n.ancestors) == 0
}
func (n *N) depth() int {
return len(n.ancestors)
}
// Append appends a node tree.
func (n *N) Append(c *N) *N {
if len(c.ancestors) != 0 {
panic("Node already having a parent")
}
for _, child := range n.Children {
child.isLast = false
}
c.ancestors = append(n.ancestors, c.ancestors...)
c.ancestors = append([]*N{n}, c.ancestors...)
c.isLast = true
n.Children = append(n.Children, c)
return n
}