commands.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package dialog
  2. import (
  3. "github.com/charmbracelet/bubbles/key"
  4. tea "github.com/charmbracelet/bubbletea"
  5. "github.com/charmbracelet/lipgloss"
  6. "github.com/opencode-ai/opencode/internal/tui/layout"
  7. "github.com/opencode-ai/opencode/internal/tui/styles"
  8. "github.com/opencode-ai/opencode/internal/tui/util"
  9. )
  10. // Command represents a command that can be executed
  11. type Command struct {
  12. ID string
  13. Title string
  14. Description string
  15. Handler func(cmd Command) tea.Cmd
  16. }
  17. // CommandSelectedMsg is sent when a command is selected
  18. type CommandSelectedMsg struct {
  19. Command Command
  20. }
  21. // CloseCommandDialogMsg is sent when the command dialog is closed
  22. type CloseCommandDialogMsg struct{}
  23. // CommandDialog interface for the command selection dialog
  24. type CommandDialog interface {
  25. tea.Model
  26. layout.Bindings
  27. SetCommands(commands []Command)
  28. SetSelectedCommand(commandID string)
  29. }
  30. type commandDialogCmp struct {
  31. commands []Command
  32. selectedIdx int
  33. width int
  34. height int
  35. selectedCommandID string
  36. }
  37. type commandKeyMap struct {
  38. Up key.Binding
  39. Down key.Binding
  40. Enter key.Binding
  41. Escape key.Binding
  42. J key.Binding
  43. K key.Binding
  44. }
  45. var commandKeys = commandKeyMap{
  46. Up: key.NewBinding(
  47. key.WithKeys("up"),
  48. key.WithHelp("↑", "previous command"),
  49. ),
  50. Down: key.NewBinding(
  51. key.WithKeys("down"),
  52. key.WithHelp("↓", "next command"),
  53. ),
  54. Enter: key.NewBinding(
  55. key.WithKeys("enter"),
  56. key.WithHelp("enter", "select command"),
  57. ),
  58. Escape: key.NewBinding(
  59. key.WithKeys("esc"),
  60. key.WithHelp("esc", "close"),
  61. ),
  62. J: key.NewBinding(
  63. key.WithKeys("j"),
  64. key.WithHelp("j", "next command"),
  65. ),
  66. K: key.NewBinding(
  67. key.WithKeys("k"),
  68. key.WithHelp("k", "previous command"),
  69. ),
  70. }
  71. func (c *commandDialogCmp) Init() tea.Cmd {
  72. return nil
  73. }
  74. func (c *commandDialogCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  75. switch msg := msg.(type) {
  76. case tea.KeyMsg:
  77. switch {
  78. case key.Matches(msg, commandKeys.Up) || key.Matches(msg, commandKeys.K):
  79. if c.selectedIdx > 0 {
  80. c.selectedIdx--
  81. }
  82. return c, nil
  83. case key.Matches(msg, commandKeys.Down) || key.Matches(msg, commandKeys.J):
  84. if c.selectedIdx < len(c.commands)-1 {
  85. c.selectedIdx++
  86. }
  87. return c, nil
  88. case key.Matches(msg, commandKeys.Enter):
  89. if len(c.commands) > 0 {
  90. return c, util.CmdHandler(CommandSelectedMsg{
  91. Command: c.commands[c.selectedIdx],
  92. })
  93. }
  94. case key.Matches(msg, commandKeys.Escape):
  95. return c, util.CmdHandler(CloseCommandDialogMsg{})
  96. }
  97. case tea.WindowSizeMsg:
  98. c.width = msg.Width
  99. c.height = msg.Height
  100. }
  101. return c, nil
  102. }
  103. func (c *commandDialogCmp) View() string {
  104. if len(c.commands) == 0 {
  105. return styles.BaseStyle.Padding(1, 2).
  106. Border(lipgloss.RoundedBorder()).
  107. BorderBackground(styles.Background).
  108. BorderForeground(styles.ForgroundDim).
  109. Width(40).
  110. Render("No commands available")
  111. }
  112. // Calculate max width needed for command titles
  113. maxWidth := 40 // Minimum width
  114. for _, cmd := range c.commands {
  115. if len(cmd.Title) > maxWidth-4 { // Account for padding
  116. maxWidth = len(cmd.Title) + 4
  117. }
  118. if len(cmd.Description) > maxWidth-4 {
  119. maxWidth = len(cmd.Description) + 4
  120. }
  121. }
  122. // Limit height to avoid taking up too much screen space
  123. maxVisibleCommands := min(10, len(c.commands))
  124. // Build the command list
  125. commandItems := make([]string, 0, maxVisibleCommands)
  126. startIdx := 0
  127. // If we have more commands than can be displayed, adjust the start index
  128. if len(c.commands) > maxVisibleCommands {
  129. // Center the selected item when possible
  130. halfVisible := maxVisibleCommands / 2
  131. if c.selectedIdx >= halfVisible && c.selectedIdx < len(c.commands)-halfVisible {
  132. startIdx = c.selectedIdx - halfVisible
  133. } else if c.selectedIdx >= len(c.commands)-halfVisible {
  134. startIdx = len(c.commands) - maxVisibleCommands
  135. }
  136. }
  137. endIdx := min(startIdx+maxVisibleCommands, len(c.commands))
  138. for i := startIdx; i < endIdx; i++ {
  139. cmd := c.commands[i]
  140. itemStyle := styles.BaseStyle.Width(maxWidth)
  141. descStyle := styles.BaseStyle.Width(maxWidth).Foreground(styles.ForgroundDim)
  142. if i == c.selectedIdx {
  143. itemStyle = itemStyle.
  144. Background(styles.PrimaryColor).
  145. Foreground(styles.Background).
  146. Bold(true)
  147. descStyle = descStyle.
  148. Background(styles.PrimaryColor).
  149. Foreground(styles.Background)
  150. }
  151. title := itemStyle.Padding(0, 1).Render(cmd.Title)
  152. description := ""
  153. if cmd.Description != "" {
  154. description = descStyle.Padding(0, 1).Render(cmd.Description)
  155. commandItems = append(commandItems, lipgloss.JoinVertical(lipgloss.Left, title, description))
  156. } else {
  157. commandItems = append(commandItems, title)
  158. }
  159. }
  160. title := styles.BaseStyle.
  161. Foreground(styles.PrimaryColor).
  162. Bold(true).
  163. Width(maxWidth).
  164. Padding(0, 1).
  165. Render("Commands")
  166. content := lipgloss.JoinVertical(
  167. lipgloss.Left,
  168. title,
  169. styles.BaseStyle.Width(maxWidth).Render(""),
  170. styles.BaseStyle.Width(maxWidth).Render(lipgloss.JoinVertical(lipgloss.Left, commandItems...)),
  171. styles.BaseStyle.Width(maxWidth).Render(""),
  172. )
  173. return styles.BaseStyle.Padding(1, 2).
  174. Border(lipgloss.RoundedBorder()).
  175. BorderBackground(styles.Background).
  176. BorderForeground(styles.ForgroundDim).
  177. Width(lipgloss.Width(content) + 4).
  178. Render(content)
  179. }
  180. func (c *commandDialogCmp) BindingKeys() []key.Binding {
  181. return layout.KeyMapToSlice(commandKeys)
  182. }
  183. func (c *commandDialogCmp) SetCommands(commands []Command) {
  184. c.commands = commands
  185. // If we have a selected command ID, find its index
  186. if c.selectedCommandID != "" {
  187. for i, cmd := range commands {
  188. if cmd.ID == c.selectedCommandID {
  189. c.selectedIdx = i
  190. return
  191. }
  192. }
  193. }
  194. // Default to first command if selected not found
  195. c.selectedIdx = 0
  196. }
  197. func (c *commandDialogCmp) SetSelectedCommand(commandID string) {
  198. c.selectedCommandID = commandID
  199. // Update the selected index if commands are already loaded
  200. if len(c.commands) > 0 {
  201. for i, cmd := range c.commands {
  202. if cmd.ID == commandID {
  203. c.selectedIdx = i
  204. return
  205. }
  206. }
  207. }
  208. }
  209. // NewCommandDialogCmp creates a new command selection dialog
  210. func NewCommandDialogCmp() CommandDialog {
  211. return &commandDialogCmp{
  212. commands: []Command{},
  213. selectedIdx: 0,
  214. selectedCommandID: "",
  215. }
  216. }