-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
98 lines (82 loc) · 2.18 KB
/
ui.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
package ui
import (
"fmt"
"io"
"os"
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
)
type UI interface {
Print(a ...interface{}) (n int, err error)
Printf(format string, a ...interface{}) (n int, err error)
Println(a ...interface{}) (n int, err error)
Errorf(format string, a ...interface{}) (n int, err error)
Errorln(a ...interface{}) (n int, err error)
}
var (
Stdout = colorable.NewColorableStdout()
Stderr = colorable.NewColorableStderr()
Default UI = Console{Stdout: Stdout, Stderr: Stderr}
)
func Print(a ...interface{}) (n int) {
n, err := Default.Print(a...)
if err != nil {
// If something as basic as printing to stdout fails, just panic and exit
os.Exit(1)
}
return
}
func Printf(format string, a ...interface{}) (n int) {
n, err := Default.Printf(format, a...)
if err != nil {
// If something as basic as printing to stdout fails, just panic and exit
os.Exit(1)
}
return
}
func Println(a ...interface{}) (n int) {
n, err := Default.Println(a...)
if err != nil {
// If something as basic as printing to stdout fails, just panic and exit
os.Exit(1)
}
return
}
func Errorf(format string, a ...interface{}) (n int) {
n, err := Default.Errorf(format, a...)
if err != nil {
// If something as basic as printing to stderr fails, just panic and exit
os.Exit(1)
}
return
}
func Errorln(a ...interface{}) (n int) {
n, err := Default.Errorln(a...)
if err != nil {
// If something as basic as printing to stderr fails, just panic and exit
os.Exit(1)
}
return
}
func IsTerminal(f *os.File) bool {
return isatty.IsTerminal(f.Fd())
}
type Console struct {
Stdout io.Writer
Stderr io.Writer
}
func (c Console) Print(a ...interface{}) (n int, err error) {
return fmt.Fprint(c.Stdout, a...)
}
func (c Console) Printf(format string, a ...interface{}) (n int, err error) {
return fmt.Fprintf(c.Stdout, format, a...)
}
func (c Console) Println(a ...interface{}) (n int, err error) {
return fmt.Fprintln(c.Stdout, a...)
}
func (c Console) Errorf(format string, a ...interface{}) (n int, err error) {
return fmt.Fprintf(c.Stderr, format, a...)
}
func (c Console) Errorln(a ...interface{}) (n int, err error) {
return fmt.Fprintln(c.Stderr, a...)
}