command.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package commands
  2. import (
  3. "encoding/json"
  4. "slices"
  5. "strings"
  6. tea "github.com/charmbracelet/bubbletea/v2"
  7. "github.com/sst/opencode/pkg/client"
  8. )
  9. type ExecuteCommandMsg Command
  10. type ExecuteCommandsMsg []Command
  11. type CommandExecutedMsg Command
  12. type Keybinding struct {
  13. RequiresLeader bool
  14. Key string
  15. }
  16. func (k Keybinding) Matches(msg tea.KeyPressMsg, leader bool) bool {
  17. key := k.Key
  18. key = strings.TrimSpace(key)
  19. return key == msg.String() && (k.RequiresLeader == leader)
  20. }
  21. type CommandName string
  22. type Command struct {
  23. Name CommandName
  24. Description string
  25. Keybindings []Keybinding
  26. Trigger string
  27. }
  28. func (c Command) Keys() []string {
  29. var keys []string
  30. for _, k := range c.Keybindings {
  31. keys = append(keys, k.Key)
  32. }
  33. return keys
  34. }
  35. type CommandRegistry map[CommandName]Command
  36. func (r CommandRegistry) Sorted() []Command {
  37. var commands []Command
  38. for _, command := range r {
  39. commands = append(commands, command)
  40. }
  41. slices.SortFunc(commands, func(a, b Command) int {
  42. if a.Name == AppExitCommand {
  43. return 1
  44. }
  45. if b.Name == AppExitCommand {
  46. return -1
  47. }
  48. return strings.Compare(string(a.Name), string(b.Name))
  49. })
  50. return commands
  51. }
  52. func (r CommandRegistry) Matches(msg tea.KeyPressMsg, leader bool) []Command {
  53. var matched []Command
  54. for _, command := range r.Sorted() {
  55. if command.Matches(msg, leader) {
  56. matched = append(matched, command)
  57. }
  58. }
  59. return matched
  60. }
  61. const (
  62. AppHelpCommand CommandName = "app_help"
  63. EditorOpenCommand CommandName = "editor_open"
  64. SessionNewCommand CommandName = "session_new"
  65. SessionListCommand CommandName = "session_list"
  66. SessionShareCommand CommandName = "session_share"
  67. SessionInterruptCommand CommandName = "session_interrupt"
  68. SessionCompactCommand CommandName = "session_compact"
  69. ToolDetailsCommand CommandName = "tool_details"
  70. ModelListCommand CommandName = "model_list"
  71. ThemeListCommand CommandName = "theme_list"
  72. ProjectInitCommand CommandName = "project_init"
  73. InputClearCommand CommandName = "input_clear"
  74. InputPasteCommand CommandName = "input_paste"
  75. InputSubmitCommand CommandName = "input_submit"
  76. InputNewlineCommand CommandName = "input_newline"
  77. HistoryPreviousCommand CommandName = "history_previous"
  78. HistoryNextCommand CommandName = "history_next"
  79. MessagesPageUpCommand CommandName = "messages_page_up"
  80. MessagesPageDownCommand CommandName = "messages_page_down"
  81. MessagesHalfPageUpCommand CommandName = "messages_half_page_up"
  82. MessagesHalfPageDownCommand CommandName = "messages_half_page_down"
  83. MessagesPreviousCommand CommandName = "messages_previous"
  84. MessagesNextCommand CommandName = "messages_next"
  85. MessagesFirstCommand CommandName = "messages_first"
  86. MessagesLastCommand CommandName = "messages_last"
  87. AppExitCommand CommandName = "app_exit"
  88. )
  89. func (k Command) Matches(msg tea.KeyPressMsg, leader bool) bool {
  90. for _, binding := range k.Keybindings {
  91. if binding.Matches(msg, leader) {
  92. return true
  93. }
  94. }
  95. return false
  96. }
  97. func parseBindings(bindings ...string) []Keybinding {
  98. var parsedBindings []Keybinding
  99. for _, binding := range bindings {
  100. for p := range strings.SplitSeq(binding, ",") {
  101. requireLeader := strings.HasPrefix(p, "<leader>")
  102. keybinding := strings.ReplaceAll(p, "<leader>", "")
  103. keybinding = strings.TrimSpace(keybinding)
  104. parsedBindings = append(parsedBindings, Keybinding{
  105. RequiresLeader: requireLeader,
  106. Key: keybinding,
  107. })
  108. }
  109. }
  110. return parsedBindings
  111. }
  112. func LoadFromConfig(config *client.ConfigInfo) CommandRegistry {
  113. defaults := []Command{
  114. {
  115. Name: AppHelpCommand,
  116. Description: "show help",
  117. Keybindings: parseBindings("<leader>h"),
  118. Trigger: "help",
  119. },
  120. {
  121. Name: EditorOpenCommand,
  122. Description: "open editor",
  123. Keybindings: parseBindings("<leader>e"),
  124. Trigger: "editor",
  125. },
  126. {
  127. Name: SessionNewCommand,
  128. Description: "new session",
  129. Keybindings: parseBindings("<leader>n"),
  130. Trigger: "new",
  131. },
  132. {
  133. Name: SessionListCommand,
  134. Description: "list sessions",
  135. Keybindings: parseBindings("<leader>l"),
  136. Trigger: "sessions",
  137. },
  138. {
  139. Name: SessionShareCommand,
  140. Description: "share session",
  141. Keybindings: parseBindings("<leader>s"),
  142. Trigger: "share",
  143. },
  144. {
  145. Name: SessionInterruptCommand,
  146. Description: "interrupt session",
  147. Keybindings: parseBindings("esc"),
  148. },
  149. {
  150. Name: SessionCompactCommand,
  151. Description: "compact the session",
  152. Keybindings: parseBindings("<leader>c"),
  153. Trigger: "compact",
  154. },
  155. {
  156. Name: ToolDetailsCommand,
  157. Description: "toggle tool details",
  158. Keybindings: parseBindings("<leader>d"),
  159. Trigger: "details",
  160. },
  161. {
  162. Name: ModelListCommand,
  163. Description: "list models",
  164. Keybindings: parseBindings("<leader>m"),
  165. Trigger: "models",
  166. },
  167. {
  168. Name: ThemeListCommand,
  169. Description: "list themes",
  170. Keybindings: parseBindings("<leader>t"),
  171. Trigger: "themes",
  172. },
  173. {
  174. Name: ProjectInitCommand,
  175. Description: "create/update AGENTS.md",
  176. Keybindings: parseBindings("<leader>i"),
  177. Trigger: "init",
  178. },
  179. {
  180. Name: InputClearCommand,
  181. Description: "clear input",
  182. Keybindings: parseBindings("ctrl+c"),
  183. },
  184. {
  185. Name: InputPasteCommand,
  186. Description: "paste content",
  187. Keybindings: parseBindings("ctrl+v"),
  188. },
  189. {
  190. Name: InputSubmitCommand,
  191. Description: "submit message",
  192. Keybindings: parseBindings("enter"),
  193. },
  194. {
  195. Name: InputNewlineCommand,
  196. Description: "insert newline",
  197. Keybindings: parseBindings("shift+enter", "ctrl+j"),
  198. },
  199. // {
  200. // Name: HistoryPreviousCommand,
  201. // Description: "previous prompt",
  202. // Keybindings: parseBindings("up"),
  203. // },
  204. // {
  205. // Name: HistoryNextCommand,
  206. // Description: "next prompt",
  207. // Keybindings: parseBindings("down"),
  208. // },
  209. {
  210. Name: MessagesPageUpCommand,
  211. Description: "page up",
  212. Keybindings: parseBindings("pgup"),
  213. },
  214. {
  215. Name: MessagesPageDownCommand,
  216. Description: "page down",
  217. Keybindings: parseBindings("pgdown"),
  218. },
  219. {
  220. Name: MessagesHalfPageUpCommand,
  221. Description: "half page up",
  222. Keybindings: parseBindings("ctrl+alt+u"),
  223. },
  224. {
  225. Name: MessagesHalfPageDownCommand,
  226. Description: "half page down",
  227. Keybindings: parseBindings("ctrl+alt+d"),
  228. },
  229. {
  230. Name: MessagesPreviousCommand,
  231. Description: "previous message",
  232. Keybindings: parseBindings("ctrl+alt+k"),
  233. },
  234. {
  235. Name: MessagesNextCommand,
  236. Description: "next message",
  237. Keybindings: parseBindings("ctrl+alt+j"),
  238. },
  239. {
  240. Name: MessagesFirstCommand,
  241. Description: "first message",
  242. Keybindings: parseBindings("ctrl+g"),
  243. },
  244. {
  245. Name: MessagesLastCommand,
  246. Description: "last message",
  247. Keybindings: parseBindings("ctrl+alt+g"),
  248. },
  249. {
  250. Name: AppExitCommand,
  251. Description: "exit the app",
  252. Keybindings: parseBindings("ctrl+c", "<leader>q"),
  253. Trigger: "exit",
  254. },
  255. }
  256. registry := make(CommandRegistry)
  257. keybinds := map[string]string{}
  258. marshalled, _ := json.Marshal(*config.Keybinds)
  259. json.Unmarshal(marshalled, &keybinds)
  260. for _, command := range defaults {
  261. if keybind, ok := keybinds[string(command.Name)]; ok {
  262. command.Keybindings = parseBindings(keybind)
  263. }
  264. registry[command.Name] = command
  265. }
  266. return registry
  267. }