-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.go
204 lines (166 loc) · 5.34 KB
/
write.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
// Copyright 2024 Collin Kreklow
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package cli
import (
"bytes"
"fmt"
"io"
"os"
"sync"
"sync/atomic"
"github.com/mattn/go-isatty"
)
// lockingWriter is a simple mutex-protected writer.
type lockingWriter struct {
m sync.Mutex
w io.Writer
}
// Write passes the provided data to the embedded io.Writer.
func (lw *lockingWriter) Write(b []byte) (n int, err error) {
lw.m.Lock()
n, err = lw.w.Write(b)
lw.m.Unlock()
return
}
// TermPrinter provides printing functions based on a standard unix
// terminal. The Print* functions direct output to os.Stdout, while the
// Eprint* functions direct output to os.Stderr.
//
// TermPrinter also includes a "live printing" function in Lprintf which
// provides a non-scrolling output for continuously-updating status
// information. Messages printed via Print* or Eprint* will not be
// overwritten by Lprintf.
//
// TermPrinter provides locking over the output writers, so it is safe
// to call concurrently from multiple goroutines.
//
// If TermPrinter is not created with NewTermPrinter, SetStdout and
// SetStderr must be called before use.
type TermPrinter struct {
livecount uint32
outIsTerm bool
errIsTerm bool
out io.Writer
err io.Writer
livebuf bytes.Buffer
}
// NewTermPrinter returns a TermPrinter set to output to os.Stdout and
// os.Stderr.
func NewTermPrinter() *TermPrinter {
return &TermPrinter{
out: &lockingWriter{w: os.Stdout},
err: &lockingWriter{w: os.Stderr},
}
}
// SetStdout sets the destination for calls to Print, Printf, Println
// and Lprintf.
func (tp *TermPrinter) SetStdout(w io.Writer) {
tp.out = &lockingWriter{w: w}
tp.outIsTerm = false
if f, ok := w.(*os.File); ok {
tp.outIsTerm = isatty.IsTerminal(f.Fd())
}
}
// SetStderr sets the destination for calls to EPrint, EPrintf and
// EPrintln.
func (tp *TermPrinter) SetStderr(w io.Writer) {
tp.err = &lockingWriter{w: w}
tp.errIsTerm = false
if f, ok := w.(*os.File); ok {
tp.errIsTerm = isatty.IsTerminal(f.Fd())
}
}
// Print operates in the manner of fmt.Print, writing to Stdout.
func (tp *TermPrinter) Print(v ...interface{}) (int, error) {
if tp.outIsTerm {
tp.resetLiveLines()
}
return fmt.Fprint(tp.out, v...)
}
// Printf operates in the manner of fmt.Printf, writing to Stdout.
func (tp *TermPrinter) Printf(f string, v ...interface{}) (int, error) {
if tp.outIsTerm {
tp.resetLiveLines()
}
return fmt.Fprintf(tp.out, f, v...)
}
// Println operates in the manner of fmt.Println, writing to Stdout.
func (tp *TermPrinter) Println(v ...interface{}) (int, error) {
if tp.outIsTerm {
tp.resetLiveLines()
}
return fmt.Fprintln(tp.out, v...)
}
// Lprintf implements a "live update" version of fmt.Printf. If Stdout
// appears to be a terminal, the previously output line(s) will be
// cleared before the new line(s) are written.
//
// While Lprintf is safe for concurrent use with Print* and Eprint*,
// concurrent use of Lprintf will conflict, overwriting the previous
// output.
func (tp *TermPrinter) Lprintf(f string, v ...interface{}) (int, error) {
if !tp.outIsTerm {
return fmt.Fprintf(tp.out, f, v...)
}
tp.clearLiveLines()
tp.livebuf.Reset()
fmt.Fprintf(&tp.livebuf, f, v...)
b := tp.livebuf.Bytes()
atomic.StoreUint32(&tp.livecount, uint32(bytes.Count(b, []byte{'\n'})))
return tp.out.Write(b)
}
// Eprint operates in the manner of fmt.Print, writing to Stderr.
func (tp *TermPrinter) Eprint(v ...interface{}) (int, error) {
if tp.errIsTerm {
tp.resetLiveLines()
}
return fmt.Fprint(tp.err, v...)
}
// Eprintf operates in the manner of fmt.Printf, writing to Stderr.
func (tp *TermPrinter) Eprintf(f string, v ...interface{}) (int, error) {
if tp.errIsTerm {
tp.resetLiveLines()
}
return fmt.Fprintf(tp.err, f, v...)
}
// Eprintln operates in the manner of fmt.Println, writing to Stderr.
func (tp *TermPrinter) Eprintln(v ...interface{}) (int, error) {
if tp.errIsTerm {
tp.resetLiveLines()
}
return fmt.Fprintln(tp.err, v...)
}
func (tp *TermPrinter) resetLiveLines() {
atomic.StoreUint32(&tp.livecount, 0)
}
//nolint:gochecknoglobals // improves performance of clearLiveLines
var clearline = []byte("\x1b[1A\x1b[2K")
func (tp *TermPrinter) clearLiveLines() {
ll := atomic.LoadUint32(&tp.livecount)
for l := uint32(0); l < ll; l++ {
_, err := tp.out.Write(clearline)
if err != nil {
panic(err)
}
}
tp.resetLiveLines()
}