1
0

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