console.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package console
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/logrusorgru/aurora"
  6. )
  7. type (
  8. // Console wraps from the fmt.Sprintf,
  9. // by default, it implemented the colorConsole to provide the colorful output to the console
  10. // and the ideaConsole to output with prefix for the plugin of intellij
  11. Console interface {
  12. Success(format string, a ...interface{})
  13. Info(format string, a ...interface{})
  14. Debug(format string, a ...interface{})
  15. Warning(format string, a ...interface{})
  16. Error(format string, a ...interface{})
  17. Fatalln(format string, a ...interface{})
  18. MarkDone()
  19. Must(err error)
  20. }
  21. colorConsole struct{}
  22. // for idea log
  23. ideaConsole struct{}
  24. )
  25. // NewConsole returns an instance of Console
  26. func NewConsole(idea bool) Console {
  27. if idea {
  28. return NewIdeaConsole()
  29. }
  30. return NewColorConsole()
  31. }
  32. // NewColorConsole returns an instance of colorConsole
  33. func NewColorConsole() Console {
  34. return &colorConsole{}
  35. }
  36. func (c *colorConsole) Info(format string, a ...interface{}) {
  37. msg := fmt.Sprintf(format, a...)
  38. fmt.Println(msg)
  39. }
  40. func (c *colorConsole) Debug(format string, a ...interface{}) {
  41. msg := fmt.Sprintf(format, a...)
  42. fmt.Println(aurora.Blue(msg))
  43. }
  44. func (c *colorConsole) Success(format string, a ...interface{}) {
  45. msg := fmt.Sprintf(format, a...)
  46. fmt.Println(aurora.Green(msg))
  47. }
  48. func (c *colorConsole) Warning(format string, a ...interface{}) {
  49. msg := fmt.Sprintf(format, a...)
  50. fmt.Println(aurora.Yellow(msg))
  51. }
  52. func (c *colorConsole) Error(format string, a ...interface{}) {
  53. msg := fmt.Sprintf(format, a...)
  54. fmt.Println(aurora.Red(msg))
  55. }
  56. func (c *colorConsole) Fatalln(format string, a ...interface{}) {
  57. c.Error(format, a...)
  58. os.Exit(1)
  59. }
  60. func (c *colorConsole) MarkDone() {
  61. c.Success("Done.")
  62. }
  63. func (c *colorConsole) Must(err error) {
  64. if err != nil {
  65. c.Fatalln("%+v", err)
  66. }
  67. }
  68. // NewIdeaConsole returns a instance of ideaConsole
  69. func NewIdeaConsole() Console {
  70. return &ideaConsole{}
  71. }
  72. func (i *ideaConsole) Info(format string, a ...interface{}) {
  73. msg := fmt.Sprintf(format, a...)
  74. fmt.Println(msg)
  75. }
  76. func (i *ideaConsole) Debug(format string, a ...interface{}) {
  77. msg := fmt.Sprintf(format, a...)
  78. fmt.Println(aurora.Blue(msg))
  79. }
  80. func (i *ideaConsole) Success(format string, a ...interface{}) {
  81. msg := fmt.Sprintf(format, a...)
  82. fmt.Println("[SUCCESS]: ", msg)
  83. }
  84. func (i *ideaConsole) Warning(format string, a ...interface{}) {
  85. msg := fmt.Sprintf(format, a...)
  86. fmt.Println("[WARNING]: ", msg)
  87. }
  88. func (i *ideaConsole) Error(format string, a ...interface{}) {
  89. msg := fmt.Sprintf(format, a...)
  90. fmt.Println("[ERROR]: ", msg)
  91. }
  92. func (i *ideaConsole) Fatalln(format string, a ...interface{}) {
  93. i.Error(format, a...)
  94. os.Exit(1)
  95. }
  96. func (i *ideaConsole) MarkDone() {
  97. i.Success("Done.")
  98. }
  99. func (i *ideaConsole) Must(err error) {
  100. if err != nil {
  101. i.Fatalln("%+v", err)
  102. }
  103. }