Create printer pkg, fix color force enable/disable

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
Ahmet Alp Balkan
2020-04-12 22:30:10 -07:00
parent bef0a4cca7
commit 7598c4d4dd
13 changed files with 135 additions and 62 deletions

View File

@@ -0,0 +1,42 @@
package printer
import (
"fmt"
"io"
"github.com/fatih/color"
)
var (
errorColor = color.New(color.FgRed, color.Bold)
warningColor = color.New(color.FgYellow, color.Bold)
successColor = color.New(color.FgGreen)
)
func init() {
colors := UseColors()
if colors == nil {
return
}
if *colors {
errorColor.EnableColor()
warningColor.EnableColor()
successColor.EnableColor()
} else {
errorColor.DisableColor()
warningColor.DisableColor()
successColor.DisableColor()
}
}
func Error(w io.Writer, format string, args ...interface{}) {
fmt.Fprintf(w, color.RedString("error: ")+format+"\n", args...)
}
func Warning(w io.Writer, format string, args ...interface{}) {
fmt.Fprintf(w, color.YellowString("warning: ")+format+"\n", args...)
}
func Success(w io.Writer, format string, args ...interface{}) {
fmt.Fprintf(w, color.GreenString(fmt.Sprintf(format+"\n", args...)))
}