1
0

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