/
print-help.go
290 lines (256 loc) · 6.18 KB
/
print-help.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
// Copyright (C) 2023 Takayuki Sato. All Rights Reserved.
// This program is free software under MIT License.
// See the file LICENSE in this distribution for more details.
package cliargs
import (
"fmt"
"strings"
"github.com/sttk/linebreak"
)
// Help is a struct type which holds help text blocks and help options block.
type Help struct {
marginLeft, marginRight int
blocks []block
}
type block struct {
indent, marginLeft, marginRight int
texts []string
}
// NewHelp is a function to create a Help instance.
// This function can optionally take left margin and right margin as variadic
// arguments.
func NewHelp(wrapOpts ...int) Help {
var help Help
if len(wrapOpts) > 0 {
help.marginLeft = wrapOpts[0]
}
if len(wrapOpts) > 1 {
help.marginRight = wrapOpts[1]
}
help.blocks = make([]block, 0, 2)
return help
}
// Iter is a method which creates a HelpIter instance.
func (help Help) Iter() HelpIter {
if len(help.blocks) == 0 {
return HelpIter{}
}
lineWidth := linebreak.TermCols()
return HelpIter{
lineWidth: lineWidth,
blocks: help.blocks,
blockIter: newBlockIter(help.blocks[0], lineWidth),
}
}
// HelpIter is a struct type to iterate lines of help texts.
type HelpIter struct {
lineWidth int
blocks []block
blockIter blockIter
}
// Next is a method which returns a line of a help text and a status which
// indicates this HelpIter has more texts or not.
// If there are more lines, the returned IterStatus value is ITER_HAS_MORE,
// otherwise the value is ITER_NO_MORE.
func (iter *HelpIter) Next() (string, bool) {
line, more := iter.blockIter.next()
if !more {
if len(iter.blocks) <= 1 {
return line, false
}
iter.blocks = iter.blocks[1:]
iter.blockIter = newBlockIter(iter.blocks[0], iter.lineWidth)
}
return line, true
}
type blockIter struct {
texts []string
index int
indent int
margin string
lineIter linebreak.LineIter
}
func newBlockIter(b block, lineWidth int) blockIter {
if len(b.texts) == 0 {
return blockIter{}
}
printWidth := lineWidth - b.marginLeft - b.marginRight
if printWidth <= b.indent {
return blockIter{}
}
return blockIter{
texts: b.texts,
indent: b.indent,
margin: strings.Repeat(" ", b.marginLeft),
lineIter: linebreak.New(b.texts[0], printWidth),
}
}
func (iter *blockIter) next() (string, bool) {
if len(iter.texts) == 0 {
return "", false
}
line, more := iter.lineIter.Next()
if len(line) > 0 {
line = iter.margin + line
}
if !more {
iter.index++
if iter.index >= len(iter.texts) {
return line, more
}
iter.lineIter.Init(iter.texts[iter.index])
iter.lineIter.SetIndent("")
return line, true
}
iter.lineIter.SetIndent(strings.Repeat(" ", iter.indent))
return line, more
}
// AddText is a method which adds a text to this Help instance.
// And this method can optionally set indent, left margin, and right margin as
// variadic arguments, too.
func (help *Help) AddText(text string, wrapOpts ...int) {
b := block{
marginLeft: help.marginLeft,
marginRight: help.marginRight,
}
if len(wrapOpts) > 0 {
b.indent = wrapOpts[0]
}
if len(wrapOpts) > 1 {
b.marginLeft += wrapOpts[1]
}
if len(wrapOpts) > 2 {
b.marginRight += wrapOpts[2]
}
b.texts = []string{text}
help.blocks = append(help.blocks, b)
}
// AddTexts is a method which adds an array of texts to this Help instance.
// And this method can optionally set indent, left margin, and right margin as
// variadic arguments, too.
func (help *Help) AddTexts(texts []string, wrapOpts ...int) {
b := block{
marginLeft: help.marginLeft,
marginRight: help.marginRight,
}
if len(wrapOpts) > 0 {
b.indent = wrapOpts[0]
}
if len(wrapOpts) > 1 {
b.marginLeft += wrapOpts[1]
}
if len(wrapOpts) > 2 {
b.marginRight += wrapOpts[2]
}
b.texts = texts
help.blocks = append(help.blocks, b)
}
// AddOpts is a method which adds OptCfg(s) to this Help instance.
// And this method can optionally set indent, left margin, and right margin as
// variadic arguments, too.
func (help *Help) AddOpts(optCfgs []OptCfg, wrapOpts ...int) {
b := block{
marginLeft: help.marginLeft,
marginRight: help.marginRight,
}
if len(wrapOpts) > 0 {
b.indent = wrapOpts[0]
}
if len(wrapOpts) > 1 {
b.marginLeft += wrapOpts[1]
}
if len(wrapOpts) > 2 {
b.marginRight += wrapOpts[2]
}
texts := make([]string, len(optCfgs))
if b.indent > 0 {
i := 0
for _, cfg := range optCfgs {
storeKey := cfg.StoreKey
if len(storeKey) == 0 {
if len(cfg.Names) > 0 && cfg.Names[0] == anyOption {
storeKey = cfg.Names[0]
}
}
if storeKey == anyOption {
continue
}
texts[i] = makeOptTitle(cfg)
width := linebreak.TextWidth(texts[i])
if width+2 > b.indent {
texts[i] += "\n" + strings.Repeat(" ", b.indent) + cfg.Desc
} else {
texts[i] += strings.Repeat(" ", b.indent-width) + cfg.Desc
}
i++
}
texts = texts[0:i]
} else {
widths := make([]int, len(texts))
indent := 0
i := 0
for _, cfg := range optCfgs {
storeKey := cfg.StoreKey
if len(storeKey) == 0 {
if len(cfg.Names) > 0 && cfg.Names[0] == anyOption {
storeKey = cfg.Names[0]
}
}
if storeKey == anyOption {
continue
}
texts[i] = makeOptTitle(cfg)
widths[i] = linebreak.TextWidth(texts[i])
if indent < widths[i] {
indent = widths[i]
}
i++
}
texts = texts[0:i]
indent += 2
b.indent = indent
i = 0
for _, cfg := range optCfgs {
storeKey := cfg.StoreKey
if len(storeKey) == 0 {
if len(cfg.Names) > 0 && cfg.Names[0] == anyOption {
storeKey = cfg.Names[0]
}
}
if storeKey == anyOption {
continue
}
texts[i] += strings.Repeat(" ", indent-widths[i]) + cfg.Desc
i++
}
}
b.texts = texts
help.blocks = append(help.blocks, b)
}
func makeOptTitle(cfg OptCfg) string {
title := ""
for _, name := range cfg.Names {
switch len(name) {
case 0:
case 1:
title += ", -" + name
default:
title += ", --" + name
}
}
if cfg.HasArg && len(cfg.ArgInHelp) > 0 {
title += " " + cfg.ArgInHelp
}
return title[2:]
}
// Print is a method which prints help texts to standard output.
func (help Help) Print() {
iter := help.Iter()
for {
line, more := iter.Next()
fmt.Println(line)
if !more {
break
}
}
}