Skip to content

Commit 931b625

Browse files
authored
new: added Help#AddTexts method (#32)
1 parent 0bff6d2 commit 931b625

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

print-help.go

+21
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,27 @@ func (help *Help) AddText(text string, wrapOpts ...int) {
146146
help.blocks = append(help.blocks, b)
147147
}
148148

149+
// AddTexts is a method which adds an array of texts to this Help instance.
150+
// And this method can optionally set indent, left margin, and right margin as
151+
// variadic arguments, too.
152+
func (help *Help) AddTexts(texts []string, wrapOpts ...int) {
153+
b := block{
154+
marginLeft: help.marginLeft,
155+
marginRight: help.marginRight,
156+
}
157+
if len(wrapOpts) > 0 {
158+
b.indent = wrapOpts[0]
159+
}
160+
if len(wrapOpts) > 1 {
161+
b.marginLeft += wrapOpts[1]
162+
}
163+
if len(wrapOpts) > 2 {
164+
b.marginRight += wrapOpts[2]
165+
}
166+
b.texts = texts
167+
help.blocks = append(help.blocks, b)
168+
}
169+
149170
// AddOpts is a method which adds OptCfg(s) to this Help instance.
150171
// And this method can optionally set indent, left margin, and right margin as
151172
// variadic arguments, too.

print-help_test.go

+130
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,136 @@ func TestAddText_Indent(t *testing.T) {
210210
assert.Equal(t, status, cliargs.ITER_NO_MORE)
211211
}
212212

213+
func TestAddTexts_zeroText(t *testing.T) {
214+
help := cliargs.NewHelp()
215+
help.AddTexts([]string{})
216+
iter := help.Iter()
217+
218+
line, status := iter.Next()
219+
assert.Equal(t, line, "")
220+
assert.Equal(t, status, cliargs.ITER_NO_MORE)
221+
222+
help = cliargs.NewHelp()
223+
help.AddTexts([]string(nil))
224+
iter = help.Iter()
225+
226+
line, status = iter.Next()
227+
assert.Equal(t, line, "")
228+
assert.Equal(t, status, cliargs.ITER_NO_MORE)
229+
}
230+
231+
func TestAddTexts_oneText(t *testing.T) {
232+
help := cliargs.NewHelp()
233+
help.AddTexts([]string{
234+
"a12345678 b12345678 c12345678 d12345678 " +
235+
"e12345678 f12345678 g12345678 h12345678 i123"})
236+
iter := help.Iter()
237+
238+
line, status := iter.Next()
239+
assert.Equal(t, line, "a12345678 b12345678 c12345678 d12345678 "+
240+
"e12345678 f12345678 g12345678 h12345678 ")
241+
assert.Equal(t, status, cliargs.ITER_HAS_MORE)
242+
243+
line, status = iter.Next()
244+
assert.Equal(t, line, "i123")
245+
assert.Equal(t, status, cliargs.ITER_NO_MORE)
246+
247+
line, status = iter.Next()
248+
assert.Equal(t, line, "")
249+
assert.Equal(t, status, cliargs.ITER_NO_MORE)
250+
}
251+
252+
func TestAddTexts_multipleTexts(t *testing.T) {
253+
help := cliargs.NewHelp()
254+
help.AddTexts([]string{
255+
"a12345678 b12345678 c12345678 d12345678 " +
256+
"e12345678 f12345678 g12345678 h12345678 i123",
257+
"j12345678 k12345678 l12345678 m12345678 " +
258+
"n12345678 o12345678 p12345678 q12345678 r123",
259+
})
260+
iter := help.Iter()
261+
262+
line, status := iter.Next()
263+
assert.Equal(t, line, "a12345678 b12345678 c12345678 d12345678 "+
264+
"e12345678 f12345678 g12345678 h12345678 ")
265+
assert.Equal(t, status, cliargs.ITER_HAS_MORE)
266+
267+
line, status = iter.Next()
268+
assert.Equal(t, line, "i123")
269+
assert.Equal(t, status, cliargs.ITER_HAS_MORE)
270+
271+
line, status = iter.Next()
272+
assert.Equal(t, line, "j12345678 k12345678 l12345678 m12345678 "+
273+
"n12345678 o12345678 p12345678 q12345678 ")
274+
assert.Equal(t, status, cliargs.ITER_HAS_MORE)
275+
276+
line, status = iter.Next()
277+
assert.Equal(t, line, "r123")
278+
assert.Equal(t, status, cliargs.ITER_NO_MORE)
279+
280+
line, status = iter.Next()
281+
assert.Equal(t, line, "")
282+
assert.Equal(t, status, cliargs.ITER_NO_MORE)
283+
}
284+
285+
func TestAddTexts_withIndent(t *testing.T) {
286+
help := cliargs.NewHelp()
287+
help.AddTexts([]string{
288+
"a12345678 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789",
289+
"b1234 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789",
290+
}, 11)
291+
iter := help.Iter()
292+
293+
line, status := iter.Next()
294+
assert.Equal(t, line, "a12345678 123456789 123456789 123456789 123456789 123456789 123456789 123456789")
295+
assert.Equal(t, status, cliargs.ITER_HAS_MORE)
296+
297+
line, status = iter.Next()
298+
assert.Equal(t, line, " 123456789 123456789")
299+
assert.Equal(t, status, cliargs.ITER_HAS_MORE)
300+
301+
line, status = iter.Next()
302+
assert.Equal(t, line, "b1234 123456789 123456789 123456789 123456789 123456789 123456789 123456789")
303+
assert.Equal(t, status, cliargs.ITER_HAS_MORE)
304+
305+
line, status = iter.Next()
306+
assert.Equal(t, line, " 123456789 123456789")
307+
assert.Equal(t, status, cliargs.ITER_NO_MORE)
308+
309+
line, status = iter.Next()
310+
assert.Equal(t, line, "")
311+
assert.Equal(t, status, cliargs.ITER_NO_MORE)
312+
}
313+
314+
func TestAddTexts_withMargins(t *testing.T) {
315+
help := cliargs.NewHelp(2, 2)
316+
help.AddTexts([]string{
317+
"a12345678 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789",
318+
"b1234 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789",
319+
}, 11, 5, 3)
320+
iter := help.Iter()
321+
322+
line, status := iter.Next()
323+
assert.Equal(t, line, " a12345678 123456789 123456789 123456789 123456789 123456789 ")
324+
assert.Equal(t, status, cliargs.ITER_HAS_MORE)
325+
326+
line, status = iter.Next()
327+
assert.Equal(t, line, " 123456789 123456789 123456789 123456789")
328+
assert.Equal(t, status, cliargs.ITER_HAS_MORE)
329+
330+
line, status = iter.Next()
331+
assert.Equal(t, line, " b1234 123456789 123456789 123456789 123456789 123456789 ")
332+
assert.Equal(t, status, cliargs.ITER_HAS_MORE)
333+
334+
line, status = iter.Next()
335+
assert.Equal(t, line, " 123456789 123456789 123456789 123456789")
336+
assert.Equal(t, status, cliargs.ITER_NO_MORE)
337+
338+
line, status = iter.Next()
339+
assert.Equal(t, line, "")
340+
assert.Equal(t, status, cliargs.ITER_NO_MORE)
341+
}
342+
213343
func TestAddOpts_zeroOpts(t *testing.T) {
214344
help := cliargs.NewHelp()
215345
help.AddOpts([]cliargs.OptCfg{})

0 commit comments

Comments
 (0)