arguments.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package dialog
  2. import (
  3. "fmt"
  4. "github.com/charmbracelet/bubbles/v2/key"
  5. "github.com/charmbracelet/bubbles/v2/textinput"
  6. tea "github.com/charmbracelet/bubbletea/v2"
  7. "github.com/charmbracelet/lipgloss/v2"
  8. "github.com/sst/opencode/internal/styles"
  9. "github.com/sst/opencode/internal/theme"
  10. "github.com/sst/opencode/internal/util"
  11. )
  12. type argumentsDialogKeyMap struct {
  13. Enter key.Binding
  14. Escape key.Binding
  15. }
  16. // ShortHelp implements key.Map.
  17. func (k argumentsDialogKeyMap) ShortHelp() []key.Binding {
  18. return []key.Binding{
  19. key.NewBinding(
  20. key.WithKeys("enter"),
  21. key.WithHelp("enter", "confirm"),
  22. ),
  23. key.NewBinding(
  24. key.WithKeys("esc"),
  25. key.WithHelp("esc", "cancel"),
  26. ),
  27. }
  28. }
  29. // FullHelp implements key.Map.
  30. func (k argumentsDialogKeyMap) FullHelp() [][]key.Binding {
  31. return [][]key.Binding{k.ShortHelp()}
  32. }
  33. // ShowMultiArgumentsDialogMsg is a message that is sent to show the multi-arguments dialog.
  34. type ShowMultiArgumentsDialogMsg struct {
  35. CommandID string
  36. Content string
  37. ArgNames []string
  38. }
  39. // CloseMultiArgumentsDialogMsg is a message that is sent when the multi-arguments dialog is closed.
  40. type CloseMultiArgumentsDialogMsg struct {
  41. Submit bool
  42. CommandID string
  43. Content string
  44. Args map[string]string
  45. }
  46. // MultiArgumentsDialogCmp is a component that asks the user for multiple command arguments.
  47. type MultiArgumentsDialogCmp struct {
  48. width, height int
  49. inputs []textinput.Model
  50. focusIndex int
  51. keys argumentsDialogKeyMap
  52. commandID string
  53. content string
  54. argNames []string
  55. }
  56. // NewMultiArgumentsDialogCmp creates a new MultiArgumentsDialogCmp.
  57. func NewMultiArgumentsDialogCmp(commandID, content string, argNames []string) MultiArgumentsDialogCmp {
  58. t := theme.CurrentTheme()
  59. inputs := make([]textinput.Model, len(argNames))
  60. for i, name := range argNames {
  61. ti := textinput.New()
  62. ti.Placeholder = fmt.Sprintf("Enter value for %s...", name)
  63. ti.SetWidth(40)
  64. ti.Prompt = ""
  65. ti.Styles.Blurred.Placeholder = ti.Styles.Blurred.Placeholder.Background(t.Background())
  66. ti.Styles.Blurred.Text = ti.Styles.Blurred.Text.Background(t.Background())
  67. ti.Styles.Blurred.Prompt = ti.Styles.Blurred.Prompt.Foreground(t.Primary())
  68. ti.Styles.Focused.Placeholder = ti.Styles.Focused.Placeholder.Background(t.Background())
  69. ti.Styles.Focused.Text = ti.Styles.Focused.Text.Background(t.Background())
  70. ti.Styles.Focused.Prompt = ti.Styles.Focused.Prompt.Foreground(t.Primary())
  71. // ti.PromptStyle = ti.PromptStyle.Background(t.Background())
  72. // ti.TextStyle = ti.TextStyle.Background(t.Background())
  73. // Only focus the first input initially
  74. if i == 0 {
  75. ti.Focus()
  76. // ti.PromptStyle = ti.PromptStyle.Foreground(t.Primary())
  77. // ti.TextStyle = ti.TextStyle.Foreground(t.Primary())
  78. } else {
  79. ti.Blur()
  80. }
  81. inputs[i] = ti
  82. }
  83. return MultiArgumentsDialogCmp{
  84. inputs: inputs,
  85. keys: argumentsDialogKeyMap{},
  86. commandID: commandID,
  87. content: content,
  88. argNames: argNames,
  89. focusIndex: 0,
  90. }
  91. }
  92. // Init implements tea.Model.
  93. func (m MultiArgumentsDialogCmp) Init() tea.Cmd {
  94. // Make sure only the first input is focused
  95. for i := range m.inputs {
  96. if i == 0 {
  97. m.inputs[i].Focus()
  98. } else {
  99. m.inputs[i].Blur()
  100. }
  101. }
  102. return textinput.Blink
  103. }
  104. // Update implements tea.Model.
  105. func (m MultiArgumentsDialogCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  106. var cmds []tea.Cmd
  107. // t := theme.CurrentTheme()
  108. switch msg := msg.(type) {
  109. case tea.KeyMsg:
  110. switch {
  111. case key.Matches(msg, key.NewBinding(key.WithKeys("esc"))):
  112. return m, util.CmdHandler(CloseMultiArgumentsDialogMsg{
  113. Submit: false,
  114. CommandID: m.commandID,
  115. Content: m.content,
  116. Args: nil,
  117. })
  118. case key.Matches(msg, key.NewBinding(key.WithKeys("enter"))):
  119. // If we're on the last input, submit the form
  120. if m.focusIndex == len(m.inputs)-1 {
  121. args := make(map[string]string)
  122. for i, name := range m.argNames {
  123. args[name] = m.inputs[i].Value()
  124. }
  125. return m, util.CmdHandler(CloseMultiArgumentsDialogMsg{
  126. Submit: true,
  127. CommandID: m.commandID,
  128. Content: m.content,
  129. Args: args,
  130. })
  131. }
  132. // Otherwise, move to the next input
  133. m.inputs[m.focusIndex].Blur()
  134. m.focusIndex++
  135. m.inputs[m.focusIndex].Focus()
  136. // m.inputs[m.focusIndex].PromptStyle = m.inputs[m.focusIndex].PromptStyle.Foreground(t.Primary())
  137. // m.inputs[m.focusIndex].TextStyle = m.inputs[m.focusIndex].TextStyle.Foreground(t.Primary())
  138. case key.Matches(msg, key.NewBinding(key.WithKeys("tab"))):
  139. // Move to the next input
  140. m.inputs[m.focusIndex].Blur()
  141. m.focusIndex = (m.focusIndex + 1) % len(m.inputs)
  142. m.inputs[m.focusIndex].Focus()
  143. // m.inputs[m.focusIndex].PromptStyle = m.inputs[m.focusIndex].PromptStyle.Foreground(t.Primary())
  144. // m.inputs[m.focusIndex].TextStyle = m.inputs[m.focusIndex].TextStyle.Foreground(t.Primary())
  145. case key.Matches(msg, key.NewBinding(key.WithKeys("shift+tab"))):
  146. // Move to the previous input
  147. m.inputs[m.focusIndex].Blur()
  148. m.focusIndex = (m.focusIndex - 1 + len(m.inputs)) % len(m.inputs)
  149. m.inputs[m.focusIndex].Focus()
  150. // m.inputs[m.focusIndex].PromptStyle = m.inputs[m.focusIndex].PromptStyle.Foreground(t.Primary())
  151. // m.inputs[m.focusIndex].TextStyle = m.inputs[m.focusIndex].TextStyle.Foreground(t.Primary())
  152. }
  153. case tea.WindowSizeMsg:
  154. m.width = msg.Width
  155. m.height = msg.Height
  156. }
  157. // Update the focused input
  158. var cmd tea.Cmd
  159. m.inputs[m.focusIndex], cmd = m.inputs[m.focusIndex].Update(msg)
  160. cmds = append(cmds, cmd)
  161. return m, tea.Batch(cmds...)
  162. }
  163. // View implements tea.Model.
  164. func (m MultiArgumentsDialogCmp) View() string {
  165. t := theme.CurrentTheme()
  166. baseStyle := styles.BaseStyle()
  167. // Calculate width needed for content
  168. maxWidth := 60 // Width for explanation text
  169. title := lipgloss.NewStyle().
  170. Foreground(t.Primary()).
  171. Bold(true).
  172. Width(maxWidth).
  173. Padding(0, 1).
  174. Background(t.Background()).
  175. Render("Command Arguments")
  176. explanation := lipgloss.NewStyle().
  177. Foreground(t.Text()).
  178. Width(maxWidth).
  179. Padding(0, 1).
  180. Background(t.Background()).
  181. Render("This command requires multiple arguments. Please enter values for each:")
  182. // Create input fields for each argument
  183. inputFields := make([]string, len(m.inputs))
  184. for i, input := range m.inputs {
  185. // Highlight the label of the focused input
  186. labelStyle := lipgloss.NewStyle().
  187. Width(maxWidth).
  188. Padding(1, 1, 0, 1).
  189. Background(t.Background())
  190. if i == m.focusIndex {
  191. labelStyle = labelStyle.Foreground(t.Primary()).Bold(true)
  192. } else {
  193. labelStyle = labelStyle.Foreground(t.TextMuted())
  194. }
  195. label := labelStyle.Render(m.argNames[i] + ":")
  196. field := lipgloss.NewStyle().
  197. Foreground(t.Text()).
  198. Width(maxWidth).
  199. Padding(0, 1).
  200. Background(t.Background()).
  201. Render(input.View())
  202. inputFields[i] = lipgloss.JoinVertical(lipgloss.Left, label, field)
  203. }
  204. maxWidth = min(maxWidth, m.width-10)
  205. // Join all elements vertically
  206. elements := []string{title, explanation}
  207. elements = append(elements, inputFields...)
  208. content := lipgloss.JoinVertical(
  209. lipgloss.Left,
  210. elements...,
  211. )
  212. return baseStyle.Padding(1, 2).
  213. Border(lipgloss.RoundedBorder()).
  214. BorderBackground(t.Background()).
  215. BorderForeground(t.TextMuted()).
  216. Background(t.Background()).
  217. Width(lipgloss.Width(content) + 4).
  218. Render(content)
  219. }
  220. // SetSize sets the size of the component.
  221. func (m *MultiArgumentsDialogCmp) SetSize(width, height int) {
  222. m.width = width
  223. m.height = height
  224. }
  225. // Bindings implements layout.Bindings.
  226. func (m MultiArgumentsDialogCmp) Bindings() []key.Binding {
  227. return m.keys.ShortHelp()
  228. }