forked from pterm/pterm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bulletlist_printer.go
126 lines (109 loc) · 3.25 KB
/
bulletlist_printer.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
package pterm
import (
"io"
"strings"
)
// BulletListItem is able to render a ListItem.
type BulletListItem struct {
Level int
Text string
TextStyle *Style
Bullet string
BulletStyle *Style
}
// WithLevel returns a new BulletListItem with a specific Level.
func (p BulletListItem) WithLevel(level int) *BulletListItem {
p.Level = level
return &p
}
// WithText returns a new BulletListItem with a specific Text.
func (p BulletListItem) WithText(text string) *BulletListItem {
p.Text = text
return &p
}
// WithTextStyle returns a new BulletListItem with a specific TextStyle.
func (p BulletListItem) WithTextStyle(style *Style) *BulletListItem {
p.TextStyle = style
return &p
}
// WithBullet returns a new BulletListItem with a specific Prefix.
func (p BulletListItem) WithBullet(bullet string) *BulletListItem {
p.Bullet = bullet
return &p
}
// WithBulletStyle returns a new BulletListItem with a specific BulletStyle.
func (p BulletListItem) WithBulletStyle(style *Style) *BulletListItem {
p.BulletStyle = style
return &p
}
// DefaultBulletList contains standards, which can be used to print a BulletListPrinter.
var DefaultBulletList = BulletListPrinter{
Bullet: "•",
TextStyle: &ThemeDefault.BulletListTextStyle,
BulletStyle: &ThemeDefault.BulletListBulletStyle,
}
// BulletListPrinter is able to render a list.
type BulletListPrinter struct {
Items []BulletListItem
TextStyle *Style
Bullet string
BulletStyle *Style
Writer io.Writer
}
// WithItems returns a new list with specific Items.
func (l BulletListPrinter) WithItems(items []BulletListItem) *BulletListPrinter {
l.Items = append(l.Items, items...)
return &l
}
// WithTextStyle returns a new list with a specific text style.
func (l BulletListPrinter) WithTextStyle(style *Style) *BulletListPrinter {
l.TextStyle = style
return &l
}
// WithBullet returns a new list with a specific bullet.
func (l BulletListPrinter) WithBullet(bullet string) *BulletListPrinter {
l.Bullet = bullet
return &l
}
// WithBulletStyle returns a new list with a specific bullet style.
func (l BulletListPrinter) WithBulletStyle(style *Style) *BulletListPrinter {
l.BulletStyle = style
return &l
}
// WithWriter sets the custom Writer.
func (l BulletListPrinter) WithWriter(writer io.Writer) *BulletListPrinter {
l.Writer = writer
return &l
}
// Render prints the list to the terminal.
func (l BulletListPrinter) Render() error {
s, _ := l.Srender()
Fprintln(l.Writer, s)
return nil
}
// Srender renders the list as a string.
func (l BulletListPrinter) Srender() (string, error) {
var ret string
for _, item := range l.Items {
if item.TextStyle == nil {
if l.TextStyle == nil {
item.TextStyle = &ThemeDefault.BulletListTextStyle
} else {
item.TextStyle = l.TextStyle
}
}
if item.BulletStyle == nil {
if l.BulletStyle == nil {
item.BulletStyle = &ThemeDefault.BulletListBulletStyle
} else {
item.BulletStyle = l.BulletStyle
}
}
if item.Bullet == "" {
ret += strings.Repeat(" ", item.Level) + item.BulletStyle.Sprint(l.Bullet) + " " + item.TextStyle.Sprint(item.Text) + "\n"
} else {
ret += strings.Repeat(" ", item.Level) + item.BulletStyle.Sprint(item.Bullet) + " " + item.TextStyle.Sprint(item.Text) + "\n"
}
}
return ret, nil
}