console.go 2.4 KB

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