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() |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
15 | Stderr = colorable.NewColorableStderr() |
||
16 | Default UI = Console{Stdout: Stdout, Stderr: Stderr} |
||
17 | ) |
||
18 | |||
19 | // UI represents a simple IO output. |
||
20 | type UI interface { |
||
21 | Printf(format string, a ...interface{}) (n int, err error) |
||
22 | Println(a ...interface{}) (n int, err error) |
||
23 | Errorf(format string, a ...interface{}) (n int, err error) |
||
24 | Errorln(a ...interface{}) (n int, err error) |
||
25 | } |
||
26 | |||
27 | // Printf outputs information level logs |
||
28 | func Printf(format string, a ...interface{}) (n int, err error) { |
||
29 | return Default.Printf(color.CyanString("INFO: ")+format, a...) |
||
30 | } |
||
31 | |||
32 | // Errorf outputs error level logs |
||
33 | func Errorf(format string, a ...interface{}) (n int, err error) { |
||
34 | return Default.Errorf(color.RedString("ERROR: ")+format, a...) |
||
35 | } |
||
36 | |||
37 | // Warnf outputs warning level logs |
||
38 | func Warnf(format string, a ...interface{}) (n int, err error) { |
||
39 | return Default.Errorf(color.YellowString("WARN: ")+format, a...) |
||
40 | } |
||
41 | |||
42 | // Errorln is non formatted error printer. |
||
43 | func Errorln(a ...interface{}) (n int, err error) { |
||
44 | return Default.Errorln(a...) |
||
45 | } |
||
46 | |||
47 | // IsTerminal checks if we have tty |
||
48 | func IsTerminal(f *os.File) bool { |
||
49 | return isatty.IsTerminal(f.Fd()) |
||
50 | } |
||
51 | |||
52 | // Console is an implementation of UI interface |
||
53 | type Console struct { |
||
54 | Stdout io.Writer |
||
55 | Stderr io.Writer |
||
56 | } |
||
57 | |||
58 | // Printf prints formatted information logs to Stdout |
||
59 | func (c Console) Printf(format string, a ...interface{}) (n int, err error) { |
||
60 | return fmt.Fprintf(c.Stdout, format, a...) |
||
61 | } |
||
62 | |||
63 | // Println prints information logs to Stdout |
||
64 | func (c Console) Println(a ...interface{}) (n int, err error) { |
||
65 | return fmt.Fprintln(c.Stdout, a...) |
||
66 | } |
||
67 | |||
68 | // Errorf prints formatted error logs to Stderr |
||
69 | func (c Console) Errorf(format string, a ...interface{}) (n int, err error) { |
||
70 | return fmt.Fprintf(c.Stderr, format, a...) |
||
71 | } |
||
72 | |||
73 | // Errorln prints error logs to Stderr |
||
74 | func (c Console) Errorln(a ...interface{}) (n int, err error) { |
||
75 | return fmt.Fprintln(c.Stderr, a...) |
||
76 | } |
||
77 |