- create centralized structured logger with rotation (https://github.com/versenilvis/IRIS/commit/96ad7c95ef0f7a9957b1bbf70bb6c8da3ce28b2e) - update core lookup and utils to use new logger (https://github.com/versenilvis/IRIS/commit/8b74b5a9cdc1828cff8c6d0bdaaccbe31da7a00a) - add log startup metadata and propagate reload flags (https://github.com/versenilvis/IRIS/commit/31208aa91a58e9e09abbf5497c314f5c961a58e2) - add log key interceptions and pty events (https://github.com/versenilvis/IRIS/commit/91ccb92835dd16ee70bf630fe0f267aad93c9db0) - add test for logger 221a40c893b75e6215787163cd751a4c1cb6c1d8 - secure reload arguments file path to prevent symlink attacks (https://github.com/versenilvis/IRIS/pull/13/commits/c3eabfda451230e52cb71f0c2a556687f839bd3f) - close log file on reinitialization to prevent descriptor leak (https://github.com/versenilvis/IRIS/pull/13/commits/a57d2c9be107a8c6200cf7046cc85b2f58667dce)
75 lines
1.7 KiB
Go
75 lines
1.7 KiB
Go
package root
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/versenilvis/iris/commands/core"
|
|
"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) []core.Suggestion {
|
|
maxSugg := config.Get().UI.MaxSuggestions
|
|
seen := make(map[string]bool)
|
|
deduped := []core.Suggestion{}
|
|
|
|
// always call lookup to scan aliases and get spec suggestions
|
|
var cmdResults []core.Suggestion
|
|
if query != "" {
|
|
logger.Debugf("Merge Calling Lookup for '%s'", query)
|
|
cmdResults = core.Lookup(query)
|
|
}
|
|
|
|
// search history if in history mode
|
|
var histResults []integration.HistResult
|
|
if mode == "history" {
|
|
aliases := core.GetAliasesCopy()
|
|
histResults, _ = integration.SearchHistory(query, aliases)
|
|
}
|
|
|
|
normalizedQuery := strings.TrimSpace(query)
|
|
|
|
// add suggestion helper to deduplicate
|
|
addSuggestion := func(s core.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(core.Suggestion{
|
|
Cmd: h.Cmd,
|
|
Desc: " history",
|
|
Icon: fmt.Sprintf("%d", h.ID),
|
|
})
|
|
}
|
|
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
|
|
}
|