1
0

keyinput.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package editor
  2. import (
  3. "github.com/gdamore/tcell/v3"
  4. "moose/internal/util"
  5. "strconv"
  6. "strings"
  7. )
  8. func (m *Model) HandleKeyInput(ev *tcell.EventKey) {
  9. // if m.Mode == ModeNormal && util.StandardizeBinding(ev.Name()) is numeric { // type number in normal mode to repeat a keybind x amount of times
  10. // m.AM.NumericCM.Recording = true, .... along these lines
  11. //}
  12. if m.AM.CM.Recording == false {
  13. num, err := strconv.Atoi(util.StandardizeBinding(ev.Name()))
  14. if err != nil {
  15. goto Out
  16. }
  17. if m.Mode == ModeNormal {
  18. m.AM.RCM.Recording = true
  19. m.AM.RCM.Recorded = append(m.AM.RCM.Recorded, strconv.Itoa(num))
  20. return
  21. }
  22. }
  23. Out:
  24. for _, action := range append(m.CurrentActionSet(), m.AM.Common...) {
  25. for _, binding := range action.Bindings {
  26. switch binding.Type {
  27. case BindingSingle:
  28. if m.AM.CM.Recording {
  29. continue
  30. }
  31. if util.StandardizeBinding(binding.Single) == util.StandardizeBinding(ev.Name()) {
  32. m.AM.CM = NewChordManager()
  33. if len(action.Commands) > 0 && action.AskArgs == true {
  34. m.Mode = ModePalette
  35. m.BM.PaletteBuffer.Clear()
  36. m.BM.PaletteBuffer.Insert("/" + action.Commands[0] + " ")
  37. return
  38. }
  39. if m.AM.RCM.Recording == true {
  40. times, err := strconv.Atoi(strings.Join(m.AM.RCM.Recorded, ""))
  41. if err != nil {
  42. action.Callback(m, []string{})
  43. m.AM.RCM = NewChordManager()
  44. return
  45. }
  46. for range times {
  47. action.Callback(m, []string{})
  48. }
  49. m.AM.RCM = NewChordManager()
  50. return
  51. } else {
  52. action.Callback(m, []string{})
  53. return
  54. }
  55. }
  56. case BindingChord:
  57. if m.AM.CM.Recording {
  58. if len(binding.Chord) > len(m.AM.CM.Recorded) {
  59. matching := false
  60. for i := range m.AM.CM.Recorded {
  61. if m.AM.CM.Recorded[i] == binding.Chord[i] {
  62. matching = true
  63. }
  64. }
  65. if matching {
  66. if binding.Chord[len(m.AM.CM.Recorded)] == util.StandardizeBinding(ev.Name()) {
  67. m.AM.CM.Recorded = append(m.AM.CM.Recorded, util.StandardizeBinding(ev.Name()))
  68. if len(m.AM.CM.Recorded) == len(binding.Chord) {
  69. if m.AM.RCM.Recording == true {
  70. times, err := strconv.Atoi(strings.Join(m.AM.RCM.Recorded, ""))
  71. if err != nil {
  72. action.Callback(m, []string{})
  73. m.AM.RCM = NewChordManager()
  74. m.AM.CM = NewChordManager()
  75. return
  76. }
  77. for range times {
  78. action.Callback(m, []string{})
  79. }
  80. } else {
  81. action.Callback(m, []string{})
  82. }
  83. m.AM.RCM = NewChordManager()
  84. m.AM.CM = NewChordManager()
  85. }
  86. return
  87. }
  88. }
  89. }
  90. } else {
  91. if binding.Chord[0] == util.StandardizeBinding(ev.Name()) {
  92. m.AM.CM.Recorded = append(m.AM.CM.Recorded, util.StandardizeBinding(ev.Name()))
  93. if len(m.AM.CM.Recorded) == len(binding.Chord) {
  94. action.Callback(m, []string{})
  95. m.AM.RCM = NewChordManager()
  96. m.AM.CM = NewChordManager()
  97. } else {
  98. m.AM.CM.Recording = true
  99. }
  100. return
  101. }
  102. }
  103. }
  104. }
  105. }
  106. if m.AM.CM.Recording {
  107. m.AM.CM = NewChordManager()
  108. }
  109. if m.Mode == ModeWrite {
  110. if ev.Key() == tcell.KeyRune {
  111. m.BM.Current().Insert(ev.Str())
  112. }
  113. }
  114. if m.Mode == ModePalette {
  115. if ev.Key() == tcell.KeyRune {
  116. m.BM.PaletteBuffer.Insert(ev.Str())
  117. }
  118. }
  119. }