forked from johnmikee/pterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
header_printer.go
236 lines (202 loc) · 6.91 KB
/
header_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
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
package pterm
import (
"fmt"
"io"
"strings"
"github.com/mattn/go-runewidth"
"github.com/gemini/pterm/internal"
)
var (
// DefaultHeader returns the printer for a default header text.
// Defaults to LightWhite, Bold Text and a Gray DefaultHeader background.
DefaultHeader = HeaderPrinter{
TextStyle: &ThemeDefault.HeaderTextStyle,
BackgroundStyle: &ThemeDefault.HeaderBackgroundStyle,
Margin: 5,
}
)
// HeaderPrinter contains the data used to craft a header.
// A header is printed as a big box with text in it.
// Can be used as title screens or section separator.
type HeaderPrinter struct {
TextStyle *Style
BackgroundStyle *Style
Margin int
FullWidth bool
Writer io.Writer
}
// WithTextStyle returns a new HeaderPrinter with changed
func (p HeaderPrinter) WithTextStyle(style *Style) *HeaderPrinter {
p.TextStyle = style
return &p
}
// WithBackgroundStyle changes the background styling of the header.
func (p HeaderPrinter) WithBackgroundStyle(style *Style) *HeaderPrinter {
p.BackgroundStyle = style
return &p
}
// WithMargin changes the background styling of the header.
func (p HeaderPrinter) WithMargin(margin int) *HeaderPrinter {
p.Margin = margin
return &p
}
// WithFullWidth enables full width on a HeaderPrinter.
func (p HeaderPrinter) WithFullWidth(b ...bool) *HeaderPrinter {
p.FullWidth = internal.WithBoolean(b)
return &p
}
// WithWriter sets the custom Writer.
func (p HeaderPrinter) WithWriter(writer io.Writer) *HeaderPrinter {
p.Writer = writer
return &p
}
// Sprint formats using the default formats for its operands and returns the resulting string.
// Spaces are added between operands when neither is a string.
func (p HeaderPrinter) Sprint(a ...interface{}) string {
if RawOutput {
return Sprint(a...)
}
if p.TextStyle == nil {
p.TextStyle = NewStyle()
}
if p.BackgroundStyle == nil {
p.BackgroundStyle = NewStyle()
}
text := Sprint(a...)
var blankLine string
longestLine := internal.ReturnLongestLine(text, "\n")
longestLineLen := runewidth.StringWidth(RemoveColorFromString(longestLine)) + p.Margin*2
if p.FullWidth {
text = splitText(text, GetTerminalWidth()-p.Margin*2)
blankLine = strings.Repeat(" ", GetTerminalWidth())
} else {
if longestLineLen > GetTerminalWidth() {
text = splitText(text, GetTerminalWidth()-p.Margin*2)
blankLine = strings.Repeat(" ", GetTerminalWidth())
} else {
text = splitText(text, longestLineLen-p.Margin*2)
blankLine = strings.Repeat(" ", longestLineLen)
}
}
var marginString string
var ret string
if p.FullWidth {
longestLineLen = runewidth.StringWidth(RemoveColorFromString(internal.ReturnLongestLine(text, "\n")))
marginString = strings.Repeat(" ", (GetTerminalWidth()-longestLineLen)/2)
} else {
marginString = strings.Repeat(" ", p.Margin)
}
ret += p.BackgroundStyle.Sprint(blankLine) + "\n"
for _, line := range strings.Split(text, "\n") {
line = strings.ReplaceAll(line, "\n", "")
line = marginString + line + marginString
if runewidth.StringWidth(line) < runewidth.StringWidth(blankLine) {
line += strings.Repeat(" ", runewidth.StringWidth(blankLine)-runewidth.StringWidth(line))
}
ret += p.BackgroundStyle.Sprint(p.TextStyle.Sprint(line)) + "\n"
}
ret += p.BackgroundStyle.Sprint(blankLine) + "\n"
return ret
}
func splitText(text string, width int) string {
var lines []string
linesTmp := strings.Split(text, "\n")
for _, line := range linesTmp {
if runewidth.StringWidth(RemoveColorFromString(line)) > width {
extraLines := []string{""}
extraLinesCounter := 0
for i, letter := range line {
if i%width == 0 && i != 0 {
extraLinesCounter++
extraLines = append(extraLines, "")
}
extraLines[extraLinesCounter] += string(letter)
}
for _, extraLine := range extraLines {
extraLine += "\n"
lines = append(lines, extraLine)
}
} else {
line += "\n"
lines = append(lines, line)
}
}
var line string
for _, s := range lines {
line += s
}
return strings.TrimSuffix(line, "\n")
}
// Sprintln formats using the default formats for its operands and returns the resulting string.
// Spaces are always added between operands and a newline is appended.
func (p HeaderPrinter) Sprintln(a ...interface{}) string {
return p.Sprint(strings.TrimSuffix(Sprintln(a...), "\n"))
}
// Sprintf formats according to a format specifier and returns the resulting string.
func (p HeaderPrinter) Sprintf(format string, a ...interface{}) string {
return p.Sprint(Sprintf(format, a...))
}
// Sprintfln formats according to a format specifier and returns the resulting string.
// Spaces are always added between operands and a newline is appended.
func (p HeaderPrinter) Sprintfln(format string, a ...interface{}) string {
return p.Sprintf(format, a...) + "\n"
}
// Print formats using the default formats for its operands and writes to standard output.
// Spaces are added between operands when neither is a string.
// It returns the number of bytes written and any write error encountered.
func (p *HeaderPrinter) Print(a ...interface{}) *TextPrinter {
Fprint(p.Writer, p.Sprint(a...))
tp := TextPrinter(p)
return &tp
}
// Println formats using the default formats for its operands and writes to standard output.
// Spaces are always added between operands and a newline is appended.
// It returns the number of bytes written and any write error encountered.
func (p *HeaderPrinter) Println(a ...interface{}) *TextPrinter {
Fprint(p.Writer, p.Sprintln(a...))
tp := TextPrinter(p)
return &tp
}
// Printf formats according to a format specifier and writes to standard output.
// It returns the number of bytes written and any write error encountered.
func (p *HeaderPrinter) Printf(format string, a ...interface{}) *TextPrinter {
Fprint(p.Writer, p.Sprintf(format, a...))
tp := TextPrinter(p)
return &tp
}
// Printfln formats according to a format specifier and writes to standard output.
// Spaces are always added between operands and a newline is appended.
// It returns the number of bytes written and any write error encountered.
func (p *HeaderPrinter) Printfln(format string, a ...interface{}) *TextPrinter {
Fprint(p.Writer, p.Sprintfln(format, a...))
tp := TextPrinter(p)
return &tp
}
// PrintOnError prints every error which is not nil.
// If every error is nil, nothing will be printed.
// This can be used for simple error checking.
func (p *HeaderPrinter) PrintOnError(a ...interface{}) *TextPrinter {
for _, arg := range a {
if err, ok := arg.(error); ok {
if err != nil {
p.Println(err)
}
}
}
tp := TextPrinter(p)
return &tp
}
// PrintOnErrorf wraps every error which is not nil and prints it.
// If every error is nil, nothing will be printed.
// This can be used for simple error checking.
func (p *HeaderPrinter) PrintOnErrorf(format string, a ...interface{}) *TextPrinter {
for _, arg := range a {
if err, ok := arg.(error); ok {
if err != nil {
p.Println(fmt.Errorf(format, err))
}
}
}
tp := TextPrinter(p)
return &tp
}