keys.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package editor
  2. import (
  3. "github.com/charmbracelet/bubbles/v2/key"
  4. )
  5. type EditorKeyMap struct {
  6. AddFile key.Binding
  7. SendMessage key.Binding
  8. OpenEditor key.Binding
  9. Newline key.Binding
  10. }
  11. func DefaultEditorKeyMap() EditorKeyMap {
  12. return EditorKeyMap{
  13. AddFile: key.NewBinding(
  14. key.WithKeys("/"),
  15. key.WithHelp("/", "add file"),
  16. ),
  17. SendMessage: key.NewBinding(
  18. key.WithKeys("enter"),
  19. key.WithHelp("enter", "send"),
  20. ),
  21. OpenEditor: key.NewBinding(
  22. key.WithKeys("ctrl+o"),
  23. key.WithHelp("ctrl+o", "open editor"),
  24. ),
  25. Newline: key.NewBinding(
  26. key.WithKeys("shift+enter", "ctrl+j"),
  27. // "ctrl+j" is a common keybinding for newline in many editors. If
  28. // the terminal supports "shift+enter", we substitute the help text
  29. // to reflect that.
  30. key.WithHelp("ctrl+j", "newline"),
  31. ),
  32. }
  33. }
  34. // KeyBindings implements layout.KeyMapProvider
  35. func (k EditorKeyMap) KeyBindings() []key.Binding {
  36. return []key.Binding{
  37. k.AddFile,
  38. k.SendMessage,
  39. k.OpenEditor,
  40. k.Newline,
  41. AttachmentsKeyMaps.AttachmentDeleteMode,
  42. AttachmentsKeyMaps.DeleteAllAttachments,
  43. AttachmentsKeyMaps.Escape,
  44. }
  45. }
  46. type DeleteAttachmentKeyMaps struct {
  47. AttachmentDeleteMode key.Binding
  48. Escape key.Binding
  49. DeleteAllAttachments key.Binding
  50. }
  51. // TODO: update this to use the new keymap concepts
  52. var AttachmentsKeyMaps = DeleteAttachmentKeyMaps{
  53. AttachmentDeleteMode: key.NewBinding(
  54. key.WithKeys("ctrl+r"),
  55. key.WithHelp("ctrl+r+{i}", "delete attachment at index i"),
  56. ),
  57. Escape: key.NewBinding(
  58. key.WithKeys("esc"),
  59. key.WithHelp("esc", "cancel delete mode"),
  60. ),
  61. DeleteAllAttachments: key.NewBinding(
  62. key.WithKeys("r"),
  63. key.WithHelp("ctrl+r+r", "delete all attachments"),
  64. ),
  65. }