message.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package error
  2. import "strings"
  3. type ErrorMessage [] StyledText
  4. type TextStyle int
  5. const (
  6. TS_NORMAL TextStyle = iota
  7. TS_BOLD
  8. TS_ERROR
  9. TS_WARNING
  10. TS_INFO
  11. TS_SPOT
  12. TS_INLINE
  13. TS_INLINE_CODE
  14. )
  15. type StyledText struct {
  16. Style TextStyle
  17. Text string
  18. }
  19. func T(style TextStyle, text string) StyledText {
  20. return StyledText { style, text }
  21. }
  22. var T_LF = StyledText { Style: TS_NORMAL, Text: "\n", }
  23. var T_SPACE = StyledText { Style: TS_NORMAL, Text: " ", }
  24. var T_INDENT = StyledText { Style: TS_NORMAL, Text: " ", }
  25. func (t StyledText) String() string {
  26. const Bold = "\033[1m"
  27. const Red = "\033[31m"
  28. const Green = "\033[32m"
  29. const Orange = "\033[33m"
  30. const Blue = "\033[34m"
  31. const Magenta = "\033[35m"
  32. const Cyan = "\033[36m"
  33. const Reset = "\033[0m"
  34. switch t.Style {
  35. case TS_NORMAL:
  36. return t.Text
  37. case TS_BOLD:
  38. return Bold + t.Text + Reset
  39. case TS_ERROR:
  40. return Bold + t.Text + Reset
  41. case TS_WARNING:
  42. return Bold + t.Text + Reset
  43. case TS_INFO:
  44. return Orange + Bold + t.Text + Reset
  45. case TS_SPOT:
  46. return Red + Bold + t.Text + Reset
  47. case TS_INLINE:
  48. return Red + Bold + t.Text + Reset
  49. case TS_INLINE_CODE:
  50. return Magenta + Bold + t.Text + Reset
  51. default:
  52. return t.Text
  53. }
  54. }
  55. func (msg *ErrorMessage) String() string {
  56. var buf strings.Builder
  57. for _, segment := range *msg {
  58. buf.WriteString(segment.String())
  59. }
  60. return buf.String()
  61. }
  62. func (msg *ErrorMessage) Write(text StyledText) {
  63. *msg = append(*msg, text)
  64. }
  65. func (msg *ErrorMessage) WriteText(style TextStyle, text string) {
  66. msg.Write(T(style, text))
  67. }
  68. func (msg *ErrorMessage) WriteInnerText(style TextStyle, text string) {
  69. msg.Write(T_SPACE)
  70. msg.WriteText(style, text)
  71. msg.Write(T_SPACE)
  72. }
  73. func (msg *ErrorMessage) WriteEndText(style TextStyle, text string) {
  74. msg.Write(T_SPACE)
  75. msg.WriteText(style, text)
  76. }
  77. func (msg *ErrorMessage) WriteBuffer(style TextStyle, buf *strings.Builder) {
  78. var text = buf.String()
  79. buf.Reset()
  80. if len(text) > 0 {
  81. msg.WriteText(style, text)
  82. }
  83. }
  84. func (msg *ErrorMessage) WriteAll(another ErrorMessage) {
  85. *msg = append(*msg, another...)
  86. }
  87. func JoinErrMsg(messages []ErrorMessage, separator StyledText) ErrorMessage {
  88. var joined = make(ErrorMessage, 0, len(messages))
  89. for i, item := range messages {
  90. joined.WriteAll(item)
  91. if i != len(messages)-1 {
  92. joined.Write(separator)
  93. }
  94. }
  95. return joined
  96. }