|
3 | 3 | */ |
4 | 4 | package cli |
5 | 5 |
|
6 | | -import "fmt" |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | +) |
7 | 10 |
|
8 | 11 | const ( |
9 | 12 | ColorOff = "\033[0m" // Reset Color |
@@ -86,42 +89,61 @@ const ( |
86 | 89 | ErrorColor = BRed |
87 | 90 | ) |
88 | 91 |
|
89 | | -func Debug(s string) { |
90 | | - fmt.Print(DebugColor + s + ColorOff) |
| 92 | +var ( |
| 93 | + withColor = true |
| 94 | +) |
| 95 | + |
| 96 | +func EnableColor() { |
| 97 | + withColor = true |
| 98 | +} |
| 99 | + |
| 100 | +func DisableColor() { |
| 101 | + withColor = false |
| 102 | +} |
| 103 | + |
| 104 | +func colorized(color string, a ...interface{}) string { |
| 105 | + if withColor { |
| 106 | + return color + fmt.Sprint(a...) + ColorOff |
| 107 | + } |
| 108 | + return fmt.Sprint(a...) |
| 109 | +} |
| 110 | + |
| 111 | +func Debug(w io.Writer, a ...interface{}) (n int, err error) { |
| 112 | + return Print(w, colorized(DebugColor, a...)) |
91 | 113 | } |
92 | 114 |
|
93 | | -func Info(s string) { |
94 | | - fmt.Print(InfoColor + s + ColorOff) |
| 115 | +func Info(w io.Writer, a ...interface{}) (n int, err error) { |
| 116 | + return Print(w, colorized(InfoColor, a...)) |
95 | 117 | } |
96 | 118 |
|
97 | | -func Notice(s string) { |
98 | | - fmt.Print(NoticeColor + s + ColorOff) |
| 119 | +func Notice(w io.Writer, a ...interface{}) (n int, err error) { |
| 120 | + return Print(w, colorized(NoticeColor, a...)) |
99 | 121 | } |
100 | 122 |
|
101 | | -func Warning(s string) { |
102 | | - fmt.Print(WarningColor + s + ColorOff) |
| 123 | +func Warning(w io.Writer, a ...interface{}) (n int, err error) { |
| 124 | + return Print(w, colorized(WarningColor, a...)) |
103 | 125 | } |
104 | 126 |
|
105 | | -func Error(s string) { |
106 | | - fmt.Print(ErrorColor + s + ColorOff) |
| 127 | +func Error(w io.Writer, a ...interface{}) (n int, err error) { |
| 128 | + return Print(w, colorized(ErrorColor, a...)) |
107 | 129 | } |
108 | 130 |
|
109 | | -func Debugf(format string, args ...interface{}) { |
110 | | - Debug(fmt.Sprintf(format, args...)) |
| 131 | +func Debugf(w io.Writer, format string, args ...interface{}) (n int, err error) { |
| 132 | + return Debug(w, fmt.Sprintf(format, args...)) |
111 | 133 | } |
112 | 134 |
|
113 | | -func Infof(format string, args ...interface{}) { |
114 | | - Info(fmt.Sprintf(format, args...)) |
| 135 | +func Infof(w io.Writer, format string, args ...interface{}) (n int, err error) { |
| 136 | + return Info(w, fmt.Sprintf(format, args...)) |
115 | 137 | } |
116 | 138 |
|
117 | | -func Noticef(format string, args ...interface{}) { |
118 | | - Notice(fmt.Sprintf(format, args...)) |
| 139 | +func Noticef(w io.Writer, format string, args ...interface{}) (n int, err error) { |
| 140 | + return Notice(w, fmt.Sprintf(format, args...)) |
119 | 141 | } |
120 | 142 |
|
121 | | -func Warningf(format string, args ...interface{}) { |
122 | | - Warning(fmt.Sprintf(format, args...)) |
| 143 | +func Warningf(w io.Writer, format string, args ...interface{}) (n int, err error) { |
| 144 | + return Warning(w, fmt.Sprintf(format, args...)) |
123 | 145 | } |
124 | 146 |
|
125 | | -func Errorf(format string, args ...interface{}) { |
126 | | - Error(fmt.Sprintf(format, args...)) |
| 147 | +func Errorf(w io.Writer, format string, args ...interface{}) (n int, err error) { |
| 148 | + return Error(w, fmt.Sprintf(format, args...)) |
127 | 149 | } |
0 commit comments