1
|
|
|
package main |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"fmt" |
5
|
|
|
"io" |
6
|
|
|
"os" |
7
|
|
|
|
8
|
|
|
"github.com/fatih/color" |
9
|
|
|
"github.com/mattn/go-colorable" |
10
|
|
|
"github.com/mattn/go-isatty" |
11
|
|
|
) |
12
|
|
|
|
13
|
|
|
var ( |
14
|
|
|
Stdout = colorable.NewColorableStdout() |
|
|
|
|
15
|
|
|
Stderr = colorable.NewColorableStderr() |
16
|
|
|
Default UI = Console{Stdout: Stdout, Stderr: Stderr} |
17
|
|
|
) |
18
|
|
|
|
19
|
|
|
type UI interface { |
|
|
|
|
20
|
|
|
Printf(format string, a ...interface{}) (n int, err error) |
21
|
|
|
Println(a ...interface{}) (n int, err error) |
22
|
|
|
Errorf(format string, a ...interface{}) (n int, err error) |
23
|
|
|
Errorln(a ...interface{}) (n int, err error) |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
func Printf(format string, a ...interface{}) (n int, err error) { |
|
|
|
|
27
|
|
|
return Default.Printf(color.CyanString("INFO: ")+format, a...) |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
func Errorf(format string, a ...interface{}) (n int, err error) { |
|
|
|
|
31
|
|
|
return Default.Errorf(color.RedString("ERROR: ")+format, a...) |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
func Warnf(format string, a ...interface{}) (n int, err error) { |
|
|
|
|
35
|
|
|
return Default.Errorf(color.YellowString("WARN: ")+format, a...) |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
func Errorln(a ...interface{}) (n int, err error) { |
|
|
|
|
39
|
|
|
return Default.Errorln(a...) |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
func IsTerminal(f *os.File) bool { |
|
|
|
|
43
|
|
|
return isatty.IsTerminal(f.Fd()) |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
type Console struct { |
|
|
|
|
47
|
|
|
Stdout io.Writer |
48
|
|
|
Stderr io.Writer |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
func (c Console) Printf(format string, a ...interface{}) (n int, err error) { |
|
|
|
|
52
|
|
|
return fmt.Fprintf(c.Stdout, format, a...) |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
func (c Console) Println(a ...interface{}) (n int, err error) { |
|
|
|
|
56
|
|
|
return fmt.Fprintln(c.Stdout, a...) |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
func (c Console) Errorf(format string, a ...interface{}) (n int, err error) { |
|
|
|
|
60
|
|
|
return fmt.Fprintf(c.Stderr, format, a...) |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
func (c Console) Errorln(a ...interface{}) (n int, err error) { |
|
|
|
|
64
|
|
|
return fmt.Fprintln(c.Stderr, a...) |
65
|
|
|
} |
66
|
|
|
|