My mirror of the Barnard terminal client for Mumble.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

textbox.go 1.6 KiB

10 år sedan
10 år sedan
10 år sedan
10 år sedan
10 år sedan
10 år sedan
10 år sedan
10 år sedan
10 år sedan
10 år sedan
10 år sedan
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package uiterm
  2. import (
  3. "strings"
  4. "unicode/utf8"
  5. "github.com/nsf/termbox-go"
  6. )
  7. type Textbox struct {
  8. Text string
  9. Fg Attribute
  10. Bg Attribute
  11. Input func(ui *Ui, textbox *Textbox, text string)
  12. ui *Ui
  13. active bool
  14. x0, y0, x1, y1 int
  15. }
  16. func (t *Textbox) uiInitialize(ui *Ui) {
  17. t.ui = ui
  18. }
  19. func (t *Textbox) uiSetActive(active bool) {
  20. t.active = active
  21. t.uiDraw()
  22. }
  23. func (t *Textbox) uiSetBounds(x0, y0, x1, y1 int) {
  24. t.x0 = x0
  25. t.y0 = y0
  26. t.x1 = x1
  27. t.y1 = y1
  28. t.uiDraw()
  29. }
  30. func (t *Textbox) uiDraw() {
  31. t.ui.beginDraw()
  32. defer t.ui.endDraw()
  33. var setCursor = false
  34. reader := strings.NewReader(t.Text)
  35. for y := t.y0; y < t.y1; y++ {
  36. for x := t.x0; x < t.x1; x++ {
  37. var chr rune
  38. if ch, _, err := reader.ReadRune(); err != nil {
  39. if t.active && !setCursor {
  40. termbox.SetCursor(x, y)
  41. setCursor = true
  42. }
  43. chr = ' '
  44. } else {
  45. chr = ch
  46. }
  47. termbox.SetCell(x, y, chr, termbox.Attribute(t.Fg), termbox.Attribute(t.Bg))
  48. }
  49. }
  50. }
  51. func (t *Textbox) uiKeyEvent(mod Modifier, key Key) {
  52. redraw := false
  53. switch key {
  54. case KeyCtrlC:
  55. t.Text = ""
  56. redraw = true
  57. case KeyEnter:
  58. if t.Input != nil {
  59. t.Input(t.ui, t, t.Text)
  60. }
  61. t.Text = ""
  62. redraw = true
  63. case KeySpace:
  64. t.Text = t.Text + " "
  65. redraw = true
  66. case KeyBackspace:
  67. case KeyBackspace2:
  68. if len(t.Text) > 0 {
  69. if r, size := utf8.DecodeLastRuneInString(t.Text); r != utf8.RuneError {
  70. t.Text = t.Text[:len(t.Text)-size]
  71. redraw = true
  72. }
  73. }
  74. }
  75. if redraw {
  76. t.uiDraw()
  77. }
  78. }
  79. func (t *Textbox) uiCharacterEvent(chr rune) {
  80. t.Text = t.Text + string(chr)
  81. t.uiDraw()
  82. }