/
cfprint.go
283 lines (231 loc) · 6.27 KB
/
cfprint.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
// Package cfprint prints config data to the console in a table.
package cfprint
import (
"github.com/sqp/godock/libs/ternary"
"github.com/sqp/godock/libs/text/color"
"github.com/sqp/godock/libs/text/tablist"
"github.com/sqp/godock/widgets/cfbuild/cftype" // Types for config file builder usage.
"strconv"
)
// Printer rows.
const (
RowType = iota
RowName
RowOld
RowNew
)
//
//-----------------------------------------------------------------[ DEFAULT ]--
// Default prints the configuration compared to its default values.
//
func Default(build cftype.Builder, showAll bool) {
cols := []tablist.ColInfo{
tablist.NewColLeft(0, "Type"),
tablist.NewColLeft(0, "Name"),
}
if showAll {
cols = append(cols,
tablist.NewColLeft(0, "Default"),
)
}
cols = append(cols,
tablist.NewColLeft(0, "Current"),
)
lf := &lineFeed{
TableFormater: *tablist.NewFormater(cols...),
}
if showAll {
lf.valuePrint = lf.valueDefault
build.KeyWalk(lf.Add)
} else {
build.KeyWalk(lf.addTest)
}
lf.Print()
if lf.countChanged > 0 {
build.Log().Info("changed", lf.countChanged, "/", lf.countChangeable)
} else {
build.Log().Info("nothing changed")
}
}
func (lf *lineFeed) valueDefault(key *cftype.Key, line tablist.Liner) {
flag := false
def, e := key.Storage().Default(key.Group, key.Name)
if e != nil {
println("default: ", e.Error())
}
valStatus := key.ValueState(def)
for i, st := range valStatus {
curline := lf.testNewLine(line, i)
switch st.State {
case cftype.StateBothEmpty:
line.Set(RowOld, "**EMPTY**")
case cftype.StateUnchanged:
curline.Set(RowOld, st.Old)
case cftype.StateEdited:
curline.Set(RowOld, st.Old)
if st.New == "" {
curline.Colored(RowNew, color.FgMagenta, "**EMPTY**")
} else {
curline.Colored(RowNew, color.FgGreen, st.New)
}
flag = true
case cftype.StateAdded:
curline.Set(RowOld, "**EMPTY**")
curline.Colored(RowNew, color.FgYellow, st.New)
flag = true
case cftype.StateRemoved:
curline.Set(RowOld, st.Old)
curline.Colored(RowNew, color.FgMagenta, "**EMPTY**")
flag = true
}
}
if flag {
lf.countChanged++
}
}
func (lf *lineFeed) addTest(key *cftype.Key) {
def, e := key.Storage().Default(key.Group, key.Name)
if e != nil {
println("default: ", e.Error())
return
}
valStatus := key.ValueState(def)
if !valStatus.IsChanged() {
return
}
lf.countChanged++
lf.valuePrint = func(key *cftype.Key, line tablist.Liner) {
for i, st := range valStatus {
curline := lf.testNewLine(line, i)
switch {
// case cftype.StateUnchanged:
// curline.Set(RowOld, st.Old)
case st.State == cftype.StateEdited && st.New != "":
curline.Set(RowOld, st.New)
case st.State == cftype.StateAdded:
curline.Colored(RowOld, color.FgYellow, st.New)
case st.State == cftype.StateEdited, st.State == cftype.StateRemoved:
curline.Colored(RowOld, color.FgMagenta, "**EMPTY**")
}
}
}
lf.Add(key)
}
//
//-------------------------------------------------------------[ LINE FEEDER ]--
// Updated prints the configuration compared to its storage values.
//
func Updated(build cftype.Builder) {
// build.Log().DEV("save to virtual")
lf := &lineFeed{
TableFormater: *tablist.NewFormater(
tablist.NewColLeft(0, "Type"),
tablist.NewColLeft(0, "Name"),
tablist.NewColLeft(0, "Old value"),
tablist.NewColLeft(0, "New value"),
),
}
lf.valuePrint = lf.valueUpdated
build.KeyWalk(lf.Add)
lf.Print()
if lf.countChanged > 0 {
build.Log().Info("changed", lf.countChanged, "/", lf.countChangeable)
} else {
build.Log().Info("nothing changed")
}
}
func (lf *lineFeed) valueUpdated(key *cftype.Key, line tablist.Liner) {
flag := false
older := key.Storage().Valuer(key.Group, key.Name)
valStatus := key.ValueState(older)
for i, st := range valStatus {
curline := lf.testNewLine(line, i)
switch st.State {
case cftype.StateBothEmpty:
line.Colored(RowOld, color.FgMagenta, "**EMPTY**")
line.Colored(RowNew, color.BgRed, " == ")
case cftype.StateUnchanged:
curline.Set(RowOld, st.Old)
curline.Colored(RowNew, color.BgRed, " == ")
case cftype.StateEdited:
curline.Set(RowOld, st.Old)
st.New = ternary.String(st.New == "", "**EMPTY**", st.New)
curline.Colored(RowNew, color.FgGreen, st.New)
flag = true
case cftype.StateAdded:
curline.Set(RowOld, "**EMPTY**")
curline.Colored(RowNew, color.FgGreen, st.New)
flag = true
case cftype.StateRemoved:
curline.Set(RowOld, st.Old)
curline.Colored(RowNew, color.FgGreen, "**EMPTY**")
flag = true
}
}
if flag {
lf.countChanged++
}
}
//
//-------------------------------------------------------------[ LINE FEEDER ]--
type lineFeed struct {
tablist.TableFormater
valuePrint func(key *cftype.Key, line tablist.Liner)
lastGroup string
hasFrame bool
countChanged int
countChangeable int
}
// Add adds a key to the printer.
//
func (lf *lineFeed) Add(key *cftype.Key) {
if key.Group != lf.lastGroup {
lf.AddGroup(RowName, key.Group)
lf.lastGroup = key.Group
lf.hasFrame = false
}
var line tablist.Liner
var title string
switch {
case key.IsType(cftype.KeyFrame, cftype.KeyExpander):
line = lf.AddEmptyFilled()
lf.hasFrame = true
line.Set(RowType, key.Type.String())
if len(key.AuthorizedValues) == 0 {
title = "[*FRAME NO TITLE*]"
} else {
title = key.AuthorizedValues[0]
}
case key.IsType(cftype.KeySeparator):
line = lf.AddEmptyFilled()
line.Set(RowType, key.Type.String())
title = lf.indent() + "---------"
case key.IsType(cftype.KeyTextLabel, cftype.KeyLaunchCmdSimple):
line = lf.AddEmptyFilled()
line.Set(RowType, key.Type.String())
title = lf.indent() + key.Name
default:
line = lf.AddLine()
lf.valuePrint(key, line)
line.Colored(RowType, color.FgGreen, key.Type.String())
title = lf.indent() + key.Name
lf.countChangeable++
}
line.Set(RowName, title)
}
// indent returns 2 space of indent for keys in a frame.
//
func (lf *lineFeed) indent() string {
return ternary.String(lf.hasFrame, " ", "")
}
// testNewLine creates a new line if the valueID is greater than 0.
//
func (lf *lineFeed) testNewLine(line tablist.Liner, valueID int) tablist.Liner {
if valueID > 0 {
reline := lf.AddLine()
reline.Set(RowType, " +value "+strconv.Itoa(valueID+1))
reline.Set(RowName, lf.indent()+" -----")
return reline
}
return line
}