-
Notifications
You must be signed in to change notification settings - Fork 3
/
output.go
164 lines (133 loc) · 4.15 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
package line
import (
"fmt"
"strings"
)
// Output represents an output
type Output struct {
l *Line
prefix string
suffix string
formatter Formatter
err error
n int
}
// Err is the error that occurred in the Print call that produced this output
func (o *Output) Err() error {
return o.err
}
// N number of bytes written on the Print call that produced this output
func (o *Output) N() int {
return o.n
}
// NewOutput creates a new output
func NewOutput(l *Line, prefix, suffix string, formatter Formatter) *Output {
return &Output{l: l, prefix: prefix, suffix: suffix, formatter: formatter}
}
// Prefix sets the prefix for the returned Output
func (o *Output) Prefix(prefix string) *Output {
return NewOutput(o.l, prefix, o.suffix, o.formatter)
}
// Suffix sets the suffix for the returned Output
func (o *Output) Suffix(suffix string) *Output {
return NewOutput(o.l, o.prefix, suffix, o.formatter)
}
// Format sets the Format for the returned Output
func (o *Output) Format(f Formatter) *Output {
return NewOutput(o.l, o.prefix, o.suffix, f)
}
// Print prints and returns an Output allowing for chaining
func (o *Output) Print(a ...interface{}) *Output {
n, err := o.doPrint(a...)
output := &Output{l: o.l, prefix: o.prefix, suffix: o.suffix, formatter: o.formatter, err: err, n: n}
return output
}
// Println prints the arguments with a new line
func (o *Output) Println(a ...interface{}) *Output {
a = append(a, "\n")
n, err := o.doPrint(a...)
output := &Output{l: o.l, prefix: o.prefix, suffix: o.suffix, formatter: o.formatter, err: err, n: n}
return output
}
// Printf prints and returns an Output allowing for chaining
func (o *Output) Printf(format string, a ...interface{}) *Output {
n, err := o.doPrint(fmt.Sprintf(format, a...))
output := &Output{l: o.l, prefix: o.prefix, suffix: o.suffix, formatter: o.formatter, err: err, n: n}
return output
}
// Info prints using formatting suitable for an info message
func (o *Output) Info(a ...interface{}) *Output {
return o.Yellow().Print(a...)
}
// Progress prints with an "-->" prefix
func (o *Output) Progress(a ...interface{}) *Output {
return o.Prefix("--> ").Print(a...)
}
// Error prints using formatting suitable for an error message
func (o *Output) Error(a ...interface{}) *Output {
return o.Red().Print(a...)
}
// Black prints black text
func (o *Output) Black(a ...interface{}) *Output {
return o.Format(BlackColor).Print(a...)
}
// Red prints red text
func (o *Output) Red(a ...interface{}) *Output {
return o.Format(RedColor).Print(a...)
}
// Green prints green text
func (o *Output) Green(a ...interface{}) *Output {
return o.Format(GreenColor).Print(a...)
}
// Yellow prints yellow text
func (o *Output) Yellow(a ...interface{}) *Output {
return o.Format(YellowColor).Print(a...)
}
// Blue prints blue text
func (o *Output) Blue(a ...interface{}) *Output {
return o.Format(BlueColor).Print(a...)
}
// Magenta prints magenta text
func (o *Output) Magenta(a ...interface{}) *Output {
return o.Format(MagentaColor).Print(a...)
}
// Cyan prints cyan text
func (o *Output) Cyan(a ...interface{}) *Output {
return o.Format(CyanColor).Print(a...)
}
// White prints white text
func (o *Output) White(a ...interface{}) *Output {
return o.Format(WhiteColor).Print(a...)
}
func (o *Output) doPrint(a ...interface{}) (int, error) {
out := fmt.Sprint(a...)
if len(out) == 0 {
return 0, nil
}
skipFirst := o.l.getLast() != '\n'
skipLast := out[len(out)-1] == '\n'
out = o.insertFixes(skipFirst, skipLast, out)
return o.l.doFprint(o.formatter, out)
}
func (o *Output) insertFixes(skipFirstPrefix bool, skipLastPrefix bool, output string) string {
lines := strings.Split(output, "\n")
if len(lines) == 1 { // no newlines
if skipFirstPrefix {
return output
}
return fmt.Sprintf("%v%v", o.prefix, output)
}
fixes := []string{}
for y, line := range lines {
out := ""
if (y == 0 && !skipFirstPrefix) || (y == len(lines)-1 && !skipLastPrefix) || (y > 0 && y < len(lines)-1) {
out = o.prefix
}
out = fmt.Sprintf("%s%s", out, line)
if y != len(lines)-1 {
out = fmt.Sprintf("%s%s", out, o.suffix)
}
fixes = append(fixes, out)
}
return strings.Join(fixes, "\n")
}