-
Notifications
You must be signed in to change notification settings - Fork 929
/
tee_printer.go
63 lines (53 loc) · 1.27 KB
/
tee_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
package terminal
import (
"fmt"
"io"
"io/ioutil"
)
type TeePrinter struct {
disableTerminalOutput bool
outputBucket io.Writer
stdout io.Writer
}
func NewTeePrinter(w io.Writer) *TeePrinter {
return &TeePrinter{
outputBucket: ioutil.Discard,
stdout: w,
}
}
func (t *TeePrinter) SetOutputBucket(bucket io.Writer) {
if bucket == nil {
bucket = ioutil.Discard
}
t.outputBucket = bucket
}
func (t *TeePrinter) Print(values ...interface{}) (int, error) {
str := fmt.Sprint(values...)
t.saveOutputToBucket(str)
if !t.disableTerminalOutput {
return fmt.Fprint(t.stdout, str)
}
return 0, nil
}
func (t *TeePrinter) Printf(format string, a ...interface{}) (int, error) {
str := fmt.Sprintf(format, a...)
t.saveOutputToBucket(str)
if !t.disableTerminalOutput {
return fmt.Fprint(t.stdout, str)
}
return 0, nil
}
func (t *TeePrinter) Println(values ...interface{}) (int, error) {
str := fmt.Sprint(values...)
t.saveOutputToBucket(str)
if !t.disableTerminalOutput {
return fmt.Fprintln(t.stdout, str)
}
return 0, nil
}
func (t *TeePrinter) DisableTerminalOutput(disable bool) {
t.disableTerminalOutput = disable
}
func (t *TeePrinter) saveOutputToBucket(output string) {
_, _ = t.outputBucket.Write([]byte(Decolorize(output)))
}