Files
iris-context/root/suggestions.go
T
VERSE ebde9ba68f refactor(commands): separate commands and its core into two folders (#32)
- I think it would be better if one for commands only, one for the spec
handling core logic so people won't be confused by the core logic inside
the commands folder (cleaner I guess)
- Regen docs
2026-07-05 11:27:30 +07:00

71 lines
1.6 KiB
Go

package root
import (
"strings"
"github.com/versenilvis/iris/spec"
"github.com/versenilvis/iris/config"
"github.com/versenilvis/iris/integration"
"github.com/versenilvis/iris/logger"
)
// MergeResults collects and dedupes suggestions for a query and mode
func MergeResults(query string, mode string) []spec.Suggestion {
maxSugg := config.Get().UI.MaxSuggestions
seen := make(map[string]bool)
deduped := []spec.Suggestion{}
// always call lookup to scan aliases and get spec suggestions
logger.Debugf("Merge Calling Lookup for '%s'", query)
cmdResults := spec.Lookup(query)
// search history if in history mode
var histResults []integration.HistResult
if mode == "history" {
aliases := spec.GetAliasesCopy()
histResults, _ = integration.SearchHistory(query, aliases)
}
normalizedQuery := strings.TrimSpace(query)
// add suggestion helper to deduplicate
addSuggestion := func(s spec.Suggestion) {
normalizedCmd := strings.TrimSpace(s.Cmd)
if normalizedCmd == "" {
return
}
// filter exact match to avoid loops and redundant suggestions
if normalizedCmd == normalizedQuery {
return
}
if !seen[s.Cmd] {
seen[s.Cmd] = true
deduped = append(deduped, s)
}
}
if mode == "history" {
// history mode: history first, then spec/alias
for _, h := range histResults {
addSuggestion(spec.Suggestion{
Cmd: h.Cmd,
Desc: "history",
Icon: "history",
})
}
for _, s := range cmdResults {
addSuggestion(s)
}
} else {
// spec mode: spec/alias only
for _, s := range cmdResults {
addSuggestion(s)
}
}
if len(deduped) > maxSugg {
deduped = deduped[:maxSugg]
}
return deduped
}