forked from pterm/pterm
/
print.go
203 lines (175 loc) · 5.53 KB
/
print.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
package pterm
import (
"fmt"
"io"
"strings"
"sync"
"github.com/gookit/color"
)
// Need to use this because "github.com/gookit/color" is NOT a thread-safe library for Print & Sprintf functions.
// Used to protect against some unsafe actions in Fprint as well
var pLock sync.RWMutex
// SetDefaultOutput sets the default output of pterm.
func SetDefaultOutput(w io.Writer) {
pLock.Lock()
defer pLock.Unlock()
color.SetOutput(w)
}
// Sprint formats using the default formats for its operands and returns the resulting string.
// Spaces are added between operands when neither is a string.
func Sprint(a ...interface{}) string {
pLock.Lock()
defer pLock.Unlock()
return color.Sprint(a...)
}
// Sprintf formats according to a format specifier and returns the resulting string.
func Sprintf(format string, a ...interface{}) string {
pLock.Lock()
defer pLock.Unlock()
return color.Sprintf(format, a...)
}
// Sprintfln formats according to a format specifier and returns the resulting string.
// Spaces are always added between operands and a newline is appended.
func Sprintfln(format string, a ...interface{}) string {
pLock.Lock()
defer pLock.Unlock()
return color.Sprintf(format, a...) + "\n"
}
// Sprintln returns what Println would print to the terminal.
func Sprintln(a ...interface{}) string {
str := fmt.Sprintln(a...)
return Sprint(str)
}
// Sprinto returns what Printo would print.
func Sprinto(a ...interface{}) string {
return "\r" + Sprint(a...)
}
// Print formats using the default formats for its operands and writes to standard output.
// Spaces are added between operands when neither is a string.
// It returns the number of bytes written and any write error encountered.
func Print(a ...interface{}) {
Fprint(nil, a...)
}
// Println formats using the default formats for its operands and writes to standard output.
// Spaces are always added between operands and a newline is appended.
// It returns the number of bytes written and any write error encountered.
func Println(a ...interface{}) {
Print(Sprintln(a...))
}
// Printf formats according to a format specifier and writes to standard output.
// It returns the number of bytes written and any write error encountered.
func Printf(format string, a ...interface{}) {
Print(Sprintf(format, a...))
}
// Printfln formats according to a format specifier and writes to standard output.
// Spaces are always added between operands and a newline is appended.
// It returns the number of bytes written and any write error encountered.
func Printfln(format string, a ...interface{}) {
Print(Sprintfln(format, a...))
}
// PrintOnError prints every error which is not nil.
// If every error is nil, nothing will be printed.
// This can be used for simple error checking.
func PrintOnError(a ...interface{}) {
for _, arg := range a {
if err, ok := arg.(error); ok {
if err != nil {
Println(err)
}
}
}
}
// PrintOnErrorf wraps every error which is not nil and prints it.
// If every error is nil, nothing will be printed.
// This can be used for simple error checking.
func PrintOnErrorf(format string, a ...interface{}) {
for _, arg := range a {
if err, ok := arg.(error); ok {
if err != nil {
Println(fmt.Errorf(format, err))
}
}
}
}
// Fprint formats using the default formats for its operands and writes to w.
// Spaces are added between operands when neither is a string.
// It returns the number of bytes written and any write error encountered.
func Fprint(writer io.Writer, a ...interface{}) {
pLock.Lock()
defer pLock.Unlock()
if !Output.Load() {
return
}
var ret string
var printed bool
activeProgressBarPrinters.lock.Lock()
for _, bar := range activeProgressBarPrinters.printers {
if bar.IsActive && bar.Writer == writer {
ret += sClearLine()
ret += "\r" + color.Sprint(a...)
printed = true
}
}
activeProgressBarPrinters.lock.Unlock()
activeSpinnerPrinters.lock.Lock()
for _, spinner := range activeSpinnerPrinters.printers {
if spinner.atomicIsActive.Load() && spinner.Writer == writer {
ret += sClearLine()
ret += "\r" + color.Sprint(a...)
printed = true
}
}
activeSpinnerPrinters.lock.Unlock()
if !printed {
ret = color.Sprint(a...)
}
if writer != nil {
color.Fprint(writer, color.Sprint(ret))
} else {
color.Print(color.Sprint(ret))
}
}
// Fprintln formats using the default formats for its operands and writes to w.
// Spaces are always added between operands and a newline is appended.
// It returns the number of bytes written and any write error encountered.
func Fprintln(writer io.Writer, a ...interface{}) {
Fprint(writer, Sprint(a...)+"\n")
}
// Printo overrides the current line in a terminal.
// If the current line is empty, the text will be printed like with pterm.Print.
// Example:
//
// pterm.Printo("Hello, World")
// time.Sleep(time.Second)
// pterm.Printo("Hello, Earth!")
func Printo(a ...interface{}) {
pLock.Lock()
defer pLock.Unlock()
if !Output.Load() {
return
}
color.Print("\r" + color.Sprint(a...))
}
// Fprinto prints Printo to a custom writer.
func Fprinto(w io.Writer, a ...interface{}) {
pLock.Lock()
defer pLock.Unlock()
if !Output.Load() {
return
}
if w != nil {
color.Fprint(w, "\r", color.Sprint(a...))
} else {
color.Print("\r", color.Sprint(a...))
}
}
// RemoveColorFromString removes color codes from a string.
func RemoveColorFromString(a ...interface{}) string {
return color.ClearCode(Sprint(a...))
}
func fClearLine(writer io.Writer) {
Fprinto(writer, strings.Repeat(" ", GetTerminalWidth()))
}
func sClearLine() string {
return "\r" + color.Sprint(strings.Repeat(" ", GetTerminalWidth()))
}