message.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package error
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type ErrorMessage [] StyledText
  7. type TextStyle int
  8. const (
  9. TS_NORMAL TextStyle = iota
  10. TS_BOLD
  11. TS_ERROR
  12. TS_WARNING
  13. TS_INFO
  14. TS_SPOT
  15. TS_INLINE
  16. TS_INLINE_CODE
  17. )
  18. type StyledText struct {
  19. Style TextStyle
  20. Text string
  21. }
  22. func T(style TextStyle, text string) StyledText {
  23. return StyledText { style, text }
  24. }
  25. var T_LF = StyledText { Style: TS_NORMAL, Text: "\n", }
  26. var T_SPACE = StyledText { Style: TS_NORMAL, Text: " ", }
  27. var T_INDENT = StyledText { Style: TS_NORMAL, Text: " ", }
  28. func (t StyledText) String() string {
  29. const Bold = "\033[1m"
  30. const Red = "\033[31m"
  31. const Green = "\033[32m"
  32. const Orange = "\033[33m"
  33. const Blue = "\033[34m"
  34. const Magenta = "\033[35m"
  35. const Cyan = "\033[36m"
  36. const Reset = "\033[0m"
  37. switch t.Style {
  38. case TS_NORMAL:
  39. return t.Text
  40. case TS_BOLD:
  41. return Bold + t.Text + Reset
  42. case TS_ERROR:
  43. return Bold + t.Text + Reset
  44. case TS_WARNING:
  45. return Bold + t.Text + Reset
  46. case TS_INFO:
  47. return Orange + Bold + t.Text + Reset
  48. case TS_SPOT:
  49. return Red + Bold + t.Text + Reset
  50. case TS_INLINE:
  51. return Red + Bold + t.Text + Reset
  52. case TS_INLINE_CODE:
  53. return Magenta + Bold + t.Text + Reset
  54. default:
  55. return t.Text
  56. }
  57. }
  58. func (msg *ErrorMessage) String() string {
  59. var buf strings.Builder
  60. for _, segment := range *msg {
  61. buf.WriteString(segment.String())
  62. }
  63. return buf.String()
  64. }
  65. func (msg *ErrorMessage) Write(text StyledText) {
  66. *msg = append(*msg, text)
  67. }
  68. func (msg *ErrorMessage) WriteText(style TextStyle, text string) {
  69. msg.Write(T(style, text))
  70. }
  71. func (msg *ErrorMessage) WriteInnerText(style TextStyle, text string) {
  72. msg.Write(T_SPACE)
  73. msg.WriteText(style, text)
  74. msg.Write(T_SPACE)
  75. }
  76. func (msg *ErrorMessage) WriteEndText(style TextStyle, text string) {
  77. msg.Write(T_SPACE)
  78. msg.WriteText(style, text)
  79. }
  80. func (msg *ErrorMessage) WriteBuffer(style TextStyle, buf *strings.Builder) {
  81. var text = buf.String()
  82. buf.Reset()
  83. if len(text) > 0 {
  84. msg.WriteText(style, text)
  85. }
  86. }
  87. func (msg *ErrorMessage) WriteAll(another ErrorMessage) {
  88. *msg = append(*msg, another...)
  89. }
  90. func (msg *ErrorMessage) WriteAllWithIndent(another ErrorMessage, amount uint) {
  91. msg.WriteRepeated(T_INDENT, amount)
  92. for i, item := range another {
  93. msg.Write(item)
  94. if item == T_LF && i != len(another)-1 {
  95. msg.WriteRepeated(T_INDENT, amount)
  96. }
  97. }
  98. }
  99. func (msg *ErrorMessage) WriteRepeated(text StyledText, amount uint) {
  100. for i := uint(0); i < amount; i += 1 {
  101. msg.Write(text)
  102. }
  103. }
  104. func JoinErrMsg(messages []ErrorMessage, separator StyledText) ErrorMessage {
  105. var joined = make(ErrorMessage, 0, len(messages))
  106. for i, item := range messages {
  107. joined.WriteAll(item)
  108. if i != len(messages)-1 {
  109. joined.Write(separator)
  110. }
  111. }
  112. return joined
  113. }
  114. func ListErrMsgItems(items []ErrorMessage, prefix string) ErrorMessage {
  115. var msg = make(ErrorMessage, 0)
  116. msg.WriteText(TS_NORMAL, fmt.Sprintf("%s {", prefix))
  117. if len(items) > 0 {
  118. msg.Write(T_LF)
  119. }
  120. for i, item := range items {
  121. msg.WriteAllWithIndent(item, 1)
  122. if i != len(items)-1 {
  123. msg.WriteText(TS_NORMAL, ",")
  124. }
  125. msg.Write(T_LF)
  126. }
  127. msg.WriteText(TS_NORMAL, "}")
  128. return msg
  129. }