keymap.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package session
  2. import (
  3. "github.com/charmbracelet/bubbles/key"
  4. "github.com/remogatto/sugarfoam/components/form"
  5. "github.com/remogatto/sugarfoam/components/group"
  6. "github.com/remogatto/sugarfoam/components/table"
  7. "github.com/remogatto/sugarfoam/components/viewport"
  8. )
  9. type keyBindings struct {
  10. group *group.Model
  11. quit, enter key.Binding
  12. }
  13. func (k *keyBindings) ShortHelp() []key.Binding {
  14. keys := make([]key.Binding, 0)
  15. current := k.group.Current()
  16. switch item := current.(type) {
  17. case *table.Model:
  18. keys = append(
  19. keys,
  20. item.KeyMap.LineUp,
  21. item.KeyMap.LineDown,
  22. )
  23. case *viewport.Model:
  24. keys = append(
  25. keys,
  26. item.KeyMap.Up,
  27. item.KeyMap.Down,
  28. )
  29. case *form.Model:
  30. keys = append(
  31. keys,
  32. item.KeyBinds()...,
  33. )
  34. }
  35. keys = append(
  36. keys,
  37. k.group.KeyMap.FocusNext,
  38. k.group.KeyMap.FocusPrev,
  39. k.quit,
  40. )
  41. return keys
  42. }
  43. func (k keyBindings) FullHelp() [][]key.Binding {
  44. return [][]key.Binding{
  45. {
  46. k.quit,
  47. },
  48. }
  49. }
  50. func newBindings(g *group.Model) *keyBindings {
  51. return &keyBindings{
  52. group: g,
  53. quit: key.NewBinding(
  54. key.WithKeys("esc"), key.WithHelp("esc", "quit app"),
  55. ),
  56. }
  57. }