|
|
@@ -2,10 +2,14 @@ package completions
|
|
|
|
|
|
import (
|
|
|
"sort"
|
|
|
+ "strings"
|
|
|
|
|
|
+ "github.com/charmbracelet/lipgloss/v2"
|
|
|
"github.com/lithammer/fuzzysearch/fuzzy"
|
|
|
"github.com/sst/opencode/internal/app"
|
|
|
+ "github.com/sst/opencode/internal/commands"
|
|
|
"github.com/sst/opencode/internal/components/dialog"
|
|
|
+ "github.com/sst/opencode/internal/theme"
|
|
|
)
|
|
|
|
|
|
type CommandCompletionProvider struct {
|
|
|
@@ -27,15 +31,36 @@ func (c *CommandCompletionProvider) GetEntry() dialog.CompletionItemI {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+func (c *CommandCompletionProvider) GetEmptyMessage() string {
|
|
|
+ return "no matching commands"
|
|
|
+}
|
|
|
+
|
|
|
+func getCommandCompletionItem(cmd commands.Command, space int) dialog.CompletionItemI {
|
|
|
+ t := theme.CurrentTheme()
|
|
|
+ spacer := strings.Repeat(" ", space)
|
|
|
+ title := " /" + cmd.Name + lipgloss.NewStyle().Foreground(t.TextMuted()).Render(spacer+cmd.Description)
|
|
|
+ value := "/" + cmd.Name
|
|
|
+ return dialog.NewCompletionItem(dialog.CompletionItem{
|
|
|
+ Title: title,
|
|
|
+ Value: value,
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
func (c *CommandCompletionProvider) GetChildEntries(query string) ([]dialog.CompletionItemI, error) {
|
|
|
+ space := 1
|
|
|
+ for _, cmd := range c.app.Commands {
|
|
|
+ if lipgloss.Width(cmd.Name) > space {
|
|
|
+ space = lipgloss.Width(cmd.Name)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ space += 2
|
|
|
+
|
|
|
if query == "" {
|
|
|
// If no query, return all commands
|
|
|
items := []dialog.CompletionItemI{}
|
|
|
for _, cmd := range c.app.Commands {
|
|
|
- items = append(items, dialog.NewCompletionItem(dialog.CompletionItem{
|
|
|
- Title: " /" + cmd.Name,
|
|
|
- Value: "/" + cmd.Name,
|
|
|
- }))
|
|
|
+ space := space - lipgloss.Width(cmd.Name)
|
|
|
+ items = append(items, getCommandCompletionItem(cmd, space))
|
|
|
}
|
|
|
return items, nil
|
|
|
}
|
|
|
@@ -45,11 +70,9 @@ func (c *CommandCompletionProvider) GetChildEntries(query string) ([]dialog.Comp
|
|
|
commandMap := make(map[string]dialog.CompletionItemI)
|
|
|
|
|
|
for _, cmd := range c.app.Commands {
|
|
|
+ space := space - lipgloss.Width(cmd.Name)
|
|
|
commandNames = append(commandNames, cmd.Name)
|
|
|
- commandMap[cmd.Name] = dialog.NewCompletionItem(dialog.CompletionItem{
|
|
|
- Title: " /" + cmd.Name,
|
|
|
- Value: "/" + cmd.Name,
|
|
|
- })
|
|
|
+ commandMap[cmd.Name] = getCommandCompletionItem(cmd, space)
|
|
|
}
|
|
|
|
|
|
// Find fuzzy matches
|
|
|
@@ -68,4 +91,3 @@ func (c *CommandCompletionProvider) GetChildEntries(query string) ([]dialog.Comp
|
|
|
|
|
|
return items, nil
|
|
|
}
|
|
|
-
|