forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tee_printer.go
92 lines (76 loc) · 2.06 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
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
package terminal
import (
"fmt"
)
type Printer interface {
Print(a ...interface{}) (n int, err error)
Printf(format string, a ...interface{}) (n int, err error)
Println(a ...interface{}) (n int, err error)
ForcePrint(a ...interface{}) (n int, err error)
ForcePrintf(format string, a ...interface{}) (n int, err error)
ForcePrintln(a ...interface{}) (n int, err error)
}
type OutputCapture interface {
SetOutputBucket(*[]string)
}
type TerminalOutputSwitch interface {
DisableTerminalOutput(bool)
}
type TeePrinter struct {
disableTerminalOutput bool
outputBucket *[]string
}
func NewTeePrinter() *TeePrinter {
return &TeePrinter{}
}
func (t *TeePrinter) SetOutputBucket(bucket *[]string) {
t.outputBucket = bucket
}
func (t *TeePrinter) Print(values ...interface{}) (n int, err error) {
str := fmt.Sprint(values...)
t.saveOutputToBucket(str)
if !t.disableTerminalOutput {
return fmt.Print(str)
}
return
}
func (t *TeePrinter) Printf(format string, a ...interface{}) (n int, err error) {
str := fmt.Sprintf(format, a...)
t.saveOutputToBucket(str)
if !t.disableTerminalOutput {
return fmt.Print(str)
}
return
}
func (t *TeePrinter) Println(values ...interface{}) (n int, err error) {
str := fmt.Sprint(values...)
t.saveOutputToBucket(str)
if !t.disableTerminalOutput {
return fmt.Println(str)
}
return
}
func (t *TeePrinter) ForcePrint(values ...interface{}) (n int, err error) {
str := fmt.Sprint(values...)
t.saveOutputToBucket(str)
return fmt.Print(str)
}
func (t *TeePrinter) ForcePrintf(format string, a ...interface{}) (n int, err error) {
str := fmt.Sprintf(format, a...)
t.saveOutputToBucket(str)
return fmt.Print(str)
}
func (t *TeePrinter) ForcePrintln(values ...interface{}) (n int, err error) {
str := fmt.Sprint(values...)
t.saveOutputToBucket(str)
return fmt.Println(str)
}
func (t *TeePrinter) DisableTerminalOutput(disable bool) {
t.disableTerminalOutput = disable
}
func (t *TeePrinter) saveOutputToBucket(output string) {
if t.outputBucket == nil {
return
}
*t.outputBucket = append(*t.outputBucket, Decolorize(output))
}