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.

ui.go 4.2 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
10 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package barnard
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/kennygrant/sanitize"
  7. "github.com/layeh/barnard/uiterm"
  8. "github.com/layeh/gumble/gumble"
  9. )
  10. const (
  11. uiViewLogo = "logo"
  12. uiViewTop = "top"
  13. uiViewStatus = "status"
  14. uiViewInput = "input"
  15. uiViewInputStatus = "inputstatus"
  16. uiViewOutput = "output"
  17. uiViewTree = "tree"
  18. )
  19. func esc(str string) string {
  20. return sanitize.HTML(str)
  21. }
  22. func (b *Barnard) UpdateInputStatus(status string) {
  23. b.UiInputStatus.Text = status
  24. b.UiTree.Rebuild()
  25. b.Ui.Refresh()
  26. }
  27. func (b *Barnard) AddOutputLine(line string) {
  28. now := time.Now()
  29. b.UiOutput.AddLine(fmt.Sprintf("[%02d:%02d:%02d] %s", now.Hour(), now.Minute(), now.Second(), line))
  30. }
  31. func (b *Barnard) AddOutputMessage(sender *gumble.User, message string) {
  32. if sender == nil {
  33. b.AddOutputLine(message)
  34. } else {
  35. b.AddOutputLine(fmt.Sprintf("%s: %s", sender.Name(), strings.TrimSpace(esc(message))))
  36. }
  37. }
  38. func (b *Barnard) OnVoiceToggle(ui *uiterm.Ui, key uiterm.Key) {
  39. if b.UiStatus.Text == " Tx " {
  40. b.UiStatus.Text = " Idle "
  41. b.UiStatus.Fg = uiterm.ColorBlack
  42. b.UiStatus.Bg = uiterm.ColorWhite
  43. b.Stream.StopSource()
  44. } else {
  45. b.UiStatus.Fg = uiterm.ColorWhite | uiterm.AttrBold
  46. b.UiStatus.Bg = uiterm.ColorRed
  47. b.UiStatus.Text = " Tx "
  48. b.Stream.StartSource()
  49. }
  50. ui.Refresh()
  51. }
  52. func (b *Barnard) OnQuitPress(ui *uiterm.Ui, key uiterm.Key) {
  53. b.Client.Disconnect()
  54. b.Ui.Close()
  55. }
  56. func (b *Barnard) OnClearPress(ui *uiterm.Ui, key uiterm.Key) {
  57. b.UiOutput.Clear()
  58. }
  59. func (b *Barnard) OnScrollOutputUp(ui *uiterm.Ui, key uiterm.Key) {
  60. b.UiOutput.ScrollUp()
  61. }
  62. func (b *Barnard) OnScrollOutputDown(ui *uiterm.Ui, key uiterm.Key) {
  63. b.UiOutput.ScrollDown()
  64. }
  65. func (b *Barnard) OnScrollOutputTop(ui *uiterm.Ui, key uiterm.Key) {
  66. b.UiOutput.ScrollTop()
  67. }
  68. func (b *Barnard) OnScrollOutputBottom(ui *uiterm.Ui, key uiterm.Key) {
  69. b.UiOutput.ScrollBottom()
  70. }
  71. func (b *Barnard) OnFocusPress(ui *uiterm.Ui, key uiterm.Key) {
  72. active := b.Ui.Active()
  73. if active == uiViewInput {
  74. b.Ui.SetActive(uiViewTree)
  75. } else if active == uiViewTree {
  76. b.Ui.SetActive(uiViewInput)
  77. }
  78. }
  79. func (b *Barnard) OnTextInput(ui *uiterm.Ui, textbox *uiterm.Textbox, text string) {
  80. if text == "" {
  81. return
  82. }
  83. if b.Client != nil && b.Client.Self() != nil {
  84. b.Client.Self().Channel().Send(text, false)
  85. b.AddOutputMessage(b.Client.Self(), text)
  86. }
  87. }
  88. func (b *Barnard) OnUiInitialize(ui *uiterm.Ui) {
  89. ui.Add(uiViewLogo, &uiterm.Label{
  90. Text: " barnard ",
  91. Fg: uiterm.ColorWhite | uiterm.AttrBold,
  92. Bg: uiterm.ColorMagenta,
  93. })
  94. ui.Add(uiViewTop, &uiterm.Label{
  95. Fg: uiterm.ColorWhite,
  96. Bg: uiterm.ColorBlue,
  97. })
  98. b.UiStatus = uiterm.Label{
  99. Text: " Idle ",
  100. Fg: uiterm.ColorBlack,
  101. Bg: uiterm.ColorWhite,
  102. }
  103. ui.Add(uiViewStatus, &b.UiStatus)
  104. b.UiInput = uiterm.Textbox{
  105. Fg: uiterm.ColorWhite,
  106. Bg: uiterm.ColorBlack,
  107. Input: b.OnTextInput,
  108. }
  109. ui.Add(uiViewInput, &b.UiInput)
  110. b.UiInputStatus = uiterm.Label{
  111. Fg: uiterm.ColorBlack,
  112. Bg: uiterm.ColorWhite,
  113. }
  114. ui.Add(uiViewInputStatus, &b.UiInputStatus)
  115. b.UiOutput = uiterm.Textview{
  116. Fg: uiterm.ColorWhite,
  117. Bg: uiterm.ColorBlack,
  118. }
  119. ui.Add(uiViewOutput, &b.UiOutput)
  120. b.UiTree = uiterm.Tree{
  121. Generator: b.TreeItem,
  122. Listener: b.TreeItemSelect,
  123. Fg: uiterm.ColorWhite,
  124. Bg: uiterm.ColorBlack,
  125. }
  126. ui.Add(uiViewTree, &b.UiTree)
  127. b.Ui.AddKeyListener(b.OnFocusPress, uiterm.KeyTab)
  128. b.Ui.AddKeyListener(b.OnVoiceToggle, uiterm.KeyF1)
  129. b.Ui.AddKeyListener(b.OnQuitPress, uiterm.KeyF10)
  130. b.Ui.AddKeyListener(b.OnClearPress, uiterm.KeyCtrlL)
  131. b.Ui.AddKeyListener(b.OnScrollOutputUp, uiterm.KeyPgup)
  132. b.Ui.AddKeyListener(b.OnScrollOutputDown, uiterm.KeyPgdn)
  133. b.Ui.AddKeyListener(b.OnScrollOutputTop, uiterm.KeyHome)
  134. b.Ui.AddKeyListener(b.OnScrollOutputBottom, uiterm.KeyEnd)
  135. }
  136. func (b *Barnard) OnUiResize(ui *uiterm.Ui, width, height int) {
  137. ui.SetBounds(uiViewLogo, 0, 0, 9, 1)
  138. ui.SetBounds(uiViewTop, 9, 0, width-6, 1)
  139. ui.SetBounds(uiViewStatus, width-6, 0, width, 1)
  140. ui.SetBounds(uiViewInput, 0, height-1, width, height)
  141. ui.SetBounds(uiViewInputStatus, 0, height-2, width, height-1)
  142. ui.SetBounds(uiViewOutput, 0, 1, width-20, height-2)
  143. ui.SetBounds(uiViewTree, width-20, 1, width, height-2)
  144. }