My mirror of the Barnard terminal client for Mumble.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

158 lignes
2.8 KiB

  1. package uiterm
  2. import (
  3. "strings"
  4. "github.com/nsf/termbox-go"
  5. )
  6. type Textview struct {
  7. Lines []string
  8. CurrentLine int
  9. Fg Attribute
  10. Bg Attribute
  11. parsedLines []string
  12. ui *Ui
  13. x0, y0, x1, y1 int
  14. }
  15. func (t *Textview) uiInitialize(ui *Ui) {
  16. t.ui = ui
  17. }
  18. func (t *Textview) uiSetActive(active bool) {
  19. }
  20. func (t *Textview) uiSetBounds(x0, y0, x1, y1 int) {
  21. t.x0 = x0
  22. t.y0 = y0
  23. t.x1 = x1
  24. t.y1 = y1
  25. t.updateParsedLines()
  26. t.uiDraw()
  27. }
  28. func (t *Textview) ScrollUp() {
  29. if newLine := t.CurrentLine + 1; newLine < len(t.parsedLines) {
  30. t.CurrentLine = newLine
  31. }
  32. t.uiDraw()
  33. }
  34. func (t *Textview) ScrollDown() {
  35. if newLine := t.CurrentLine - 1; newLine >= 0 {
  36. t.CurrentLine = newLine
  37. }
  38. t.uiDraw()
  39. }
  40. func (t *Textview) ScrollTop() {
  41. if newLine := len(t.parsedLines) - 1; newLine > 0 {
  42. t.CurrentLine = newLine
  43. } else {
  44. t.CurrentLine = 0
  45. }
  46. t.uiDraw()
  47. }
  48. func (t *Textview) ScrollBottom() {
  49. t.CurrentLine = 0
  50. t.uiDraw()
  51. }
  52. func (t *Textview) updateParsedLines() {
  53. width := t.x1 - t.x0 - 3
  54. if t.Lines == nil || width <= 0 {
  55. t.parsedLines = nil
  56. return
  57. }
  58. parsed := make([]string, 0, len(t.Lines))
  59. for _, line := range t.Lines {
  60. current := ""
  61. chars := 0
  62. reader := strings.NewReader(line)
  63. for {
  64. if chars >= width {
  65. parsed = append(parsed, current)
  66. chars = 0
  67. current = ""
  68. }
  69. if reader.Len() <= 0 {
  70. if chars > 0 {
  71. parsed = append(parsed, current)
  72. }
  73. break
  74. }
  75. if ch, _, err := reader.ReadRune(); err == nil {
  76. current = current + string(ch)
  77. chars++
  78. }
  79. }
  80. }
  81. t.parsedLines = parsed
  82. }
  83. func (t *Textview) AddLine(line string) {
  84. t.Lines = append(t.Lines, line)
  85. t.updateParsedLines()
  86. t.uiDraw()
  87. }
  88. func (t *Textview) Clear() {
  89. t.Lines = nil
  90. t.CurrentLine = 0
  91. t.parsedLines = nil
  92. t.uiDraw()
  93. }
  94. func (t *Textview) uiDraw() {
  95. t.ui.beginDraw()
  96. defer t.ui.endDraw()
  97. var reader *strings.Reader
  98. line := len(t.parsedLines) - 1 - t.CurrentLine
  99. if line < 0 {
  100. line = 0
  101. }
  102. totalLines := len(t.parsedLines)
  103. if totalLines == 0 {
  104. totalLines = 1
  105. }
  106. currentScrollLine := t.y1 - 1 - int((float32(t.CurrentLine)/float32(totalLines))*float32(t.y1-t.y0))
  107. for y := t.y1 - 1; y >= t.y0; y-- {
  108. if t.parsedLines != nil && line >= 0 {
  109. reader = strings.NewReader(t.parsedLines[line])
  110. } else {
  111. reader = nil
  112. }
  113. for x := t.x0; x < t.x1; x++ {
  114. var chr rune = ' '
  115. if x == t.x1-1 { // scrollbar
  116. if y == currentScrollLine {
  117. chr = '█'
  118. } else {
  119. chr = '░'
  120. }
  121. } else if x < t.x1-3 {
  122. if reader != nil {
  123. if ch, _, err := reader.ReadRune(); err == nil {
  124. chr = ch
  125. }
  126. }
  127. }
  128. termbox.SetCell(x, y, chr, termbox.Attribute(t.Fg), termbox.Attribute(t.Bg))
  129. }
  130. line--
  131. }
  132. }
  133. func (t *Textview) uiKeyEvent(mod Modifier, key Key) {
  134. }
  135. func (t *Textview) uiCharacterEvent(chr rune) {
  136. }