Skip to content

Commit

Permalink
add suggestions for context and resources on the command bar
Browse files Browse the repository at this point in the history
  • Loading branch information
wjiec committed Nov 13, 2023
1 parent 337ad9e commit 4b0ad25
Showing 1 changed file with 55 additions and 5 deletions.
60 changes: 55 additions & 5 deletions internal/view/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -668,3 +710,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
}

0 comments on commit 4b0ad25

Please sign in to comment.