keyinput.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. }
  47. }
  48. }
  49. } else {
  50. if binding.Chord[0] == util.StandardizeBinding(ev.Name()) {
  51. hasValidContinuingChordPath = true
  52. m.AM.CM.Recorded = append(m.AM.CM.Recorded, util.StandardizeBinding(ev.Name()))
  53. if len(m.AM.CM.Recorded) == len(binding.Chord) {
  54. action.Callback(m, []string{})
  55. m.AM.CM = NewChordManager()
  56. return
  57. } else {
  58. m.AM.CM.Recording = true
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }
  65. if !hasValidContinuingChordPath {
  66. m.AM.CM = NewChordManager()
  67. }
  68. if m.Mode == ModeWrite {
  69. if ev.Key() == tcell.KeyRune {
  70. m.BM.Current().Insert(ev.Str())
  71. }
  72. }
  73. if m.Mode == ModePalette {
  74. if ev.Key() == tcell.KeyRune {
  75. m.BM.PaletteBuffer.Insert(ev.Str())
  76. }
  77. }
  78. }