keys.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package commands
  2. import (
  3. "github.com/charmbracelet/bubbles/v2/key"
  4. )
  5. type CommandsDialogKeyMap struct {
  6. Select,
  7. Next,
  8. Previous,
  9. Tab,
  10. Close key.Binding
  11. }
  12. func DefaultCommandsDialogKeyMap() CommandsDialogKeyMap {
  13. return CommandsDialogKeyMap{
  14. Select: key.NewBinding(
  15. key.WithKeys("enter", "ctrl+y"),
  16. key.WithHelp("enter", "confirm"),
  17. ),
  18. Next: key.NewBinding(
  19. key.WithKeys("down", "ctrl+n"),
  20. key.WithHelp("↓", "next item"),
  21. ),
  22. Previous: key.NewBinding(
  23. key.WithKeys("up", "ctrl+p"),
  24. key.WithHelp("↑", "previous item"),
  25. ),
  26. Tab: key.NewBinding(
  27. key.WithKeys("tab"),
  28. key.WithHelp("tab", "switch selection"),
  29. ),
  30. Close: key.NewBinding(
  31. key.WithKeys("esc", "alt+esc"),
  32. key.WithHelp("esc", "cancel"),
  33. ),
  34. }
  35. }
  36. // KeyBindings implements layout.KeyMapProvider
  37. func (k CommandsDialogKeyMap) KeyBindings() []key.Binding {
  38. return []key.Binding{
  39. k.Select,
  40. k.Next,
  41. k.Previous,
  42. k.Tab,
  43. k.Close,
  44. }
  45. }
  46. // FullHelp implements help.KeyMap.
  47. func (k CommandsDialogKeyMap) FullHelp() [][]key.Binding {
  48. m := [][]key.Binding{}
  49. slice := k.KeyBindings()
  50. for i := 0; i < len(slice); i += 4 {
  51. end := min(i+4, len(slice))
  52. m = append(m, slice[i:end])
  53. }
  54. return m
  55. }
  56. // ShortHelp implements help.KeyMap.
  57. func (k CommandsDialogKeyMap) ShortHelp() []key.Binding {
  58. return []key.Binding{
  59. k.Tab,
  60. key.NewBinding(
  61. key.WithKeys("down", "up"),
  62. key.WithHelp("↑↓", "choose"),
  63. ),
  64. k.Select,
  65. k.Close,
  66. }
  67. }
  68. type ArgumentsDialogKeyMap struct {
  69. Confirm key.Binding
  70. Next key.Binding
  71. Previous key.Binding
  72. Close key.Binding
  73. }
  74. func DefaultArgumentsDialogKeyMap() ArgumentsDialogKeyMap {
  75. return ArgumentsDialogKeyMap{
  76. Confirm: key.NewBinding(
  77. key.WithKeys("enter"),
  78. key.WithHelp("enter", "confirm"),
  79. ),
  80. Next: key.NewBinding(
  81. key.WithKeys("tab", "down"),
  82. key.WithHelp("tab/↓", "next"),
  83. ),
  84. Previous: key.NewBinding(
  85. key.WithKeys("shift+tab", "up"),
  86. key.WithHelp("shift+tab/↑", "previous"),
  87. ),
  88. Close: key.NewBinding(
  89. key.WithKeys("esc", "alt+esc"),
  90. key.WithHelp("esc", "cancel"),
  91. ),
  92. }
  93. }
  94. // KeyBindings implements layout.KeyMapProvider
  95. func (k ArgumentsDialogKeyMap) KeyBindings() []key.Binding {
  96. return []key.Binding{
  97. k.Confirm,
  98. k.Next,
  99. k.Previous,
  100. k.Close,
  101. }
  102. }
  103. // FullHelp implements help.KeyMap.
  104. func (k ArgumentsDialogKeyMap) FullHelp() [][]key.Binding {
  105. m := [][]key.Binding{}
  106. slice := k.KeyBindings()
  107. for i := 0; i < len(slice); i += 4 {
  108. end := min(i+4, len(slice))
  109. m = append(m, slice[i:end])
  110. }
  111. return m
  112. }
  113. // ShortHelp implements help.KeyMap.
  114. func (k ArgumentsDialogKeyMap) ShortHelp() []key.Binding {
  115. return []key.Binding{
  116. k.Confirm,
  117. k.Next,
  118. k.Previous,
  119. k.Close,
  120. }
  121. }