forked from docker/docker-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
56 lines (45 loc) · 1.2 KB
/
command.go
File metadata and controls
56 lines (45 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package completions
import (
"context"
"slices"
"strings"
"github.com/docker/docker-agent/pkg/app"
"github.com/docker/docker-agent/pkg/tui/commands"
"github.com/docker/docker-agent/pkg/tui/components/completion"
)
type commandCompletion struct {
app *app.App
}
func NewCommandCompletion(a *app.App) Completion {
return &commandCompletion{
app: a,
}
}
func (c *commandCompletion) RequiresEmptyEditor() bool {
return true
}
func (c *commandCompletion) Trigger() string {
return "/"
}
func (c *commandCompletion) Items() []completion.Item {
var items []completion.Item
for _, cmd := range commands.BuildCommandCategories(context.Background(), c.app) {
for _, command := range cmd.Commands {
items = append(items, completion.Item{
Label: command.Label,
Description: command.Description,
Value: command.SlashCommand,
})
}
}
return sortItemsByLabel(items)
}
func sortItemsByLabel(items []completion.Item) []completion.Item {
slices.SortFunc(items, func(a, b completion.Item) int {
return strings.Compare(strings.ToLower(a.Label), strings.ToLower(b.Label))
})
return items
}
func (c *commandCompletion) MatchMode() completion.MatchMode {
return completion.MatchPrefix
}