Improved AUR completions. ZSH support added

This commit is contained in:
Jguer 2017-04-09 22:17:10 +01:00
parent 40f1743f42
commit e73d75fc37
7 changed files with 91 additions and 66 deletions

View file

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"io" "io"
"math" "math"
"net/http"
"os" "os"
"os/exec" "os/exec"
"strconv" "strconv"
@ -329,68 +328,30 @@ func GetPkgbuild(pkg string) (err error) {
return return
} }
func createAURList(path string) (err error) {
os.MkdirAll(os.Getenv("HOME")+"/.cache/yay", 0755)
out, err := os.Create(path)
if err != nil {
return err
}
defer out.Close()
resp, err := http.Get("https://aur.archlinux.org/packages.gz")
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func updateAURList(out io.Writer) (err error) {
resp, err := http.Get("https://aur.archlinux.org/packages.gz")
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
// Complete provides completion info for shells // Complete provides completion info for shells
func Complete() (err error) { func Complete() (err error) {
path := os.Getenv("HOME") + "/.cache/yay/aur.cache" path := os.Getenv("HOME") + "/.cache/yay/aur.cache"
if _, err := os.Stat(path); os.IsNotExist(err) { if info, err := os.Stat(path); os.IsNotExist(err) || time.Since(info.ModTime()).Hours() > 48 {
err = createAURList(path) os.MkdirAll(os.Getenv("HOME")+"/.cache/yay", 0755)
out, err := os.Create(path)
if err != nil {
return err
}
defer out.Close()
aur.CreateAURList(out)
err = pac.CreatePackageList(out)
return err
} }
in, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0755) in, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return err
}
defer in.Close() defer in.Close()
if err != nil {
return err
}
info, err := in.Stat()
if time.Since(info.ModTime()).Hours() > 48 {
err = updateAURList(in)
}
// Get the data
_, err = io.Copy(os.Stdout, in) _, err = io.Copy(os.Stdout, in)
if err != nil { return err
return err
}
return nil
} }

View file

