keys.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package quit
  2. import (
  3. "github.com/charmbracelet/bubbles/v2/key"
  4. )
  5. // KeyMap defines the keyboard bindings for the quit dialog.
  6. type KeyMap struct {
  7. LeftRight,
  8. EnterSpace,
  9. Yes,
  10. No,
  11. Tab,
  12. Close key.Binding
  13. }
  14. func DefaultKeymap() KeyMap {
  15. return KeyMap{
  16. LeftRight: key.NewBinding(
  17. key.WithKeys("left", "right"),
  18. key.WithHelp("←/→", "switch options"),
  19. ),
  20. EnterSpace: key.NewBinding(
  21. key.WithKeys("enter", " "),
  22. key.WithHelp("enter/space", "confirm"),
  23. ),
  24. Yes: key.NewBinding(
  25. key.WithKeys("y", "Y", "ctrl+c"),
  26. key.WithHelp("y/Y/ctrl+c", "yes"),
  27. ),
  28. No: key.NewBinding(
  29. key.WithKeys("n", "N"),
  30. key.WithHelp("n/N", "no"),
  31. ),
  32. Tab: key.NewBinding(
  33. key.WithKeys("tab"),
  34. key.WithHelp("tab", "switch options"),
  35. ),
  36. Close: key.NewBinding(
  37. key.WithKeys("esc", "alt+esc"),
  38. key.WithHelp("esc", "cancel"),
  39. ),
  40. }
  41. }
  42. // KeyBindings implements layout.KeyMapProvider
  43. func (k KeyMap) KeyBindings() []key.Binding {
  44. return []key.Binding{
  45. k.LeftRight,
  46. k.EnterSpace,
  47. k.Yes,
  48. k.No,
  49. k.Tab,
  50. k.Close,
  51. }
  52. }
  53. // FullHelp implements help.KeyMap.
  54. func (k KeyMap) FullHelp() [][]key.Binding {
  55. m := [][]key.Binding{}
  56. slice := k.KeyBindings()
  57. for i := 0; i < len(slice); i += 4 {
  58. end := min(i+4, len(slice))
  59. m = append(m, slice[i:end])
  60. }
  61. return m
  62. }
  63. // ShortHelp implements help.KeyMap.
  64. func (k KeyMap) ShortHelp() []key.Binding {
  65. return []key.Binding{
  66. k.LeftRight,
  67. k.EnterSpace,
  68. }
  69. }