12345678910111213141516171819202122232425262728293031323334353637383940 |
- package console
- import (
- "fmt"
- "github.com/mgutz/ansi"
- "os"
- )
- // Success 打印一条成功消息,绿色输出
- func Success(msg string) {
- colorOut(msg, "green")
- }
- // Error 打印一条报错消息,红色输出
- func Error(msg string) {
- colorOut(msg, "red")
- }
- // Warning 打印一条提示消息,黄色输出
- func Warning(msg string) {
- colorOut(msg, "yellow")
- }
- // Exit 打印一条报错消息,并退出 os.Exit(1)
- func Exit(msg string) {
- Error(msg)
- os.Exit(1)
- }
- // ExitIf 语法糖,自带 err != nil 判断
- func ExitIf(err error) {
- if err != nil {
- Exit(err.Error())
- }
- }
- // colorOut 内部使用,设置高亮颜色
- func colorOut(message, color string) {
- _, _ = fmt.Fprintln(os.Stdout, ansi.Color(message, color))
- }
|