@ -1,7 +1,10 @@
package aur package aur
import ( import (
"bufio"
"fmt" "fmt"
"net/http"
"os"
"github.com/jguer/yay/pacman" "github.com/jguer/yay/pacman"
"github.com/jguer/yay/util" "github.com/jguer/yay/util"
@ -88,3 +91,29 @@ func GetPkgbuild(pkgN string, dir string) (err error) {
util.DownloadAndUnpack(BaseURL+aq[0].URLPath, dir, false) util.DownloadAndUnpack(BaseURL+aq[0].URLPath, dir, false)
return return
} }
//CreateAURList creates a new completion file
func CreateAURList(out *os.File) (err error) {
resp, err := http.Get("https://aur.archlinux.org/packages.gz")
if err != nil {
return err
}
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
scanner.Scan()
for scanner.Scan() {
fmt.Print(scanner.Text())
out.WriteString(scanner.Text())
if util.Shell == "fish" {
fmt.Print("\tAUR\n")
out.WriteString("\tAUR\n")
} else {
fmt.Print("\n")
out.WriteString("\n")
}
}
return nil
}

View file

@ -42,7 +42,7 @@ func parser() (op string, options []string, packages []string, err error) {
} }
op = "yogurt" op = "yogurt"
for _, arg := range os.Args[1:] { for i, arg := range os.Args[1:] {
if arg[0] == '-' && arg[1] != '-' { if arg[0] == '-' && arg[1] != '-' {
switch arg { switch arg {
case "-b": case "-b":
@ -62,6 +62,7 @@ func parser() (op string, options []string, packages []string, err error) {
case "--topdown": case "--topdown":
util.SortMode = util.TopDown util.SortMode = util.TopDown
case "--complete": case "--complete":
util.Shell = os.Args[i+1]
yay.Complete() yay.Complete()
os.Exit(0) os.Exit(0)
case "--help": case "--help":

View file

@ -546,3 +546,39 @@ func GetPkgbuild(pkgN string, path string) (err error) {
} }
return fmt.Errorf("package not found") return fmt.Errorf("package not found")
} }
//CreatePackageList appends Repo packages to completion cache
func CreatePackageList(out *os.File) (err error) {
h, err := conf.CreateHandle()
defer h.Release()
if err != nil {
return
}
dbList, err := h.SyncDbs()
if err != nil {
return
}
p := func(pkg alpm.Package) error {
fmt.Print(pkg.Name())
out.WriteString(pkg.Name())
if util.Shell == "fish" {
fmt.Print("\t" + pkg.DB().Name() + "\n")
out.WriteString("\t" + pkg.DB().Name() + "\n")
} else {
fmt.Print("\n")
out.WriteString("\n")
}
return nil
}
f := func(db alpm.Db) error {
db.PkgCache().ForEach(p)
return nil
}
dbList.ForEach(f)
return nil
}

View file

@ -25,6 +25,8 @@ const (
Minimal Minimal
) )
var Shell = "fish"
// Build controls if packages will be built from ABS. // Build controls if packages will be built from ABS.
var Build = false var Build = false

View file

@ -1,17 +1,14 @@
# Completions for apacman
# Original Author for pacman: Giorgio Lando <patroclo7@gmail.com> # Original Author for pacman: Giorgio Lando <patroclo7@gmail.com>
# Updated for pacman by maxfl, SanskritFritz, faho, f1u77y
# Updated for yay by jguer # Updated for yay by jguer
set -l progname yay set -l progname yay
set -l listinstalled "(pacman -Q | string replace ' ' \t)" set -l listinstalled "(pacman -Q | string replace ' ' \t)"
# This might be an issue if another package manager is also installed (e.g. for containers) # This might be an issue if another package manager is also installed (e.g. for containers)
set -l listall "(__fish_print_packages)" set -l listall "(yay --complete fish)"
set -l listaur "(yay --complete)"
set -l listrepos "(__fish_print_pacman_repos)" set -l listrepos "(__fish_print_pacman_repos)"
set -l listgroups "(pacman -Sg)\t'Package Group'" set -l listgroups "(pacman -Sg)\t'Package Group'"
set -l listpacman "(__fish_print_packages)"
set -l noopt 'not __fish_contains_opt -s S -s D -s Q -s R -s U -s T -s F database query sync remove upgrade deptest files' set -l noopt 'not __fish_contains_opt -s S -s D -s Q -s R -s U -s T -s F database query sync remove upgrade deptest files'
set -l database '__fish_contains_opt -s D database' set -l database '__fish_contains_opt -s D database'
set -l getpkgbuild '__fish_contains_opt -s G getpkgbuild' set -l getpkgbuild '__fish_contains_opt -s G getpkgbuild'
@ -118,7 +115,7 @@ complete -c $progname -n "$sync; and not __fish_contains_opt -s u sysupgrade" -s
complete -c $progname -n "$sync; and __fish_contains_opt -s u sysupgrade" -s u -l sysupgrade -d 'Also downgrade packages' complete -c $progname -n "$sync; and __fish_contains_opt -s u sysupgrade" -s u -l sysupgrade -d 'Also downgrade packages'
complete -c $progname -n $sync -s w -l downloadonly -d 'Only download the target packages' complete -c $progname -n $sync -s w -l downloadonly -d 'Only download the target packages'
complete -c $progname -n $sync -s y -l refresh -d 'Download fresh copy of the package list' complete -c $progname -n $sync -s y -l refresh -d 'Download fresh copy of the package list'
complete -c $progname -n "$sync" -xa "$listall $listgroups $listaur" complete -c $progname -n "$sync" -xa "$listall $listgroups"
# Database options # Database options
set -l has_db_opt '__fish_contains_opt asdeps asexplicit' set -l has_db_opt '__fish_contains_opt asdeps asexplicit'
@ -134,7 +131,7 @@ complete -c $progname -n "$files; and not $has_file_opt" -xa -l -d 'List files o
complete -c $progname -n "$files; and not $has_file_opt" -xa --search -d 'Search packages for matching files' complete -c $progname -n "$files; and not $has_file_opt" -xa --search -d 'Search packages for matching files'
complete -c $progname -n "$files; and not $has_file_opt" -xa -s -d 'Search packages for matching files' complete -c $progname -n "$files; and not $has_file_opt" -xa -s -d 'Search packages for matching files'
complete -c $progname -n "$files" -s y -l refresh -d 'Refresh the files database' -f complete -c $progname -n "$files" -s y -l refresh -d 'Refresh the files database' -f
complete -c $progname -n "$files" -s l -l list -d 'List files owned by given packages' -xa $listall complete -c $progname -n "$files" -s l -l list -d 'List files owned by given packages' -xa $listpacman
complete -c $progname -n "$files" -s s -l search -d 'Search packages for matching files' complete -c $progname -n "$files" -s s -l search -d 'Search packages for matching files'
complete -c $progname -n "$files" -s o -l owns -d 'Search for packages that include the given files' complete -c $progname -n "$files" -s o -l owns -d 'Search for packages that include the given files'
complete -c $progname -n "$files" -s q -l quiet -d 'Show less information' -f complete -c $progname -n "$files" -s q -l quiet -d 'Show less information' -f

View file

@ -298,7 +298,6 @@ _pacman_completions_all_groups() {
# these can be specified as either 'package' or 'repository/package' # these can be specified as either 'package' or 'repository/package'
_pacman_completions_all_packages() { _pacman_completions_all_packages() {
local -a seq sep cmd packages repositories packages_long local -a seq sep cmd packages repositories packages_long
_pacman_get_command
if [[ ${words[CURRENT-1]} == '--ignore' ]]; then if [[ ${words[CURRENT-1]} == '--ignore' ]]; then
seq='_sequence' seq='_sequence'
@ -309,11 +308,11 @@ _pacman_completions_all_packages() {
fi fi
if compset -P1 '*/*'; then if compset -P1 '*/*'; then
packages=( $(_call_program packages $cmd[@] -Sql ${words[CURRENT]%/*}) ) packages=( $(_call_program packages yay --complete zsh ${words[CURRENT]%/*}) )
typeset -U packages typeset -U packages
${seq} _wanted repo_packages expl "repository/package" compadd ${sep[@]} ${(@)packages} ${seq} _wanted repo_packages expl "repository/package" compadd ${sep[@]} ${(@)packages}
else else
packages=( $(_call_program packages $cmd[@] -Sql) ) packages=( $(_call_program packages yay --complete zsh) )
typeset -U packages typeset -U packages
${seq} _wanted packages expl "packages" compadd ${sep[@]} - "${(@)packages}" ${seq} _wanted packages expl "packages" compadd ${sep[@]} - "${(@)packages}"