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.

89 lignes
1.5 KiB

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