-
Notifications
You must be signed in to change notification settings - Fork 3
/
line.go
131 lines (105 loc) · 3.19 KB
/
line.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
package line
import (
"fmt"
"io"
"sync"
)
// Line is an easy way to stylize terminal output
type Line struct {
prefix string
suffix string
last rune
formatter Formatter
w io.Writer
mu sync.Mutex
}
// New creates a new instance of Line
func New(out io.Writer, prefix string, suffix string, formatter Formatter) *Line {
return &Line{w: out, prefix: prefix, suffix: suffix, formatter: formatter, last: '\n'} // last was a newline so that the prefix will print on the first line
}
// Prefix sets the prefix for the returned Output
func (l *Line) Prefix(prefix string) *Output {
return NewOutput(l, prefix, l.suffix, l.formatter)
}
// Suffix sets the suffix for the returned Output
func (l *Line) Suffix(suffix string) *Output {
return NewOutput(l, l.prefix, suffix, l.formatter)
}
// Format sets the Format for the returned Output
func (l *Line) Format(f Formatter) *Output {
return NewOutput(l, l.prefix, l.suffix, f)
}
// Print prints and returns an Output allowing for chaining
func (l *Line) Print(a ...interface{}) *Output {
return NewOutput(l, l.prefix, l.suffix, l.formatter).Print(a...)
}
// Println prints the arguments with a new line
func (l *Line) Println(a ...interface{}) *Output {
return NewOutput(l, l.prefix, l.suffix, l.formatter).Println(a...)
}
// Printf prints and returns an Output allowing for chaining
func (l *Line) Printf(format string, a ...interface{}) *Output {
return NewOutput(l, l.prefix, l.suffix, l.formatter).Printf(format, a...)
}
// Info prints using formatting suitable for an info message
func (l *Line) Info(a ...interface{}) *Output {
return l.Yellow().Print(a...)
}
// Progress prints with an "-->" prefix
func (l *Line) Progress(a ...interface{}) *Output {
return l.Prefix("--> ").Print(a...)
}
// Error prints using formatting suitable for an error message
func (l *Line) Error(a ...interface{}) *Output {
return l.Red().Print(a...)
}
// Black prints black text
func (l *Line) Black(a ...interface{}) *Output {
return l.Format(BlackColor).Print(a...)
}
// Red prints red text
func (l *Line) Red(a ...interface{}) *Output {
return l.Format(RedColor).Print(a...)
}
// Green prints green text
func (l *Line) Green(a ...interface{}) *Output {
return l.Format(GreenColor).Print(a...)
}
// Yellow prints yellow text
func (l *Line) Yellow(a ...interface{}) *Output {
return l.Format(YellowColor).Print(a...)
}
// Blue prints blue text
func (l *Line) Blue(a ...interface{}) *Output {
return l.Format(BlueColor).Print(a...)
}
// Magenta prints magenta text
func (l *Line) Magenta(a ...interface{}) *Output {
return l.Format(MagentaColor).Print(a...)
}
// Cyan prints cyan text
func (l *Line) Cyan(a ...interface{}) *Output {
return l.Format(CyanColor).Print(a...)
}
// White prints white text
func (l *Line) White(a ...interface{}) *Output {
return l.Format(WhiteColor).Print(a...)
}
func (l *Line) doFprint(formatter Formatter, out string) (int, error) {
l.mu.Lock()
defer l.mu.Unlock()
print := out
if formatter != nil {
print = formatter.Sprint(print)
}
c, err := fmt.Fprint(l.w, print)
if len(out) > 0 {
l.last = rune(out[len(out)-1])
}
return c, err
}
func (l *Line) getLast() rune {
l.mu.Lock()
defer l.mu.Unlock()
return l.last
}