console.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 consle
  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. }
  23. // for idea log
  24. ideaConsole struct {
  25. }
  26. )
  27. // NewConsole returns a instance of Console
  28. func NewConsole(idea bool) Console {
  29. if idea {
  30. return NewIdeaConsole()
  31. }
  32. return NewColorConsole()
  33. }
  34. // NewColorConsole returns a 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. fmt.Println(aurora.Blue(msg))
  45. }
  46. func (c *colorConsole) Success(format string, a ...interface{}) {
  47. msg := fmt.Sprintf(format, a...)
  48. fmt.Println(aurora.Green(msg))
  49. }
  50. func (c *colorConsole) Warning(format string, a ...interface{}) {
  51. msg := fmt.Sprintf(format, a...)
  52. fmt.Println(aurora.Yellow(msg))
  53. }
  54. func (c *colorConsole) Error(format string, a ...interface{}) {
  55. msg := fmt.Sprintf(format, a...)
  56. fmt.Println(aurora.Red(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 instace 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.Blue(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. }