command.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package commands
  2. import (
  3. "encoding/json"
  4. "slices"
  5. "strings"
  6. tea "github.com/charmbracelet/bubbletea/v2"
  7. "github.com/sst/opencode-sdk-go"
  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. FileListCommand CommandName = "file_list"
  73. FileCloseCommand CommandName = "file_close"
  74. FileSearchCommand CommandName = "file_search"
  75. FileDiffToggleCommand CommandName = "file_diff_toggle"
  76. ProjectInitCommand CommandName = "project_init"
  77. InputClearCommand CommandName = "input_clear"
  78. InputPasteCommand CommandName = "input_paste"
  79. InputSubmitCommand CommandName = "input_submit"
  80. InputNewlineCommand CommandName = "input_newline"
  81. MessagesPageUpCommand CommandName = "messages_page_up"
  82. MessagesPageDownCommand CommandName = "messages_page_down"
  83. MessagesHalfPageUpCommand CommandName = "messages_half_page_up"
  84. MessagesHalfPageDownCommand CommandName = "messages_half_page_down"
  85. MessagesPreviousCommand CommandName = "messages_previous"
  86. MessagesNextCommand CommandName = "messages_next"
  87. MessagesFirstCommand CommandName = "messages_first"
  88. MessagesLastCommand CommandName = "messages_last"
  89. MessagesLayoutToggleCommand CommandName = "messages_layout_toggle"
  90. MessagesCopyCommand CommandName = "messages_copy"
  91. MessagesRevertCommand CommandName = "messages_revert"
  92. AppExitCommand CommandName = "app_exit"
  93. )
  94. func (k Command) Matches(msg tea.KeyPressMsg, leader bool) bool {
  95. for _, binding := range k.Keybindings {
  96. if binding.Matches(msg, leader) {
  97. return true
  98. }
  99. }
  100. return false
  101. }
  102. func parseBindings(bindings ...string) []Keybinding {
  103. var parsedBindings []Keybinding
  104. for _, binding := range bindings {
  105. for p := range strings.SplitSeq(binding, ",") {
  106. requireLeader := strings.HasPrefix(p, "<leader>")
  107. keybinding := strings.ReplaceAll(p, "<leader>", "")
  108. keybinding = strings.TrimSpace(keybinding)
  109. parsedBindings = append(parsedBindings, Keybinding{
  110. RequiresLeader: requireLeader,
  111. Key: keybinding,
  112. })
  113. }
  114. }
  115. return parsedBindings
  116. }
  117. func LoadFromConfig(config *opencode.Config) CommandRegistry {
  118. defaults := []Command{
  119. {
  120. Name: AppHelpCommand,
  121. Description: "show help",
  122. Keybindings: parseBindings("<leader>h"),
  123. Trigger: "help",
  124. },
  125. {
  126. Name: EditorOpenCommand,
  127. Description: "open editor",
  128. Keybindings: parseBindings("<leader>e"),
  129. Trigger: "editor",
  130. },
  131. {
  132. Name: SessionNewCommand,
  133. Description: "new session",
  134. Keybindings: parseBindings("<leader>n"),
  135. Trigger: "new",
  136. },
  137. {
  138. Name: SessionListCommand,
  139. Description: "list sessions",
  140. Keybindings: parseBindings("<leader>l"),
  141. Trigger: "sessions",
  142. },
  143. {
  144. Name: SessionShareCommand,
  145. Description: "share session",
  146. Keybindings: parseBindings("<leader>s"),
  147. Trigger: "share",
  148. },
  149. {
  150. Name: SessionInterruptCommand,
  151. Description: "interrupt session",
  152. Keybindings: parseBindings("esc"),
  153. },
  154. {
  155. Name: SessionCompactCommand,
  156. Description: "compact the session",
  157. Keybindings: parseBindings("<leader>c"),
  158. Trigger: "compact",
  159. },
  160. {
  161. Name: ToolDetailsCommand,
  162. Description: "toggle tool details",
  163. Keybindings: parseBindings("<leader>d"),
  164. Trigger: "details",
  165. },
  166. {
  167. Name: ModelListCommand,
  168. Description: "list models",
  169. Keybindings: parseBindings("<leader>m"),
  170. Trigger: "models",
  171. },
  172. {
  173. Name: ThemeListCommand,
  174. Description: "list themes",
  175. Keybindings: parseBindings("<leader>t"),
  176. Trigger: "themes",
  177. },
  178. {
  179. Name: FileListCommand,
  180. Description: "list files",
  181. Keybindings: parseBindings("<leader>f"),
  182. Trigger: "files",
  183. },
  184. {
  185. Name: FileCloseCommand,
  186. Description: "close file",
  187. Keybindings: parseBindings("esc"),
  188. },
  189. {
  190. Name: FileSearchCommand,
  191. Description: "search file",
  192. Keybindings: parseBindings("<leader>/"),
  193. },
  194. {
  195. Name: FileDiffToggleCommand,
  196. Description: "split/unified diff",
  197. Keybindings: parseBindings("<leader>v"),
  198. },
  199. {
  200. Name: ProjectInitCommand,
  201. Description: "create/update AGENTS.md",
  202. Keybindings: parseBindings("<leader>i"),
  203. Trigger: "init",
  204. },
  205. {
  206. Name: InputClearCommand,
  207. Description: "clear input",
  208. Keybindings: parseBindings("ctrl+c"),
  209. },
  210. {
  211. Name: InputPasteCommand,
  212. Description: "paste content",
  213. Keybindings: parseBindings("ctrl+v"),
  214. },
  215. {
  216. Name: InputSubmitCommand,
  217. Description: "submit message",
  218. Keybindings: parseBindings("enter"),
  219. },
  220. {
  221. Name: InputNewlineCommand,
  222. Description: "insert newline",
  223. Keybindings: parseBindings("shift+enter", "ctrl+j"),
  224. },
  225. {
  226. Name: MessagesPageUpCommand,
  227. Description: "page up",
  228. Keybindings: parseBindings("pgup"),
  229. },
  230. {
  231. Name: MessagesPageDownCommand,
  232. Description: "page down",
  233. Keybindings: parseBindings("pgdown"),
  234. },
  235. {
  236. Name: MessagesHalfPageUpCommand,
  237. Description: "half page up",
  238. Keybindings: parseBindings("ctrl+alt+u"),
  239. },
  240. {
  241. Name: MessagesHalfPageDownCommand,
  242. Description: "half page down",
  243. Keybindings: parseBindings("ctrl+alt+d"),
  244. },
  245. {
  246. Name: MessagesPreviousCommand,
  247. Description: "previous message",
  248. Keybindings: parseBindings("ctrl+up"),
  249. },
  250. {
  251. Name: MessagesNextCommand,
  252. Description: "next message",
  253. Keybindings: parseBindings("ctrl+down"),
  254. },
  255. {
  256. Name: MessagesFirstCommand,
  257. Description: "first message",
  258. Keybindings: parseBindings("ctrl+g"),
  259. },
  260. {
  261. Name: MessagesLastCommand,
  262. Description: "last message",
  263. Keybindings: parseBindings("ctrl+alt+g"),
  264. },
  265. {
  266. Name: MessagesLayoutToggleCommand,
  267. Description: "toggle layout",
  268. Keybindings: parseBindings("<leader>p"),
  269. },
  270. {
  271. Name: MessagesCopyCommand,
  272. Description: "copy message",
  273. Keybindings: parseBindings("<leader>y"),
  274. },
  275. {
  276. Name: MessagesRevertCommand,
  277. Description: "revert message",
  278. Keybindings: parseBindings("<leader>u"),
  279. },
  280. {
  281. Name: AppExitCommand,
  282. Description: "exit the app",
  283. Keybindings: parseBindings("ctrl+c", "<leader>q"),
  284. Trigger: "exit",
  285. },
  286. }
  287. registry := make(CommandRegistry)
  288. keybinds := map[string]string{}
  289. marshalled, _ := json.Marshal(config.Keybinds)
  290. json.Unmarshal(marshalled, &keybinds)
  291. for _, command := range defaults {
  292. if keybind, ok := keybinds[string(command.Name)]; ok && keybind != "" {
  293. command.Keybindings = parseBindings(keybind)
  294. }
  295. registry[command.Name] = command
  296. }
  297. return registry
  298. }