diff --git a/internal/view/app.go b/internal/view/app.go index b0bf03566c..f53e2256d3 100644 --- a/internal/view/app.go +++ b/internal/view/app.go @@ -158,13 +158,12 @@ func (a *App) suggestCommand() model.SuggestionFunc { s = strings.ToLower(s) for _, k := range a.command.alias.Aliases.Keys() { - if k == s { - continue - } - if strings.HasPrefix(k, s) { - entries = append(entries, strings.Replace(k, s, "", 1)) + if suggest, ok := shouldAddSuggest(s, k); ok { + entries = append(entries, suggest) } } + + entries = append(entries, a.suggestSubCommand(s)...) if len(entries) == 0 { return nil } @@ -173,6 +172,49 @@ func (a *App) suggestCommand() model.SuggestionFunc { } } +func (a *App) suggestSubCommand(command string) []string { + cmds := strings.Split(command, " ") + if len(cmds[0]) == 0 || len(cmds) != 2 { + return nil + } + + var suggests []string + switch strings.ToLower(cmds[0]) { + case "cow", "q", "q!", "qa", "Q", "quit", "?", "h", "help", "a", "alias", "x", "xray", "dir": + return nil // ignore special commands + case "ctx", "context", "contexts": + ctxs, err := a.factory.Client().Config().Contexts() + if err != nil { + log.Error().Err(err).Msg("failed to list contexts") + return nil + } + + for contextName := range ctxs { + if suggest, ok := shouldAddSuggest(cmds[1], contextName); ok { + suggests = append(suggests, suggest) + } + } + default: + nss, err := a.factory.Client().ValidNamespaces() + if err != nil { + log.Error().Err(err).Msg("failed to list nss") + return nil + } + + if suggest, ok := shouldAddSuggest(cmds[1], client.NamespaceAll); ok { + suggests = append(suggests, suggest) + } + + for _, ns := range nss { + if suggest, ok := shouldAddSuggest(cmds[1], ns.Name); ok { + suggests = append(suggests, suggest) + } + } + } + + return suggests +} + func (a *App) keyboard(evt *tcell.EventKey) *tcell.EventKey { if k, ok := a.HasAction(ui.AsKey(evt)); ok && !a.Content.IsTopDialog() { return k.Action(evt) @@ -667,3 +709,11 @@ func (a *App) clusterInfo() *ClusterInfo { func (a *App) statusIndicator() *ui.StatusIndicator { return a.Views()["statusIndicator"].(*ui.StatusIndicator) } + +func shouldAddSuggest(command, suggest string) (string, bool) { + if command != suggest && strings.HasPrefix(suggest, command) { + return strings.TrimPrefix(suggest, command), true + } + + return "", false +}