bar.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package door
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type BarStyle int
  7. const (
  8. SOLID BarStyle = iota
  9. HALF_STEP
  10. GRADIENT
  11. PERCENTAGE
  12. PERCENT_SPACE
  13. )
  14. type BarRange struct {
  15. Percent int64
  16. Color string
  17. }
  18. type BarLine struct {
  19. Width int
  20. Style BarStyle
  21. Percent int64 // percentage * 100
  22. ColorRange []BarRange
  23. UpdateP func() int64
  24. Line
  25. }
  26. func (bl *BarLine) CheckRange() {
  27. if len(bl.ColorRange) != 0 {
  28. // Ok, there is a color range. Get checking
  29. for _, br := range bl.ColorRange {
  30. if bl.Percent <= br.Percent {
  31. bl.DefaultColor = br.Color
  32. break
  33. }
  34. }
  35. }
  36. }
  37. func (bl *BarLine) Output() string {
  38. var output string
  39. var step_width int64
  40. if bl.UpdateP != nil {
  41. bl.Percent = bl.UpdateP()
  42. }
  43. bl.CheckRange()
  44. switch bl.Style {
  45. case SOLID, PERCENTAGE, PERCENT_SPACE:
  46. step_width = int64(100 * 100 / bl.Width)
  47. var steps int = int(bl.Percent / step_width)
  48. if Unicode {
  49. output += strings.Repeat("\u2588", steps)
  50. } else {
  51. output += strings.Repeat("\xdb", steps)
  52. }
  53. // This will work, because we aren't trying to len(output) with unicode.
  54. output += strings.Repeat(" ", int(bl.Width-steps))
  55. if bl.Style == PERCENTAGE || bl.Style == PERCENT_SPACE {
  56. percent := fmt.Sprintf("%d", bl.Percent/100)
  57. var pos int = bl.Width/2 - 1
  58. if percent != "100" {
  59. percent += "%"
  60. if len(percent) < 3 {
  61. percent = " " + percent
  62. }
  63. }
  64. if bl.Style == PERCENT_SPACE {
  65. percent = " " + percent + " "
  66. pos--
  67. }
  68. // to process/slice the string (with unicode) do this:
  69. // convert to []rune, slice that, convert back to string
  70. //
  71. // sliceable := []rune(output)
  72. // newString := string(sliceable[:5]) + " new content " + (sliceable[10:])
  73. // fmt.Printf("%d %d [%s] %d [%s]\n", bl.Width, pos, percent, len(output), output)
  74. if Unicode {
  75. runes := []rune(output)
  76. output = string(runes[:pos]) + percent + string(runes[pos+len(percent):])
  77. } else {
  78. output = output[:pos] + percent + output[pos+len(percent):]
  79. }
  80. }
  81. case HALF_STEP:
  82. step_width = int64(100 * 100 / bl.Width)
  83. var steps int = int(bl.Percent * 2 / step_width)
  84. if Unicode {
  85. output += strings.Repeat("\u2588", steps/2)
  86. } else {
  87. output += strings.Repeat("\xdb", steps/2)
  88. }
  89. if steps%1 == 1 {
  90. if Unicode {
  91. output += "\u258c"
  92. } else {
  93. output += "\xdd"
  94. }
  95. steps++
  96. }
  97. output += strings.Repeat(" ", bl.Width-(steps/2))
  98. case GRADIENT:
  99. step_width = int64(100 * 100 / bl.Width)
  100. var steps int = int(bl.Percent * 4 / step_width)
  101. if Unicode {
  102. output += strings.Repeat("\u2588", steps/4)
  103. } else {
  104. output += strings.Repeat("\xb0", steps/4)
  105. }
  106. if steps%4 != 0 {
  107. switch steps % 4 {
  108. case 1:
  109. if Unicode {
  110. output += "\u2591"
  111. } else {
  112. output += "\xb0"
  113. }
  114. case 2:
  115. if Unicode {
  116. output += "\u2592"
  117. } else {
  118. output += "\xb1"
  119. }
  120. case 3:
  121. if Unicode {
  122. output += "\u2593"
  123. } else {
  124. output += "\xb2"
  125. }
  126. }
  127. for steps%4 != 0 {
  128. steps++
  129. }
  130. }
  131. output += strings.Repeat(" ", bl.Width-(steps/4))
  132. }
  133. return bl.DefaultColor + output
  134. }