commands.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package commands
  2. import (
  3. "fmt"
  4. "strings"
  5. tea "github.com/charmbracelet/bubbletea/v2"
  6. "github.com/charmbracelet/lipgloss/v2"
  7. "github.com/charmbracelet/lipgloss/v2/compat"
  8. "github.com/sst/opencode/internal/app"
  9. "github.com/sst/opencode/internal/commands"
  10. "github.com/sst/opencode/internal/styles"
  11. "github.com/sst/opencode/internal/theme"
  12. )
  13. type CommandsComponent interface {
  14. tea.ViewModel
  15. SetSize(width, height int) tea.Cmd
  16. SetBackgroundColor(color compat.AdaptiveColor)
  17. }
  18. type commandsComponent struct {
  19. app *app.App
  20. width, height int
  21. showKeybinds bool
  22. showAll bool
  23. background *compat.AdaptiveColor
  24. limit *int
  25. }
  26. func (c *commandsComponent) SetSize(width, height int) tea.Cmd {
  27. c.width = width
  28. c.height = height
  29. return nil
  30. }
  31. func (c *commandsComponent) GetSize() (int, int) {
  32. return c.width, c.height
  33. }
  34. func (c *commandsComponent) SetBackgroundColor(color compat.AdaptiveColor) {
  35. c.background = &color
  36. }
  37. func (c *commandsComponent) View() string {
  38. t := theme.CurrentTheme()
  39. triggerStyle := styles.NewStyle().Foreground(t.Primary()).Bold(true)
  40. descriptionStyle := styles.NewStyle().Foreground(t.Text())
  41. keybindStyle := styles.NewStyle().Foreground(t.TextMuted())
  42. if c.background != nil {
  43. triggerStyle = triggerStyle.Background(*c.background)
  44. descriptionStyle = descriptionStyle.Background(*c.background)
  45. keybindStyle = keybindStyle.Background(*c.background)
  46. }
  47. var commandsToShow []commands.Command
  48. var triggeredCommands []commands.Command
  49. var untriggeredCommands []commands.Command
  50. for _, cmd := range c.app.Commands.Sorted() {
  51. if c.showAll || cmd.Trigger != "" {
  52. if cmd.Trigger != "" {
  53. triggeredCommands = append(triggeredCommands, cmd)
  54. } else if c.showAll {
  55. untriggeredCommands = append(untriggeredCommands, cmd)
  56. }
  57. }
  58. }
  59. // Combine triggered commands first, then untriggered
  60. commandsToShow = append(commandsToShow, triggeredCommands...)
  61. commandsToShow = append(commandsToShow, untriggeredCommands...)
  62. if c.limit != nil && len(commandsToShow) > *c.limit {
  63. commandsToShow = commandsToShow[:*c.limit]
  64. }
  65. if len(commandsToShow) == 0 {
  66. muted := styles.NewStyle().Foreground(theme.CurrentTheme().TextMuted())
  67. if c.showAll {
  68. return muted.Render("No commands available")
  69. }
  70. return muted.Render("No commands with triggers available")
  71. }
  72. // Calculate column widths
  73. maxTriggerWidth := 0
  74. maxDescriptionWidth := 0
  75. maxKeybindWidth := 0
  76. // Prepare command data
  77. type commandRow struct {
  78. trigger string
  79. description string
  80. keybinds string
  81. }
  82. rows := make([]commandRow, 0, len(commandsToShow))
  83. for _, cmd := range commandsToShow {
  84. trigger := ""
  85. if cmd.Trigger != "" {
  86. trigger = "/" + cmd.Trigger
  87. } else {
  88. trigger = string(cmd.Name)
  89. }
  90. description := cmd.Description
  91. // Format keybindings
  92. var keybindStrs []string
  93. if c.showKeybinds {
  94. for _, kb := range cmd.Keybindings {
  95. if kb.RequiresLeader {
  96. keybindStrs = append(keybindStrs, c.app.Config.Keybinds.Leader+" "+kb.Key)
  97. } else {
  98. keybindStrs = append(keybindStrs, kb.Key)
  99. }
  100. }
  101. }
  102. keybinds := strings.Join(keybindStrs, ", ")
  103. rows = append(rows, commandRow{
  104. trigger: trigger,
  105. description: description,
  106. keybinds: keybinds,
  107. })
  108. // Update max widths
  109. if len(trigger) > maxTriggerWidth {
  110. maxTriggerWidth = len(trigger)
  111. }
  112. if len(description) > maxDescriptionWidth {
  113. maxDescriptionWidth = len(description)
  114. }
  115. if len(keybinds) > maxKeybindWidth {
  116. maxKeybindWidth = len(keybinds)
  117. }
  118. }
  119. // Add padding between columns
  120. columnPadding := 3
  121. // Build the output
  122. var output strings.Builder
  123. maxWidth := 0
  124. for _, row := range rows {
  125. // Pad each column to align properly
  126. trigger := fmt.Sprintf("%-*s", maxTriggerWidth, row.trigger)
  127. description := fmt.Sprintf("%-*s", maxDescriptionWidth, row.description)
  128. // Apply styles and combine
  129. line := triggerStyle.Render(trigger) +
  130. triggerStyle.Render(strings.Repeat(" ", columnPadding)) +
  131. descriptionStyle.Render(description)
  132. if c.showKeybinds && row.keybinds != "" {
  133. line += keybindStyle.Render(strings.Repeat(" ", columnPadding)) +
  134. keybindStyle.Render(row.keybinds)
  135. }
  136. output.WriteString(line + "\n")
  137. maxWidth = max(maxWidth, lipgloss.Width(line))
  138. }
  139. // Remove trailing newline
  140. result := strings.TrimSuffix(output.String(), "\n")
  141. if c.background != nil {
  142. result = styles.NewStyle().Background(*c.background).Width(maxWidth).Render(result)
  143. }
  144. return result
  145. }
  146. type Option func(*commandsComponent)
  147. func WithKeybinds(show bool) Option {
  148. return func(c *commandsComponent) {
  149. c.showKeybinds = show
  150. }
  151. }
  152. func WithBackground(background compat.AdaptiveColor) Option {
  153. return func(c *commandsComponent) {
  154. c.background = &background
  155. }
  156. }
  157. func WithLimit(limit int) Option {
  158. return func(c *commandsComponent) {
  159. c.limit = &limit
  160. }
  161. }
  162. func WithShowAll(showAll bool) Option {
  163. return func(c *commandsComponent) {
  164. c.showAll = showAll
  165. }
  166. }
  167. func New(app *app.App, opts ...Option) CommandsComponent {
  168. c := &commandsComponent{
  169. app: app,
  170. background: nil,
  171. showKeybinds: true,
  172. showAll: false,
  173. }
  174. for _, opt := range opts {
  175. opt(c)
  176. }
  177. return c
  178. }