From 9c882614a38899568b38d9b9662bc83527652791 Mon Sep 17 00:00:00 2001 From: morganamilo Date: Tue, 24 Jul 2018 14:16:12 +0100 Subject: [PATCH 1/2] Rework completion Bash seperates on whitespace, so the fish completion file actually works for bash and zsh. So remove the concept of shells entirley and just use the singular aur_sh.cache file. If for some reason output without the repository data is needed, the user could always just pipe it into awk like so `yay -Pc | awk '{print $1}'`. Or perhaps a --quiet option could be added where yay will strip the output itself. The completion cache now updates when installing AUR packages. This is done as a goroutine with no wait groups. This ensures the program will never hang if there is a problem. The completion is stil updated during -Pc but as long as an AUR package has been installed recently it should not need to update. The cache will now also wait 7 days instead of 2 before refreshing. A refresh can be forced using -Pcc. --- cmd.go | 9 +++------ completions.go | 52 ++++++++++++++++++++------------------------------ install.go | 2 ++ 3 files changed, 26 insertions(+), 37 deletions(-) diff --git a/cmd.go b/cmd.go index a1204aa0..cfa67211 100644 --- a/cmd.go +++ b/cmd.go @@ -361,13 +361,10 @@ func handlePrint() (err error) { err = printUpdateList(cmdArgs) case cmdArgs.existsArg("w", "news"): err = printNewsFeed() + case cmdArgs.existsDouble("c", "complete"): + complete(true) case cmdArgs.existsArg("c", "complete"): - switch { - case cmdArgs.existsArg("f", "fish"): - complete("fish") - default: - complete("sh") - } + complete(false) case cmdArgs.existsArg("s", "stats"): err = localStatistics() default: diff --git a/completions.go b/completions.go index 68bad132..ba52d702 100644 --- a/completions.go +++ b/completions.go @@ -2,7 +2,6 @@ package main import ( "bufio" - "fmt" "io" "net/http" "os" @@ -13,7 +12,7 @@ import ( ) //CreateAURList creates a new completion file -func createAURList(out *os.File, shell string) (err error) { +func createAURList(out *os.File) (err error) { resp, err := http.Get("https://aur.archlinux.org/packages.gz") if err != nil { return err @@ -24,22 +23,15 @@ func createAURList(out *os.File, shell string) (err error) { scanner.Scan() for scanner.Scan() { - fmt.Print(scanner.Text()) out.WriteString(scanner.Text()) - if shell == "fish" { - fmt.Print("\tAUR\n") - out.WriteString("\tAUR\n") - } else { - fmt.Print("\n") - out.WriteString("\n") - } + out.WriteString("\tAUR\n") } return nil } //CreatePackageList appends Repo packages to completion cache -func createRepoList(out *os.File, shell string) (err error) { +func createRepoList(out *os.File) (err error) { dbList, err := alpmHandle.SyncDbs() if err != nil { return @@ -47,15 +39,8 @@ func createRepoList(out *os.File, shell string) (err error) { _ = dbList.ForEach(func(db alpm.Db) error { _ = db.PkgCache().ForEach(func(pkg alpm.Package) error { - fmt.Print(pkg.Name()) out.WriteString(pkg.Name()) - if shell == "fish" { - fmt.Print("\t" + pkg.DB().Name() + "\n") - out.WriteString("\t" + pkg.DB().Name() + "\n") - } else { - fmt.Print("\n") - out.WriteString("\n") - } + out.WriteString("\t" + pkg.DB().Name() + "\n") return nil }) return nil @@ -63,33 +48,38 @@ func createRepoList(out *os.File, shell string) (err error) { return nil } -// Complete provides completion info for shells -func complete(shell string) error { - var path string - - if shell == "fish" { - path = filepath.Join(cacheHome, "aur_fish"+".cache") - } else { - path = filepath.Join(cacheHome, "aur_sh"+".cache") - } +func updateCompletion(force bool) error { + path := filepath.Join(cacheHome, "completion.cache") info, err := os.Stat(path) - if os.IsNotExist(err) || time.Since(info.ModTime()).Hours() > 48 { + if os.IsNotExist(err) || time.Since(info.ModTime()).Hours() >= 7*24 || force { os.MkdirAll(filepath.Dir(path), 0755) out, errf := os.Create(path) if errf != nil { return errf } - if createAURList(out, shell) != nil { + if createAURList(out) != nil { defer os.Remove(path) } - erra := createRepoList(out, shell) + erra := createRepoList(out) out.Close() return erra } + return nil +} + +// Complete provides completion info for shells +func complete(force bool) error { + path := filepath.Join(cacheHome, "completion.cache") + + err := updateCompletion(force) + if err != nil { + return err + } + in, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644) if err != nil { return err diff --git a/install.go b/install.go index 6d11e80e..ed068ecd 100644 --- a/install.go +++ b/install.go @@ -312,6 +312,8 @@ func install(parser *arguments) error { } } + go updateCompletion(false) + err = downloadPkgBuildsSources(do.Aur, do.Bases, incompatible) if err != nil { return err From bc6028348b8ffdb6be78c79eee1dafafd3e9b4d2 Mon Sep 17 00:00:00 2001 From: morganamilo Date: Tue, 24 Jul 2018 14:42:43 +0100 Subject: [PATCH 2/2] Make the completion refresh time configurable The default setting is 7 days. The user can specify a different time in days. -1 can be set to never refresh while 0 can be used to always refresh. --- cmd.go | 5 ++++ completions.go | 2 +- config.go | 78 ++++++++++++++++++++++++++------------------------ parser.go | 2 ++ 4 files changed, 48 insertions(+), 39 deletions(-) diff --git a/cmd.go b/cmd.go index cfa67211..5dffe1aa 100644 --- a/cmd.go +++ b/cmd.go @@ -221,6 +221,11 @@ func handleConfig(option, value string) bool { config.SortMode = TopDown case "bottomup": config.SortMode = BottomUp + case "completioninterval": + n, err := strconv.Atoi(value) + if err == nil { + config.CompletionInterval = n + } case "sortby": config.SortBy = value case "noconfirm": diff --git a/completions.go b/completions.go index ba52d702..2a83f2ff 100644 --- a/completions.go +++ b/completions.go @@ -52,7 +52,7 @@ func updateCompletion(force bool) error { path := filepath.Join(cacheHome, "completion.cache") info, err := os.Stat(path) - if os.IsNotExist(err) || time.Since(info.ModTime()).Hours() >= 7*24 || force { + if os.IsNotExist(err) || (config.CompletionInterval != -1 && time.Since(info.ModTime()).Hours() >= float64(config.CompletionInterval*24)) || force { os.MkdirAll(filepath.Dir(path), 0755) out, errf := os.Create(path) if errf != nil { diff --git a/config.go b/config.go index 06be4bdb..cff3e77d 100644 --- a/config.go +++ b/config.go @@ -35,44 +35,45 @@ const ( // Configuration stores yay's config. type Configuration struct { - BuildDir string `json:"buildDir"` - Editor string `json:"editor"` - EditorFlags string `json:"editorflags"` - MakepkgBin string `json:"makepkgbin"` - MakepkgConf string `json:"makepkgconf"` - PacmanBin string `json:"pacmanbin"` - PacmanConf string `json:"pacmanconf"` - TarBin string `json:"tarbin"` - ReDownload string `json:"redownload"` - ReBuild string `json:"rebuild"` - AnswerClean string `json:"answerclean"` - AnswerDiff string `json:"answerdiff"` - AnswerEdit string `json:"answeredit"` - AnswerUpgrade string `json:"answerupgrade"` - GitBin string `json:"gitbin"` - GpgBin string `json:"gpgbin"` - GpgFlags string `json:"gpgflags"` - MFlags string `json:"mflags"` - SortBy string `json:"sortby"` - GitFlags string `json:"gitflags"` - RemoveMake string `json:"removemake"` - RequestSplitN int `json:"requestsplitn"` - SearchMode int `json:"-"` - SortMode int `json:"sortmode"` - SudoLoop bool `json:"sudoloop"` - TimeUpdate bool `json:"timeupdate"` - NoConfirm bool `json:"-"` - Devel bool `json:"devel"` - CleanAfter bool `json:"cleanAfter"` - GitClone bool `json:"gitclone"` - Provides bool `json:"provides"` - PGPFetch bool `json:"pgpfetch"` - UpgradeMenu bool `json:"upgrademenu"` - CleanMenu bool `json:"cleanmenu"` - DiffMenu bool `json:"diffmenu"` - EditMenu bool `json:"editmenu"` - CombinedUpgrade bool `json:"combinedupgrade"` - UseAsk bool `json:"useask"` + BuildDir string `json:"buildDir"` + Editor string `json:"editor"` + EditorFlags string `json:"editorflags"` + MakepkgBin string `json:"makepkgbin"` + MakepkgConf string `json:"makepkgconf"` + PacmanBin string `json:"pacmanbin"` + PacmanConf string `json:"pacmanconf"` + TarBin string `json:"tarbin"` + ReDownload string `json:"redownload"` + ReBuild string `json:"rebuild"` + AnswerClean string `json:"answerclean"` + AnswerDiff string `json:"answerdiff"` + AnswerEdit string `json:"answeredit"` + AnswerUpgrade string `json:"answerupgrade"` + GitBin string `json:"gitbin"` + GpgBin string `json:"gpgbin"` + GpgFlags string `json:"gpgflags"` + MFlags string `json:"mflags"` + SortBy string `json:"sortby"` + GitFlags string `json:"gitflags"` + RemoveMake string `json:"removemake"` + RequestSplitN int `json:"requestsplitn"` + SearchMode int `json:"-"` + SortMode int `json:"sortmode"` + CompletionInterval int `json:"completionrefreshtime"` + SudoLoop bool `json:"sudoloop"` + TimeUpdate bool `json:"timeupdate"` + NoConfirm bool `json:"-"` + Devel bool `json:"devel"` + CleanAfter bool `json:"cleanAfter"` + GitClone bool `json:"gitclone"` + Provides bool `json:"provides"` + PGPFetch bool `json:"pgpfetch"` + UpgradeMenu bool `json:"upgrademenu"` + CleanMenu bool `json:"cleanmenu"` + DiffMenu bool `json:"diffmenu"` + EditMenu bool `json:"editmenu"` + CombinedUpgrade bool `json:"combinedupgrade"` + UseAsk bool `json:"useask"` } var version = "7.885" @@ -163,6 +164,7 @@ func defaultSettings(config *Configuration) { config.MFlags = "" config.GitFlags = "" config.SortMode = BottomUp + config.CompletionInterval = 7 config.SortBy = "votes" config.SudoLoop = false config.TarBin = "bsdtar" diff --git a/parser.go b/parser.go index cd91c68f..9090b35a 100644 --- a/parser.go +++ b/parser.go @@ -472,6 +472,8 @@ func hasParam(arg string) bool { return true case "answerupgrade": return true + case "completioninterval": + return true case "sortby": return true default: