forked from jedib0t/go-pretty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
178 lines (156 loc) · 4.55 KB
/
list.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
package list
import (
"fmt"
"io"
"strings"
"unicode/utf8"
)
const (
// DefaultHTMLCSSClass stores the css-class to use when none-provided via
// SetHTMLCSSClass(cssClass string).
DefaultHTMLCSSClass = "go-pretty-table"
)
// listItem represents one line in the List
type listItem struct {
Level int
Text string
}
// List helps print a 2-dimensional array in a human readable pretty-List.
type List struct {
// approxSize stores the approximate output length/size
approxSize int
// htmlCSSClass stores the HTML CSS Class to use on the <ul> node
htmlCSSClass string
// items contains the list of items to render
items []*listItem
// level stores the current indentation level
level int
// outputMirror stores an io.Writer where the "Render" functions would write
outputMirror io.Writer
// style contains all the strings used to draw the List, and more
style *Style
}
// AppendItem appends the item to the List of items to render.
func (l *List) AppendItem(item interface{}) {
l.items = append(l.items, l.analyzeAndStringify(item))
}
// AppendItems appends the items to the List of items to render.
func (l *List) AppendItems(items []interface{}) {
for _, item := range items {
l.AppendItem(item)
}
}
// Indent indents the following items to appear right-shifted.
func (l *List) Indent() {
if len(l.items) == 0 {
// should not indent when there is no item in the current level
} else if l.level > l.items[len(l.items)-1].Level {
// already indented compared to previous item; do not indent more
} else {
l.level++
}
}
// Length returns the number of items to be rendered.
func (l *List) Length() int {
return len(l.items)
}
// Reset sets the List to its initial state.
func (l *List) Reset() {
l.approxSize = 0
l.items = make([]*listItem, 0)
l.level = 0
l.style = nil
}
// SetHTMLCSSClass sets the the HTML CSS Class to use on the <ul> node
// when rendering the List in HTML format. Recursive lists would use a numbered
// index suffix. For ex., if the cssClass is set as "foo"; the <ul> for level 0
// would have the class set as "foo"; the <ul> for level 1 would have "foo-1".
func (l *List) SetHTMLCSSClass(cssClass string) {
l.htmlCSSClass = cssClass
}
// SetOutputMirror sets an io.Writer for all the Render functions to "Write" to
// in addition to returning a string.
func (l *List) SetOutputMirror(mirror io.Writer) {
l.outputMirror = mirror
}
// SetStyle overrides the DefaultStyle with the provided one.
func (l *List) SetStyle(style Style) {
l.style = &style
}
// Style returns the current style.
func (l *List) Style() *Style {
if l.style == nil {
tempStyle := StyleDefault
l.style = &tempStyle
}
return l.style
}
func (l *List) analyzeAndStringify(item interface{}) *listItem {
itemStr := fmt.Sprint(item)
if strings.Contains(itemStr, "\t") {
itemStr = strings.Replace(itemStr, "\t", " ", -1)
}
if strings.Contains(itemStr, "\r") {
itemStr = strings.Replace(itemStr, "\r", "", -1)
}
return &listItem{
Level: l.level,
Text: itemStr,
}
}
// UnIndent un-indents the following items to appear left-shifted.
func (l *List) UnIndent() {
if l.level > 0 {
l.level--
}
}
func (l *List) UnIndentAll() {
l.level = 0
}
func (l *List) initForRender() {
// pick a default style
l.Style()
// calculate the approximate size needed by looking at all entries
l.approxSize = 0
for _, item := range l.items {
// account for the following when incrementing approxSize:
// 1. prefix, 2. padding, 3. bullet, 4. text, 5. newline
l.approxSize += utf8.RuneCountInString(l.style.LinePrefix)
if item.Level > 0 {
l.approxSize += utf8.RuneCountInString(l.style.CharItemVertical) * item.Level
}
l.approxSize += utf8.RuneCountInString(l.style.CharItemVertical)
l.approxSize += utf8.RuneCountInString(item.Text)
l.approxSize += utf8.RuneCountInString(l.style.CharNewline)
}
// default to a HTML CSS Class if none-defined
if l.htmlCSSClass == "" {
l.htmlCSSClass = DefaultHTMLCSSClass
}
}
func (l *List) hasMoreItemsInLevel(levelIdx int, fromItemIdx int) bool {
for idx := fromItemIdx + 1; idx >= 0 && idx < len(l.items); idx++ {
if l.items[idx].Level < levelIdx {
return false
} else if l.items[idx].Level == levelIdx {
return true
}
}
return false
}
func (l *List) render(out *strings.Builder) string {
outStr := out.String()
if l.outputMirror != nil && len(outStr) > 0 {
l.outputMirror.Write([]byte(outStr))
l.outputMirror.Write([]byte("\n"))
}
return outStr
}
// renderHint has hints for the Render*() logic
type renderHint struct {
isTopItem bool
isFirstItem bool
isOnlyItem bool
isLastItem bool
isBottomItem bool
}