My mirror of the Barnard terminal client for Mumble.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

textbox.go 1.6 KiB

10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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, 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. }