forked from jedib0t/go-pretty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
58 lines (51 loc) · 1.6 KB
/
render.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
package list
import "strings"
// Render renders the List in a human-readable "pretty" format. Example:
// ┌─ Game Of Thrones
// └─┬─ Winter
// ├─ Is
// ├─ Coming
// └─┬─ This
// ├─ Is
// └─ Known
func (l *List) Render() string {
l.init()
// init a new strings.Builder to build the output efficiently and grow it by
// the pre-calculated "approxSize"
var out strings.Builder
out.Grow(l.approxSize)
// render the List item-by-item and use the "Level" property in the item to
// determine the prefix/padding
for idx, item := range l.items {
// when working on item number 2 or more, render a newline first
if idx > 0 {
out.WriteRune('\n')
}
// if there is a change in level, render the connector; else just pad
// the output with spaces based on the current level
levelChanged := bool(idx > 0 && l.items[idx].Level > l.items[idx-1].Level)
if levelChanged {
if item.Level > 1 {
out.WriteString(strings.Repeat(" ", (item.Level-1)*2))
}
out.WriteString(l.style.CharConnect + l.style.CharPaddingLeft)
} else {
out.WriteString(strings.Repeat(" ", item.Level*2))
}
// render the "bullet"
if idx == 0 {
out.WriteString(l.style.CharItemTop)
} else if levelChanged {
out.WriteString(l.style.CharItemFirst)
} else if idx == len(l.items)-1 {
out.WriteString(l.style.CharItemBottom)
} else {
out.WriteString(l.style.CharItem)
}
// pad as directed before rendering the item text
out.WriteString(l.style.CharPaddingRight)
out.WriteRune(' ')
out.WriteString(l.style.Format.Apply(item.Text))
}
return out.String()
}