-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput.go
More file actions
273 lines (230 loc) · 6.98 KB
/
output.go
File metadata and controls
273 lines (230 loc) · 6.98 KB
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
package cliout
import (
"fmt"
"io"
"os"
)
const defaultPrefix = "»"
// Output holds all configuration for CLI output rendering.
type Output struct {
writer io.Writer
level Level
prefix string
hasPrefix bool
prefixColor Color
messageColor Color
theme Theme
colorEnabled bool
noColorEnv bool // true when NO_COLOR was detected at construction time
exitFunc func(int) // called by Fatal/Fatalf; defaults to os.Exit
}
// New creates an Output with sensible defaults:
// - Writer: os.Stdout
// - Level: LevelInfo
// - Prefix: "»" (or the value of the CLI_PREFIX environment variable)
// - Theme: ThemeDefault (or the theme named by the CLI_THEME environment variable)
// - Color: auto-detected (disabled if NO_COLOR is set or stdout is not a TTY)
func New() *Output {
theme := ThemeDefault
if name, ok := os.LookupEnv("CLI_THEME"); ok && name != "" {
if t, found := ThemeByName(name); found {
theme = t
}
}
prefix := defaultPrefix
hasPrefix := true
if v, ok := os.LookupEnv("CLI_PREFIX"); ok {
prefix = v
if v == "" {
hasPrefix = false
}
}
o := &Output{
writer: os.Stdout,
level: LevelInfo,
prefix: prefix,
hasPrefix: hasPrefix,
theme: theme,
colorEnabled: true,
exitFunc: os.Exit,
}
// Respect NO_COLOR environment variable.
// See https://no-color.org/
if _, ok := os.LookupEnv("NO_COLOR"); ok {
o.colorEnabled = false
o.noColorEnv = true
}
// Disable color if stdout is not a terminal.
if f, ok := o.writer.(*os.File); ok {
if !isTerminal(f) {
o.colorEnabled = false
}
}
return o
}
// isTerminal reports whether f is a terminal (character device).
func isTerminal(f *os.File) bool {
fi, err := f.Stat()
if err != nil {
return false
}
return (fi.Mode() & os.ModeCharDevice) != 0
}
// --- Configuration methods ---
// SetLevel sets the minimum output level. Messages below this level are suppressed.
func (o *Output) SetLevel(l Level) {
o.level = l
}
// SetPrefix sets the prefix string prepended to each output line.
func (o *Output) SetPrefix(p string) {
o.prefix = p
o.hasPrefix = true
}
// ClearPrefix removes the prefix from output lines.
func (o *Output) ClearPrefix() {
o.prefix = ""
o.hasPrefix = false
}
// SetPrefixColor sets the color of the prefix, overriding the theme's prefix color.
func (o *Output) SetPrefixColor(c Color) {
o.prefixColor = c
}
// SetMessageColor sets the color for all messages, overriding per-level theme colors.
func (o *Output) SetMessageColor(c Color) {
o.messageColor = c
}
// SetTheme sets the color theme. This affects prefix and per-level message colors
// unless explicitly overridden with SetPrefixColor or SetMessageColor.
func (o *Output) SetTheme(t Theme) {
o.theme = t
}
// SetWriter sets the output destination.
func (o *Output) SetWriter(w io.Writer) {
o.writer = w
}
// SetColorEnabled explicitly enables or disables color output.
// If the NO_COLOR environment variable was set when this Output was created,
// color remains disabled regardless of the value passed here.
// See https://no-color.org/
func (o *Output) SetColorEnabled(enabled bool) {
if o.noColorEnv {
return
}
o.colorEnabled = enabled
}
// --- Output methods ---
// Info prints an info-level message.
func (o *Output) Info(msg string) {
o.print(LevelInfo, msg, false)
}
// Infof prints a formatted info-level message.
func (o *Output) Infof(format string, a ...any) {
o.print(LevelInfo, fmt.Sprintf(format, a...), false)
}
// Debug prints a debug-level message.
func (o *Output) Debug(msg string) {
o.print(LevelDebug, msg, false)
}
// Debugf prints a formatted debug-level message.
func (o *Output) Debugf(format string, a ...any) {
o.print(LevelDebug, fmt.Sprintf(format, a...), false)
}
// Trace prints a trace-level message.
func (o *Output) Trace(msg string) {
o.print(LevelTrace, msg, false)
}
// Tracef prints a formatted trace-level message.
func (o *Output) Tracef(format string, a ...any) {
o.print(LevelTrace, fmt.Sprintf(format, a...), false)
}
// Warn prints a warn-level message.
func (o *Output) Warn(msg string) {
o.print(LevelWarn, msg, false)
}
// Warnf prints a formatted warn-level message.
func (o *Output) Warnf(format string, a ...any) {
o.print(LevelWarn, fmt.Sprintf(format, a...), false)
}
// Error prints an error-level message.
func (o *Output) Error(msg string) {
o.print(LevelError, msg, false)
}
// Errorf prints a formatted error-level message.
func (o *Output) Errorf(format string, a ...any) {
o.print(LevelError, fmt.Sprintf(format, a...), false)
}
// Fatal prints an error-level message and then exits with code 1.
func (o *Output) Fatal(msg string) {
o.print(LevelError, msg, false)
o.exitFunc(1)
}
// Fatalf prints a formatted error-level message and then exits with code 1.
func (o *Output) Fatalf(format string, a ...any) {
o.print(LevelError, fmt.Sprintf(format, a...), false)
o.exitFunc(1)
}
// Success prints a success message at info level, using the theme's SuccessColor.
func (o *Output) Success(msg string) {
o.print(LevelInfo, msg, true)
}
// Successf prints a formatted success message at info level.
func (o *Output) Successf(format string, a ...any) {
o.print(LevelInfo, fmt.Sprintf(format, a...), true)
}
// --- Inline color helpers ---
// Colorize wraps text with the given color's ANSI escape codes. The result
// respects this Output's color-enabled setting: when color is disabled the
// text is returned unchanged. Use this to compose multi-color messages with
// Infof, Errorf, etc.
func (o *Output) Colorize(text string, c Color) string {
return c.apply(text, o.colorEnabled)
}
// --- Internal rendering ---
// print is the core rendering method. It handles level filtering, color application,
// prefix rendering, and writing the final output line.
func (o *Output) print(level Level, msg string, isSuccess bool) {
if level < o.level {
return
}
// Determine message color: explicit override > success color > theme per-level color.
var msgColor Color
if !o.messageColor.isDefault() {
msgColor = o.messageColor
} else if isSuccess {
msgColor = o.theme.SuccessColor
} else {
msgColor = o.colorForLevel(level)
}
// Determine prefix color: explicit override > theme prefix color.
prefixColor := o.theme.PrefixColor
if !o.prefixColor.isDefault() {
prefixColor = o.prefixColor
}
// Build the output line.
var line string
if o.hasPrefix && o.prefix != "" {
coloredPrefix := prefixColor.apply(o.prefix, o.colorEnabled)
coloredMsg := msgColor.apply(msg, o.colorEnabled)
line = coloredPrefix + " " + coloredMsg
} else {
line = msgColor.apply(msg, o.colorEnabled)
}
_, _ = fmt.Fprintln(o.writer, line)
}
// colorForLevel returns the theme color for a given output level.
func (o *Output) colorForLevel(level Level) Color {
switch level {
case LevelTrace:
return o.theme.TraceColor
case LevelDebug:
return o.theme.DebugColor
case LevelInfo:
return o.theme.InfoColor
case LevelWarn:
return o.theme.WarnColor
case LevelError:
return o.theme.ErrorColor
default:
return o.theme.InfoColor
}
}