console.go 746 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package console
  2. import (
  3. "fmt"
  4. "github.com/mgutz/ansi"
  5. "os"
  6. )
  7. // Success 打印一条成功消息,绿色输出
  8. func Success(msg string) {
  9. colorOut(msg, "green")
  10. }
  11. // Error 打印一条报错消息,红色输出
  12. func Error(msg string) {
  13. colorOut(msg, "red")
  14. }
  15. // Warning 打印一条提示消息,黄色输出
  16. func Warning(msg string) {
  17. colorOut(msg, "yellow")
  18. }
  19. // Exit 打印一条报错消息,并退出 os.Exit(1)
  20. func Exit(msg string) {
  21. Error(msg)
  22. os.Exit(1)
  23. }
  24. // ExitIf 语法糖,自带 err != nil 判断
  25. func ExitIf(err error) {
  26. if err != nil {
  27. Exit(err.Error())
  28. }
  29. }
  30. // colorOut 内部使用,设置高亮颜色
  31. func colorOut(message, color string) {
  32. _, _ = fmt.Fprintln(os.Stdout, ansi.Color(message, color))
  33. }