2021-11-07 18:58:49 +00:00
|
|
|
package echo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-11-08 17:30:45 +00:00
|
|
|
ColorReset = "\033[0m"
|
|
|
|
ColorRed = "\033[31m"
|
|
|
|
ColorGreen = "\033[32m"
|
|
|
|
ColorYellow = "\033[33m"
|
|
|
|
ColorBlue = "\033[34m"
|
2021-11-07 18:58:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
useColor bool = false
|
|
|
|
output io.Writer = os.Stdout
|
|
|
|
)
|
|
|
|
|
|
|
|
func Color(enabled bool) {
|
|
|
|
useColor = enabled
|
|
|
|
}
|
|
|
|
|
|
|
|
func Output(writer io.Writer) {
|
|
|
|
output = writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func ErrorfMsg(format string, a ...interface{}) error {
|
|
|
|
msg := fmt.Sprintf(format, a...)
|
|
|
|
if useColor {
|
2021-11-08 17:30:45 +00:00
|
|
|
msg = fmt.Sprintf("%vError:%v %v", ColorRed, ColorReset, msg)
|
2021-11-07 18:58:49 +00:00
|
|
|
} else {
|
|
|
|
msg = fmt.Sprintf("Error: %v", msg)
|
|
|
|
}
|
|
|
|
return write(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func InfoFMsg(format string, a ...interface{}) error {
|
|
|
|
msg := fmt.Sprintf(format, a...)
|
|
|
|
if useColor {
|
2021-11-08 17:30:45 +00:00
|
|
|
msg = fmt.Sprintf("%vInfo:%v %v", ColorBlue, ColorReset, msg)
|
2021-11-07 18:58:49 +00:00
|
|
|
} else {
|
|
|
|
msg = fmt.Sprintf("Info: %v", msg)
|
|
|
|
}
|
|
|
|
return write(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GreenMessageF(format string, a ...interface{}) error {
|
2021-11-08 17:30:45 +00:00
|
|
|
return writeWithColor(fmt.Sprintf(format, a...), ColorGreen)
|
2021-11-07 18:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func YellowMessageF(format string, a ...interface{}) error {
|
2021-11-08 17:30:45 +00:00
|
|
|
return writeWithColor(fmt.Sprintf(format, a...), ColorYellow)
|
2021-11-07 18:58:49 +00:00
|
|
|
}
|
|
|
|
func RedMessageF(format string, a ...interface{}) error {
|
2021-11-08 17:30:45 +00:00
|
|
|
return writeWithColor(fmt.Sprintf(format, a...), ColorRed)
|
2021-11-07 18:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeWithColor(msg string, color string) error {
|
|
|
|
if useColor {
|
2021-11-08 17:30:45 +00:00
|
|
|
return write(fmt.Sprintf("%v%v%v", color, msg, ColorReset))
|
2021-11-07 18:58:49 +00:00
|
|
|
}
|
|
|
|
return write(msg)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func write(msg string) error {
|
|
|
|
_, err := fmt.Fprintln(output, msg)
|
|
|
|
return err
|
|
|
|
}
|