console.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package console
  2. import (
  3. "fmt"
  4. "os"
  5. "runtime"
  6. "github.com/logrusorgru/aurora"
  7. "github.com/zeromicro/go-zero/tools/goctl/vars"
  8. )
  9. type (
  10. // Console wraps from the fmt.Sprintf,
  11. // by default, it implemented the colorConsole to provide the colorful output to the console
  12. // and the ideaConsole to output with prefix for the plugin of intellij
  13. Console interface {
  14. Success(format string, a ...interface{})
  15. Info(format string, a ...interface{})
  16. Debug(format string, a ...interface{})
  17. Warning(format string, a ...interface{})
  18. Error(format string, a ...interface{})
  19. Fatalln(format string, a ...interface{})
  20. MarkDone()
  21. Must(err error)
  22. }
  23. colorConsole struct{}
  24. // for idea log
  25. ideaConsole struct{}
  26. )
  27. // NewConsole returns an instance of Console
  28. func NewConsole(idea bool) Console {
  29. if idea {
  30. return NewIdeaConsole()
  31. }
  32. return NewColorConsole()
  33. }
  34. // NewColorConsole returns an instance of colorConsole
  35. func NewColorConsole() Console {
  36. return &colorConsole{}
  37. }
  38. func (c *colorConsole) Info(format string, a ...interface{}) {
  39. msg := fmt.Sprintf(format, a...)
  40. fmt.Println(msg)
  41. }
  42. func (c *colorConsole) Debug(format string, a ...interface{}) {
  43. msg := fmt.Sprintf(format, a...)
  44. println(aurora.BrightCyan(msg))
  45. }
  46. func (c *colorConsole) Success(format string, a ...interface{}) {
  47. msg := fmt.Sprintf(format, a...)
  48. println(aurora.BrightGreen(msg))
  49. }
  50. func (c *colorConsole) Warning(format string, a ...interface{}) {
  51. msg := fmt.Sprintf(format, a...)
  52. println(aurora.BrightYellow(msg))
  53. }
  54. func (c *colorConsole) Error(format string, a ...interface{}) {
  55. msg := fmt.Sprintf(format, a...)
  56. println(aurora.BrightRed(msg))
  57. }
  58. func (c *colorConsole) Fatalln(format string, a ...interface{}) {
  59. c.Error(format, a...)
  60. os.Exit(1)
  61. }
  62. func (c *colorConsole) MarkDone() {
  63. c.Success("Done.")
  64. }
  65. func (c *colorConsole) Must(err error) {
  66. if err != nil {
  67. c.Fatalln("%+v", err)
  68. }
  69. }
  70. // NewIdeaConsole returns a instance of ideaConsole
  71. func NewIdeaConsole() Console {
  72. return &ideaConsole{}
  73. }
  74. func (i *ideaConsole) Info(format string, a ...interface{}) {
  75. msg := fmt.Sprintf(format, a...)
  76. fmt.Println(msg)
  77. }
  78. func (i *ideaConsole) Debug(format string, a ...interface{}) {
  79. msg := fmt.Sprintf(format, a...)
  80. fmt.Println(aurora.BrightCyan(msg))
  81. }
  82. func (i *ideaConsole) Success(format string, a ...interface{}) {
  83. msg := fmt.Sprintf(format, a...)
  84. fmt.Println("[SUCCESS]: ", msg)
  85. }
  86. func (i *ideaConsole) Warning(format string, a ...interface{}) {
  87. msg := fmt.Sprintf(format, a...)
  88. fmt.Println("[WARNING]: ", msg)
  89. }
  90. func (i *ideaConsole) Error(format string, a ...interface{}) {
  91. msg := fmt.Sprintf(format, a...)
  92. fmt.Println("[ERROR]: ", msg)
  93. }
  94. func (i *ideaConsole) Fatalln(format string, a ...interface{}) {
  95. i.Error(format, a...)
  96. os.Exit(1)
  97. }
  98. func (i *ideaConsole) MarkDone() {
  99. i.Success("Done.")
  100. }
  101. func (i *ideaConsole) Must(err error) {
  102. if err != nil {
  103. i.Fatalln("%+v", err)
  104. }
  105. }
  106. func println(msg interface{}) {
  107. value, ok := msg.(aurora.Value)
  108. if !ok {
  109. fmt.Println(msg)
  110. }
  111. goos := runtime.GOOS
  112. if goos == vars.OsWindows {
  113. fmt.Println(value.Value())
  114. return
  115. }
  116. fmt.Println(msg)
  117. }
  118. var defaultConsole = new(colorConsole)
  119. func Success(format string, a ...interface{}) {
  120. defaultConsole.Success(format, a...)
  121. }
  122. func Info(format string, a ...interface{}) {
  123. defaultConsole.Info(format, a...)
  124. }
  125. func Debug(format string, a ...interface{}) {
  126. defaultConsole.Debug(format, a...)
  127. }
  128. func Warning(format string, a ...interface{}) {
  129. defaultConsole.Warning(format, a...)
  130. }
  131. func Error(format string, a ...interface{}) {
  132. defaultConsole.Error(format, a...)
  133. }
  134. func Fatalln(format string, a ...interface{}) {
  135. defaultConsole.Fatalln(format, a...)
  136. }
  137. func MarkDone() {
  138. defaultConsole.MarkDone()
  139. }
  140. func Must(err error) {
  141. defaultConsole.Must(err)
  142. }