keyinput.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. for _, action := range append(m.CurrentActionSet(), m.AM.Common...) {
  9. for _, binding := range action.Bindings {
  10. switch binding.Type {
  11. case BindingSingle:
  12. if util.StandardizeBinding(binding.Single) == util.StandardizeBinding(ev.Name()) {
  13. if len(action.Commands) > 0 && action.AskArgs == true {
  14. m.Mode = ModePalette
  15. m.BM.PaletteBuffer.Clear()
  16. m.BM.PaletteBuffer.Insert("/" + action.Commands[0] + " ")
  17. return
  18. }
  19. action.Callback(m, []string{})
  20. return
  21. }
  22. case BindingChord:
  23. if m.AM.CM.Recording {
  24. if len(binding.Chord) > len(m.AM.CM.Recorded) {
  25. matching := false
  26. for i := range m.AM.CM.Recorded {
  27. if m.AM.CM.Recorded[i] == binding.Chord[i] {
  28. matching = true
  29. } else {
  30. matching = false
  31. break
  32. }
  33. }
  34. if matching {
  35. hasValidContinuingChordPath = true
  36. if binding.Chord[len(m.AM.CM.Recorded)] == util.StandardizeBinding(ev.Name()) {
  37. m.AM.CM.Recorded = append(m.AM.CM.Recorded, util.StandardizeBinding(ev.Name()))
  38. if len(m.AM.CM.Recorded) == len(binding.Chord) {
  39. action.Callback(m, []string{})
  40. m.AM.CM = NewChordManager()
  41. return
  42. }
  43. } else {
  44. continue
  45. }
  46. } else {
  47. continue
  48. }
  49. } else {
  50. continue
  51. }
  52. } else {
  53. if binding.Chord[0] == util.StandardizeBinding(ev.Name()) {
  54. hasValidContinuingChordPath = true
  55. m.AM.CM.Recorded = append(m.AM.CM.Recorded, util.StandardizeBinding(ev.Name()))
  56. if len(m.AM.CM.Recorded) == len(binding.Chord) {
  57. action.Callback(m, []string{})
  58. m.AM.CM = NewChordManager()
  59. return
  60. } else {
  61. m.AM.CM.Recording = true
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. if !hasValidContinuingChordPath {
  69. m.AM.CM = NewChordManager()
  70. }
  71. if m.Mode == ModeWrite {
  72. if ev.Key() == tcell.KeyRune {
  73. m.BM.Current().Insert(ev.Str())
  74. }
  75. }
  76. if m.Mode == ModePalette {
  77. if ev.Key() == tcell.KeyRune {
  78. m.BM.PaletteBuffer.Insert(ev.Str())
  79. }
  80. }
  81. }