Official cwtch.im peer and server implementations.
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.

107 lines
2.2KB

  1. package main
  2. import (
  3. "strings"
  4. "github.com/jroimartin/gocui"
  5. )
  6. func editor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
  7. //state.HideHelp = true
  8. //if state.Mode == modeEscape {
  9. // escEditor(v, key, ch, mod)
  10. // return
  11. //}
  12. if ch != 0 && mod == 0 {
  13. v.EditWrite(ch)
  14. }
  15. switch key {
  16. case gocui.KeyEsc:
  17. //state.Mode = modeEscape
  18. //state.KeepAutoscrolling = true
  19. // Space, backspace, Del
  20. case gocui.KeySpace:
  21. v.EditWrite(' ')
  22. case gocui.KeyBackspace, gocui.KeyBackspace2:
  23. v.EditDelete(true)
  24. moveAhead(v)
  25. case gocui.KeyDelete:
  26. v.EditDelete(false)
  27. // Cursor movement
  28. case gocui.KeyArrowLeft:
  29. v.MoveCursor(-1, 0, false)
  30. moveAhead(v)
  31. case gocui.KeyArrowRight:
  32. x, _ := v.Cursor()
  33. x2, _ := v.Origin()
  34. x += x2
  35. buf := v.Buffer()
  36. // I don't know really how this works, this was mostly obtained through trial
  37. // and error. Anyway, this system impedes going on a newline by moving right.
  38. // This is usually possible because once you write something to the buffer
  39. // it automatically adds " \n", which is two characters. Sooo yeah.
  40. if buf != "" && len(buf) > (x+2) {
  41. v.MoveCursor(1, 0, false)
  42. }
  43. case gocui.KeyEnter:
  44. buf := v.Buffer()
  45. v.Clear()
  46. v.SetCursor(0, 0)
  47. if buf != "" {
  48. buf = buf[:len(buf)-1]
  49. }
  50. if strings.TrimSpace(buf) != "" {
  51. //state.PushAction(buf)
  52. //state.ActionIndex = -1
  53. }
  54. enterActionConnect(buf)
  55. }
  56. }
  57. func setText(v *gocui.View, text string) {
  58. v.Clear()
  59. // Why are we doing this? Because normally when you write a line
  60. // gocui adds " \n" at the end of it. Whe clearing and adding, though,
  61. // the space isn't added back.
  62. v.Write([]byte(text + " "))
  63. v.SetCursor(len(text), 0)
  64. }
  65. // moveAhead displays the next 10 characters when moving backwards,
  66. // in order to see where we're moving or what we're deleting.
  67. func moveAhead(v *gocui.View) {
  68. cX, _ := v.Cursor()
  69. oX, _ := v.Origin()
  70. if cX < 10 && oX > 0 {
  71. newOX := oX - 10
  72. forward := 10
  73. if newOX < 0 {
  74. forward += newOX
  75. newOX = 0
  76. }
  77. v.SetOrigin(newOX, 0)
  78. v.MoveCursor(forward, 0, false)
  79. }
  80. }
  81. func enterActionConnect(buf string) {
  82. //log.Printf("Connecting: %s",buf)
  83. connect(buf)
  84. }
  85. func moveDown(v *gocui.View) {
  86. _, yPos := v.Cursor()
  87. if _, err := v.Line(yPos + 1); err == nil {
  88. v.MoveCursor(0, 1, false)
  89. }
  90. }