forked from pterm/pterm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
prefix_printer.go
350 lines (306 loc) · 9.88 KB
/
prefix_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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package pterm
import (
"fmt"
"io"
"runtime"
"strings"
"github.com/forvitinn/pterm/internal"
)
var (
// GrayBoxStyle wraps text in a gray box.
GrayBoxStyle = NewStyle(BgGray, FgLightWhite)
)
var (
// Info returns a PrefixPrinter, which can be used to print text with an "info" Prefix.
Info = PrefixPrinter{
MessageStyle: &ThemeDefault.InfoMessageStyle,
Prefix: Prefix{
Style: &ThemeDefault.InfoPrefixStyle,
Text: "INFO",
},
}
// Warning returns a PrefixPrinter, which can be used to print text with a "warning" Prefix.
Warning = PrefixPrinter{
MessageStyle: &ThemeDefault.WarningMessageStyle,
Prefix: Prefix{
Style: &ThemeDefault.WarningPrefixStyle,
Text: "WARNING",
},
}
// Success returns a PrefixPrinter, which can be used to print text with a "success" Prefix.
Success = PrefixPrinter{
MessageStyle: &ThemeDefault.SuccessMessageStyle,
Prefix: Prefix{
Style: &ThemeDefault.SuccessPrefixStyle,
Text: "SUCCESS",
},
}
// Error returns a PrefixPrinter, which can be used to print text with an "error" Prefix.
Error = PrefixPrinter{
MessageStyle: &ThemeDefault.ErrorMessageStyle,
Prefix: Prefix{
Style: &ThemeDefault.ErrorPrefixStyle,
Text: " ERROR ",
},
}
// Fatal returns a PrefixPrinter, which can be used to print text with an "fatal" Prefix.
// NOTICE: Fatal terminates the application immediately!
Fatal = PrefixPrinter{
MessageStyle: &ThemeDefault.FatalMessageStyle,
Prefix: Prefix{
Style: &ThemeDefault.FatalPrefixStyle,
Text: " FATAL ",
},
Fatal: true,
}
// Debug Prints debug messages. By default it will only print if PrintDebugMessages is true.
// You can change PrintDebugMessages with EnableDebugMessages and DisableDebugMessages, or by setting the variable itself.
Debug = PrefixPrinter{
MessageStyle: &ThemeDefault.DebugMessageStyle,
Prefix: Prefix{
Text: " DEBUG ",
Style: &ThemeDefault.DebugPrefixStyle,
},
Debugger: true,
}
// Description returns a PrefixPrinter, which can be used to print text with a "description" Prefix.
Description = PrefixPrinter{
MessageStyle: &ThemeDefault.DescriptionMessageStyle,
Prefix: Prefix{
Style: &ThemeDefault.DescriptionPrefixStyle,
Text: "Description",
},
}
)
// PrefixPrinter is the printer used to print a Prefix.
type PrefixPrinter struct {
Prefix Prefix
Scope Scope
MessageStyle *Style
Fatal bool
ShowLineNumber bool
LineNumberOffset int
Writer io.Writer
// If Debugger is true, the printer will only print if PrintDebugMessages is set to true.
// You can change PrintDebugMessages with EnableDebugMessages and DisableDebugMessages, or by setting the variable itself.
Debugger bool
}
// WithPrefix adds a custom prefix to the printer.
func (p PrefixPrinter) WithPrefix(prefix Prefix) *PrefixPrinter {
p.Prefix = prefix
return &p
}
// WithScope adds a scope to the Prefix.
func (p PrefixPrinter) WithScope(scope Scope) *PrefixPrinter {
p.Scope = scope
return &p
}
// WithMessageStyle adds a custom prefix to the printer.
func (p PrefixPrinter) WithMessageStyle(style *Style) *PrefixPrinter {
p.MessageStyle = style
return &p
}
// WithFatal sets if the printer should panic after printing.
// NOTE:
// The printer will only panic if either PrefixPrinter.Println, PrefixPrinter.Print
// or PrefixPrinter.Printf is called.
func (p PrefixPrinter) WithFatal(b ...bool) *PrefixPrinter {
p.Fatal = internal.WithBoolean(b)
return &p
}
// WithShowLineNumber sets if the printer should print the line number from where it's called in a go file.
func (p PrefixPrinter) WithShowLineNumber(b ...bool) *PrefixPrinter {
p.ShowLineNumber = internal.WithBoolean(b)
return &p
}
// WithDebugger returns a new Printer with specific Debugger value.
// If Debugger is true, the printer will only print if PrintDebugMessages is set to true.
// You can change PrintDebugMessages with EnableDebugMessages and DisableDebugMessages, or by setting the variable itself.
func (p PrefixPrinter) WithDebugger(b ...bool) *PrefixPrinter {
p.Debugger = internal.WithBoolean(b)
return &p
}
// WithLineNumberOffset can be used to exclude a specific amount of calls in the call stack.
// If you make a wrapper function for example, you can set this to one.
// The printed line number will then be the line number where your wrapper function is called.
func (p PrefixPrinter) WithLineNumberOffset(offset int) *PrefixPrinter {
p.LineNumberOffset = offset
return &p
}
// WithWriter sets the custom Writer.
func (p PrefixPrinter) WithWriter(writer io.Writer) *PrefixPrinter {
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 *PrefixPrinter) Sprint(a ...interface{}) string {
m := Sprint(a...)
if p.Debugger && !PrintDebugMessages {
return ""
}
if RawOutput {
if p.Prefix.Text != "" {
return Sprintf("%s: %s", strings.TrimSpace(p.Prefix.Text), Sprint(a...))
} else {
return Sprint(a...)
}
}
if p.Prefix.Style == nil {
p.Prefix.Style = NewStyle()
}
if p.Scope.Style == nil {
p.Scope.Style = NewStyle()
}
if p.MessageStyle == nil {
p.MessageStyle = NewStyle()
}
var ret string
var newLine bool
if strings.HasSuffix(m, "\n") {
m = strings.TrimRight(m, "\n")
newLine = true
}
messageLines := strings.Split(m, "\n")
for i, m := range messageLines {
if i == 0 {
ret += p.GetFormattedPrefix() + " "
if p.Scope.Text != "" {
ret += NewStyle(*p.Scope.Style...).Sprint(" (" + p.Scope.Text + ") ")
}
ret += p.MessageStyle.Sprint(m)
} else {
ret += "\n" + p.Prefix.Style.Sprint(strings.Repeat(" ", len(p.Prefix.Text)+2)) + " " + p.MessageStyle.Sprint(m)
}
}
_, fileName, line, _ := runtime.Caller(3 + p.LineNumberOffset)
if p.ShowLineNumber {
ret += FgGray.Sprint("\n└ " + fmt.Sprintf("(%s:%d)\n", fileName, line))
newLine = false
}
if newLine {
ret += "\n"
}
return Sprint(ret)
}
// 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 PrefixPrinter) Sprintln(a ...interface{}) string {
if p.Debugger && !PrintDebugMessages {
return ""
}
str := fmt.Sprintln(a...)
return p.Sprint(str)
}
// Sprintf formats according to a format specifier and returns the resulting string.
func (p PrefixPrinter) Sprintf(format string, a ...interface{}) string {
if p.Debugger && !PrintDebugMessages {
return ""
}
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 PrefixPrinter) Sprintfln(format string, a ...interface{}) string {
if p.Debugger && !PrintDebugMessages {
return ""
}
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 *PrefixPrinter) Print(a ...interface{}) *TextPrinter {
tp := TextPrinter(p)
if p.Debugger && !PrintDebugMessages {
return &tp
}
Fprint(p.Writer, p.Sprint(a...))
checkFatal(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 *PrefixPrinter) Println(a ...interface{}) *TextPrinter {
tp := TextPrinter(p)
if p.Debugger && !PrintDebugMessages {
return &tp
}
Fprint(p.Writer, p.Sprintln(a...))
checkFatal(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 *PrefixPrinter) Printf(format string, a ...interface{}) *TextPrinter {
tp := TextPrinter(p)
if p.Debugger && !PrintDebugMessages {
return &tp
}
Fprint(p.Writer, p.Sprintf(format, a...))
checkFatal(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 *PrefixPrinter) Printfln(format string, a ...interface{}) *TextPrinter {
tp := TextPrinter(p)
if p.Debugger && !PrintDebugMessages {
return &tp
}
Fprint(p.Writer, p.Sprintfln(format, a...))
checkFatal(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.
//
// Note: Use WithFatal(true) or Fatal to panic after first non nil error.
func (p *PrefixPrinter) 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 *PrefixPrinter) 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
}
// GetFormattedPrefix returns the Prefix as a styled text string.
func (p PrefixPrinter) GetFormattedPrefix() string {
return p.Prefix.Style.Sprint(" " + p.Prefix.Text + " ")
}
// Prefix contains the data used as the beginning of a printed text via a PrefixPrinter.
type Prefix struct {
Text string
Style *Style
}
// Scope contains the data of the optional scope of a prefix.
// If it has a text, it will be printed after the Prefix in brackets.
type Scope struct {
Text string
Style *Style
}
func checkFatal(p *PrefixPrinter) {
if p.Fatal {
panic("")
}
}