You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

table_with_color.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package tablewriter
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. )
  7. const ESC = "\033"
  8. const SEP = ";"
  9. const (
  10. BgBlackColor int = iota + 40
  11. BgRedColor
  12. BgGreenColor
  13. BgYellowColor
  14. BgBlueColor
  15. BgMagentaColor
  16. BgCyanColor
  17. BgWhiteColor
  18. )
  19. const (
  20. FgBlackColor int = iota + 30
  21. FgRedColor
  22. FgGreenColor
  23. FgYellowColor
  24. FgBlueColor
  25. FgMagentaColor
  26. FgCyanColor
  27. FgWhiteColor
  28. )
  29. const (
  30. BgHiBlackColor int = iota + 100
  31. BgHiRedColor
  32. BgHiGreenColor
  33. BgHiYellowColor
  34. BgHiBlueColor
  35. BgHiMagentaColor
  36. BgHiCyanColor
  37. BgHiWhiteColor
  38. )
  39. const (
  40. FgHiBlackColor int = iota + 90
  41. FgHiRedColor
  42. FgHiGreenColor
  43. FgHiYellowColor
  44. FgHiBlueColor
  45. FgHiMagentaColor
  46. FgHiCyanColor
  47. FgHiWhiteColor
  48. )
  49. const (
  50. Normal = 0
  51. Bold = 1
  52. UnderlineSingle = 4
  53. Italic
  54. )
  55. type Colors []int
  56. func startFormat(seq string) string {
  57. return fmt.Sprintf("%s[%sm", ESC, seq)
  58. }
  59. func stopFormat() string {
  60. return fmt.Sprintf("%s[%dm", ESC, Normal)
  61. }
  62. // Making the SGR (Select Graphic Rendition) sequence.
  63. func makeSequence(codes []int) string {
  64. codesInString := []string{}
  65. for _, code := range codes {
  66. codesInString = append(codesInString, strconv.Itoa(code))
  67. }
  68. return strings.Join(codesInString, SEP)
  69. }
  70. // Adding ANSI escape sequences before and after string
  71. func format(s string, codes interface{}) string {
  72. var seq string
  73. switch v := codes.(type) {
  74. case string:
  75. seq = v
  76. case []int:
  77. seq = makeSequence(v)
  78. case Colors:
  79. seq = makeSequence(v)
  80. default:
  81. return s
  82. }
  83. if len(seq) == 0 {
  84. return s
  85. }
  86. return startFormat(seq) + s + stopFormat()
  87. }
  88. // Adding header colors (ANSI codes)
  89. func (t *Table) SetHeaderColor(colors ...Colors) {
  90. if t.colSize != len(colors) {
  91. panic("Number of header colors must be equal to number of headers.")
  92. }
  93. for i := 0; i < len(colors); i++ {
  94. t.headerParams = append(t.headerParams, makeSequence(colors[i]))
  95. }
  96. }
  97. // Adding column colors (ANSI codes)
  98. func (t *Table) SetColumnColor(colors ...Colors) {
  99. if t.colSize != len(colors) {
  100. panic("Number of column colors must be equal to number of headers.")
  101. }
  102. for i := 0; i < len(colors); i++ {
  103. t.columnsParams = append(t.columnsParams, makeSequence(colors[i]))
  104. }
  105. }
  106. // Adding column colors (ANSI codes)
  107. func (t *Table) SetFooterColor(colors ...Colors) {
  108. if len(t.footers) != len(colors) {
  109. panic("Number of footer colors must be equal to number of footer.")
  110. }
  111. for i := 0; i < len(colors); i++ {
  112. t.footerParams = append(t.footerParams, makeSequence(colors[i]))
  113. }
  114. }
  115. func Color(colors ...int) []int {
  116. return colors
  117. }