Fixed project / alias autocomplete functions

This commit is contained in:
Ryan McGuire 2023-12-20 11:23:11 -05:00
parent 2082287bdd
commit f5201e8fea
4 changed files with 17 additions and 13 deletions

View File

@ -15,8 +15,7 @@ var projectOpenCmd = &cobra.Command{
Use: "open [fuzzy alias search]", Use: "open [fuzzy alias search]",
Short: "Open project in your IDE", Short: "Open project in your IDE",
Aliases: []string{"goto", "cd"}, Aliases: []string{"goto", "cd"},
Args: cobra.MaximumNArgs(1), Args: cobra.OnlyValidArgs,
ArgAliases: []string{"project"},
ValidArgsFunction: validAliasesFunc, ValidArgsFunction: validAliasesFunc,
Long: projOpenCmdLong, Long: projOpenCmdLong,
Run: projectOpenCmdRun, Run: projectOpenCmdRun,

View File

@ -4,12 +4,13 @@ import "github.com/spf13/cobra"
func validProjectsFunc(cmd *cobra.Command, args []string, toComplete string) ( func validProjectsFunc(cmd *cobra.Command, args []string, toComplete string) (
[]string, cobra.ShellCompDirective) { []string, cobra.ShellCompDirective) {
return cache.ProjectStrings(), cobra.ShellCompDirectiveDefault return cache.ProjectStrings(toComplete), cobra.ShellCompDirectiveNoFileComp
} }
func validAliasesFunc(cmd *cobra.Command, args []string, toComplete string) ( func validAliasesFunc(cmd *cobra.Command, args []string, toComplete string) (
[]string, cobra.ShellCompDirective) { []string, cobra.ShellCompDirective) {
return cache.AliasStrings(), cobra.ShellCompDirectiveDefault initProjectCache(cmd, args)
return cache.AliasStrings(toComplete), cobra.ShellCompDirectiveNoFileComp
} }
func validProjectsOrAliasesFunc(cmd *cobra.Command, args []string, toComplete string) ( func validProjectsOrAliasesFunc(cmd *cobra.Command, args []string, toComplete string) (

View File

@ -10,7 +10,7 @@ import (
// matched based on its Levenshtein distance, along with an integer // matched based on its Levenshtein distance, along with an integer
// indicating number of matches found // indicating number of matches found
func (c *Cache) FuzzyFindAlias(name string) []*ProjectAlias { func (c *Cache) FuzzyFindAlias(name string) []*ProjectAlias {
ranks := fuzzy.RankFindFold(name, c.AliasStrings()) ranks := fuzzy.RankFindFold(name, c.AliasStrings(""))
if ranks.Len() == 1 { if ranks.Len() == 1 {
c.log.Debug("Fuzzy found alias result", c.log.Debug("Fuzzy found alias result",
c.log.Args( c.log.Args(

View File

@ -65,18 +65,22 @@ func (c *Cache) ProjectString(p *gitlab.Project) string {
return info.String() return info.String()
} }
func (c *Cache) ProjectStrings() []string { func (c *Cache) ProjectStrings(prefix string) []string {
projects := make([]string, len(c.Projects)) projects := make([]string, 0, len(c.Projects))
for i, p := range c.Projects { for _, p := range c.Projects {
projects[i] = p.NameWithNamespace if strings.HasPrefix(p.NameWithNamespace, prefix) {
projects = append(projects, p.NameWithNamespace)
}
} }
return projects return projects
} }
func (c *Cache) AliasStrings() []string { func (c *Cache) AliasStrings(prefix string) []string {
aliases := make([]string, len(c.Aliases)) aliases := make([]string, 0, len(c.Aliases))
for i, a := range c.Aliases { for _, a := range c.Aliases {
aliases[i] = a.Alias if strings.HasPrefix(a.Alias, prefix) {
aliases = append(aliases, a.Alias)
}
} }
return aliases return aliases
} }