-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
output.go
347 lines (286 loc) · 9.31 KB
/
output.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
// Package output provides types related to formatted terminal output.
package output
import (
"bytes"
"fmt"
"io"
"os"
"sync"
"github.com/charmbracelet/glamour"
glamouransi "github.com/charmbracelet/glamour/ansi"
"github.com/mattn/go-runewidth"
"golang.org/x/term"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
// Writer defines a common set of methods that can be used to output status
// information.
//
// Note that the *f methods can accept Style instances in their arguments with
// the %s format specifier: if given, the detected colour support will be
// respected when outputting.
type Writer interface {
// These methods only write the given message if verbose mode is enabled.
Verbose(s string)
Verbosef(format string, args ...any)
VerboseLine(line FancyLine)
// These methods write their messages unconditionally.
Write(s string)
Writef(format string, args ...any)
WriteLine(line FancyLine)
}
type Context interface {
Writer
Close()
}
// Output encapsulates a standard set of functionality for commands that need
// to output human-readable data.
//
// Output is not appropriate for machine-readable data, such as JSON.
type Output struct {
w io.Writer
caps capabilities
verbose bool
// Unsurprisingly, it would be bad if multiple goroutines wrote at the same
// time, so we have a basic mutex to guard against that.
lock sync.Mutex
}
var _ sync.Locker = &Output{}
type OutputOpts struct {
// ForceColor ignores all terminal detection and enabled coloured output.
ForceColor bool
// ForceTTY ignores all terminal detection and enables TTY output.
ForceTTY bool
// ForceHeight ignores all terminal detection and sets the height to this value.
ForceHeight int
// ForceWidth ignores all terminal detection and sets the width to this value.
ForceWidth int
// ForceDarkBackground ignores all terminal detection and sets whether the terminal
// background is dark to this value.
//
// Workaround: For some reason the dark background detection hangs indefinitely in
// Buildkite, so ForceDarkBackground being set is a required (Buildkite output is
// always against a dark background anyway, so for the user this should be fine)
ForceDarkBackground bool
Verbose bool
}
type MarkdownStyleOpts func(style *glamouransi.StyleConfig)
var MarkdownNoMargin MarkdownStyleOpts = func(style *glamouransi.StyleConfig) {
z := uint(0)
style.CodeBlock.Margin = &z
style.Document.Margin = &z
style.Document.BlockPrefix = ""
style.Document.BlockSuffix = ""
}
// newOutputPlatformQuirks provides a way for conditionally compiled code to
// hook into NewOutput to perform any required setup.
var newOutputPlatformQuirks func(o *Output) error
// newCapabilityWatcher returns a channel that receives a message when
// capabilities are updated. By default, no watching functionality is
// available.
var newCapabilityWatcher = func(opts OutputOpts) chan capabilities { return nil }
func NewOutput(w io.Writer, opts OutputOpts) *Output {
caps, detectionErr := detectCapabilities(opts)
o := &Output{caps: caps, verbose: opts.Verbose, w: w}
if newOutputPlatformQuirks != nil {
if err := newOutputPlatformQuirks(o); err != nil {
o.Verbosef("Error handling platform quirks: %v", err)
}
}
// If we got an error earlier, now is where we'll report it to the user.
if detectionErr != nil {
block := o.Block(Linef(EmojiWarning, StyleWarning, "An error was returned when detecting the terminal size and capabilities:"))
block.Write("")
block.Write(detectionErr.Error())
block.Write("")
block.Write("Execution will continue, but please report this, along with your operating")
block.Write("system, terminal, and any other details, to:")
block.Write(" https://github.com/sourcegraph/sourcegraph/issues/new")
block.Close()
}
// Set up a watcher so we can adjust the size of the output if the terminal
// is resized.
if c := newCapabilityWatcher(opts); c != nil {
go func() {
for caps := range c {
o.caps = caps
}
}()
}
return o
}
func (o *Output) Lock() {
o.lock.Lock()
// Hide the cursor while we update: this reduces the jitteriness of the
// whole thing, and some terminals are smart enough to make the update we're
// about to render atomic if the cursor is hidden for a short length of
// time.
o.w.Write([]byte("\033[?25l"))
}
func (o *Output) SetVerbose() {
o.lock.Lock()
defer o.lock.Unlock()
o.verbose = true
}
func (o *Output) UnsetVerbose() {
o.lock.Lock()
defer o.lock.Unlock()
o.verbose = false
}
func (o *Output) Unlock() {
// Show the cursor once more.
o.w.Write([]byte("\033[?25h"))
o.lock.Unlock()
}
func (o *Output) Verbose(s string) {
if o.verbose {
o.Write(s)
}
}
func (o *Output) Verbosef(format string, args ...any) {
if o.verbose {
o.Writef(format, args...)
}
}
func (o *Output) VerboseLine(line FancyLine) {
if o.verbose {
o.WriteLine(line)
}
}
func (o *Output) Write(s string) {
o.Lock()
defer o.Unlock()
fmt.Fprintln(o.w, s)
}
func (o *Output) Writef(format string, args ...any) {
o.Lock()
defer o.Unlock()
fmt.Fprintf(o.w, format, o.caps.formatArgs(args)...)
fmt.Fprint(o.w, "\n")
}
func (o *Output) WriteLine(line FancyLine) {
o.Lock()
defer o.Unlock()
line.write(o.w, o.caps)
}
// Block starts a new block context. This should not be invoked if there is an
// active Pending or Progress context.
func (o *Output) Block(summary FancyLine) *Block {
o.WriteLine(summary)
return newBlock(runewidth.StringWidth(summary.emoji)+1, o)
}
// Pending sets up a new pending context. This should not be invoked if there
// is an active Block or Progress context. The emoji in the message will be
// ignored, as Pending will render its own spinner.
//
// A Pending instance must be disposed of via the Complete or Destroy methods.
func (o *Output) Pending(message FancyLine) Pending {
return newPending(message, o)
}
// Progress sets up a new progress bar context. This should not be invoked if
// there is an active Block or Pending context.
//
// A Progress instance must be disposed of via the Complete or Destroy methods.
func (o *Output) Progress(bars []ProgressBar, opts *ProgressOpts) Progress {
return newProgress(bars, o, opts)
}
// ProgressWithStatusBars sets up a new progress bar context with StatusBar
// contexts. This should not be invoked if there is an active Block or Pending
// context.
//
// A Progress instance must be disposed of via the Complete or Destroy methods.
func (o *Output) ProgressWithStatusBars(bars []ProgressBar, statusBars []*StatusBar, opts *ProgressOpts) ProgressWithStatusBars {
return newProgressWithStatusBars(bars, statusBars, o, opts)
}
type readWriter struct {
io.Reader
io.Writer
}
// PromptPassword tries to securely prompt a user for sensitive input.
func (o *Output) PromptPassword(input io.Reader, prompt FancyLine) (string, error) {
o.lock.Lock()
defer o.lock.Unlock()
// Render the prompt
prompt.Prompt = true
var promptText bytes.Buffer
prompt.write(&promptText, o.caps)
// If input is a file and terminal, read from it directly
if f, ok := input.(*os.File); ok {
fd := int(f.Fd())
if term.IsTerminal(fd) {
_, _ = o.w.Write(promptText.Bytes())
val, err := term.ReadPassword(fd)
_, _ = o.w.Write([]byte("\n")) // once we've read an input
return string(val), err
}
}
// Otherwise, create a terminal
t := term.NewTerminal(&readWriter{Reader: input, Writer: o.w}, "")
_ = t.SetSize(o.caps.Width, o.caps.Height)
return t.ReadPassword(promptText.String())
}
func MarkdownIndent(n uint) MarkdownStyleOpts {
return func(style *glamouransi.StyleConfig) {
style.Document.Indent = &n
}
}
// WriteCode renders the given code snippet as Markdown, unless color is disabled.
func (o *Output) WriteCode(languageName, str string) error {
return o.WriteMarkdown(fmt.Sprintf("```%s\n%s\n```", languageName, str), MarkdownNoMargin)
}
func (o *Output) WriteMarkdown(str string, opts ...MarkdownStyleOpts) error {
if !o.caps.Color {
o.Write(str)
return nil
}
var style glamouransi.StyleConfig
if o.caps.DarkBackground {
style = glamour.DarkStyleConfig
} else {
style = glamour.LightStyleConfig
}
for _, opt := range opts {
opt(&style)
}
r, err := glamour.NewTermRenderer(
// detect background color and pick either the default dark or light theme
glamour.WithStyles(style),
// wrap output at slightly less than terminal width
glamour.WithWordWrap(o.caps.Width*4/5),
glamour.WithEmoji(),
)
if err != nil {
return errors.Wrap(err, "renderer")
}
rendered, err := r.Render(str)
if err != nil {
return errors.Wrap(err, "render")
}
o.Write(rendered)
return nil
}
// The utility functions below do not make checks for whether the terminal is a
// TTY, and should only be invoked from behind appropriate guards.
func (o *Output) clearCurrentLine() {
fmt.Fprint(o.w, "\033[2K")
}
func (o *Output) moveDown(lines int) {
fmt.Fprintf(o.w, "\033[%dB", lines)
// Move the cursor to the leftmost column.
fmt.Fprintf(o.w, "\033[%dD", o.caps.Width+1)
}
func (o *Output) moveUp(lines int) {
fmt.Fprintf(o.w, "\033[%dA", lines)
// Move the cursor to the leftmost column.
fmt.Fprintf(o.w, "\033[%dD", o.caps.Width+1)
}
func (o *Output) MoveUpLines(lines int) {
o.moveUp(lines)
}
// writeStyle is a helper to write a style while respecting the terminal
// capabilities.
func (o *Output) writeStyle(style Style) {
fmt.Fprintf(o.w, "%s", o.caps.formatArgs([]any{style})...)
}
func (o *Output) ClearScreen() {
fmt.Fprintf(o.w, "\033c")
}