-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint.go
43 lines (34 loc) · 849 Bytes
/
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
package console
import (
"fmt"
"strings"
"golang.org/x/term"
)
const defaultCols = 80
// Flag enabled if `-no-color` option is used.
var NoColor = false
// Number of columns detected from Stdin or 80 by default.
var Cols = getCols()
var previousWasCR = false
// Print string to the console with space padding so progress indicator might be overriden.
func Print(msg string) {
if previousWasCR {
fmt.Print(strings.Repeat(" ", Cols-1), "\r")
previousWasCR = false
}
fmt.Print(msg)
if strings.HasSuffix(msg, "\r") {
previousWasCR = true
}
}
// Printf string to the console with space padding so progress indicator might be overriden.
func Printf(format string, a ...interface{}) {
Print(fmt.Sprintf(format, a...))
}
func getCols() int {
width, _, err := term.GetSize(0)
if err != nil {
return defaultCols
}
return width
}