1
0

keyinput.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package editor
  2. import (
  3. "github.com/gdamore/tcell/v3"
  4. "moose/internal/util"
  5. )
  6. func (m *Model) HandleKeyInput(ev *tcell.EventKey) {
  7. hasValidContinuingChordPath := false
  8. // if m.Mode == ModeNormal && util.StandardizeBinding(ev.Name()) is numeric { // type number in normal mode to repeat a keybind x amount of times
  9. // m.AM.NumericCM.Recording = true, .... along these lines
  10. //}
  11. for _, action := range append(m.CurrentActionSet(), m.AM.Common...) {
  12. for _, binding := range action.Bindings {
  13. switch binding.Type {
  14. case BindingSingle:
  15. if util.StandardizeBinding(binding.Single) == util.StandardizeBinding(ev.Name()) {
  16. if len(action.Commands) > 0 && action.AskArgs == true {
  17. m.Mode = ModePalette
  18. m.BM.PaletteBuffer.Clear()
  19. m.BM.PaletteBuffer.Insert("/" + action.Commands[0] + " ")
  20. return
  21. }
  22. action.Callback(m, []string{})
  23. return
  24. }
  25. case BindingChord:
  26. if m.AM.CM.Recording {
  27. if len(binding.Chord) > len(m.AM.CM.Recorded) {
  28. matching := false
  29. for i := range m.AM.CM.Recorded {
  30. if m.AM.CM.Recorded[i] == binding.Chord[i] {
  31. matching = true
  32. } else {
  33. matching = false
  34. break
  35. }
  36. }
  37. if matching {
  38. hasValidContinuingChordPath = true
  39. if binding.Chord[len(m.AM.CM.Recorded)] == util.StandardizeBinding(ev.Name()) {
  40. m.AM.CM.Recorded = append(m.AM.CM.Recorded, util.StandardizeBinding(ev.Name()))
  41. if len(m.AM.CM.Recorded) == len(binding.Chord) {
  42. action.Callback(m, []string{})
  43. m.AM.CM = NewChordManager()
  44. return
  45. }
  46. } else {
  47. continue
  48. }
  49. } else {
  50. continue
  51. }
  52. } else {
  53. continue
  54. }
  55. } else {
  56. if binding.Chord[0] == util.StandardizeBinding(ev.Name()) {
  57. hasValidContinuingChordPath = true
  58. m.AM.CM.Recorded = append(m.AM.CM.Recorded, util.StandardizeBinding(ev.Name()))
  59. if len(m.AM.CM.Recorded) == len(binding.Chord) {
  60. action.Callback(m, []string{})
  61. m.AM.CM = NewChordManager()
  62. return
  63. } else {
  64. m.AM.CM.Recording = true
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. if !hasValidContinuingChordPath {
  72. m.AM.CM = NewChordManager()
  73. }
  74. if m.Mode == ModeWrite {
  75. if ev.Key() == tcell.KeyRune {
  76. m.BM.Current().Insert(ev.Str())
  77. }
  78. }
  79. if m.Mode == ModePalette {
  80. if ev.Key() == tcell.KeyRune {
  81. m.BM.PaletteBuffer.Insert(ev.Str())
  82. }
  83. }
  84. }