My mirror of the Barnard terminal client for Mumble.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

94 rindas
1.6 KiB

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