bar.go 3.1 KB

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