From 467b7794c24e215ee35f3d8440b6ec9e20a60561 Mon Sep 17 00:00:00 2001 From: Jguer Date: Mon, 17 Jul 2017 23:44:46 +0100 Subject: [PATCH] References #39 and #40 --- actions.go | 78 ++++++++++++++++++++++++++++++++++++++++++++-------- upgrade/u.go | 58 ++++++++++++++++++++++++++++++-------- 2 files changed, 113 insertions(+), 23 deletions(-) diff --git a/actions.go b/actions.go index ae20512b..98f01f65 100644 --- a/actions.go +++ b/actions.go @@ -1,8 +1,12 @@ package main import ( + "bufio" "fmt" "os" + "sort" + "strconv" + "strings" aur "github.com/jguer/yay/aur" "github.com/jguer/yay/config" @@ -36,22 +40,72 @@ func upgradePkgs(flags []string) error { if err != nil { return err } - fmt.Printf("%+v\n", aurUp) - fmt.Printf("%+v\n", repoUp) - upgrade.Print(len(aurUp), repoUp) - upgrade.Print(0, aurUp) - // erra := aur.Upgrade(flags) + if len(aurUp)+len(repoUp) > 0 { + sort.Sort(repoUp) + fmt.Printf("\x1b[1;34;1m:: \x1b[0m\x1b[1m%d Packages to upgrade.\x1b[0m\n", len(aurUp)+len(repoUp)) + upgrade.Print(len(aurUp), repoUp) + upgrade.Print(0, aurUp) + fmt.Print("\x1b[32mEnter packages you don't want to upgrade.\x1b[0m\nNumbers: ") + } + reader := bufio.NewReader(os.Stdin) - // if errp != nil { - // return errp - // } + numberBuf, overflow, err := reader.ReadLine() + if err != nil || overflow { + fmt.Println(err) + return err + } - // var buffer bytes.Buffer - // buffer.WriteString("\n") + result := strings.Fields(string(numberBuf)) + var repoNums []int + var aurNums []int + for _, numS := range result { + num, err := strconv.Atoi(numS) + if err != nil { + continue + } + if num > len(aurUp)+len(repoUp)-1 || num < 0 { + continue + } else if num < len(aurUp) { + num = len(aurUp) - num - 1 + aurNums = append(aurNums, num) + } else { + num = len(aurUp) + len(repoUp) - num - 1 + repoNums = append(repoNums, num) + } + } - // fmt.Println("\x1b[1;36;1m::\x1b[0m\x1b[1m Starting AUR upgrade...\x1b[0m") - // return erra + if len(repoUp) != 0 { + var repoNames []string + repoloop: + for i, k := range repoUp { + for _, j := range repoNums { + if j == i { + continue repoloop + } + } + repoNames = append(repoNames, k.Name) + } + + err := config.PassToPacman("-S", repoNames, flags) + if err != nil { + fmt.Println("Error upgrading repo packages.") + } + } + + if len(aurUp) != 0 { + var aurNames []string + aurloop: + for i, k := range aurUp { + for _, j := range aurNums { + if j == i { + continue aurloop + } + } + aurNames = append(aurNames, k.Name) + } + aur.Install(aurNames, flags) + } return nil } diff --git a/upgrade/u.go b/upgrade/u.go index cfe5ebfe..41a03081 100644 --- a/upgrade/u.go +++ b/upgrade/u.go @@ -3,6 +3,7 @@ package upgrade import ( "fmt" + "unicode" alpm "github.com/jguer/go-alpm" "github.com/jguer/yay/config" @@ -18,6 +19,41 @@ type Upgrade struct { RemoteVersion string } +// Slice is a slice of Upgrades +type Slice []Upgrade + +func (s Slice) Len() int { return len(s) } +func (s Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] } + +func (s Slice) Less(i, j int) bool { + iRunes := []rune(s[i].Repository) + jRunes := []rune(s[j].Repository) + + max := len(iRunes) + if max > len(jRunes) { + max = len(jRunes) + } + + for idx := 0; idx < max; idx++ { + ir := iRunes[idx] + jr := jRunes[idx] + + lir := unicode.ToLower(ir) + ljr := unicode.ToLower(jr) + + if lir != ljr { + return lir > ljr + } + + // the lowercase runes are the same, so compare the original + if ir != jr { + return ir > jr + } + } + + return false +} + // FilterPackages filters packages based on source and type. func FilterPackages() (local []alpm.Package, remote []alpm.Package, localNames []string, remoteNames []string, err error) { @@ -57,8 +93,9 @@ func FilterPackages() (local []alpm.Package, remote []alpm.Package, return } -func Print(start int, u []Upgrade) { - for _, i := range u { +// Print prints the details of the packages to upgrade. +func Print(start int, u Slice) { + for k, i := range u { old, err := pkgb.NewCompleteVersion(i.LocalVersion) if err != nil { fmt.Println(i.Name, err) @@ -75,15 +112,15 @@ func Print(start int, u []Upgrade) { } return (hash)%6 + 31 } - // fmt.Printf("\x1b[33m%-2d\x1b[0m ", len(u)+start-k-1) - fmt.Printf("\x1b[1;%dm%s\x1b[0m/\x1b[1;39m%-20s\t\t\x1b[0m", f(i.Repository), i.Repository, i.Name) + fmt.Printf("\x1b[33m%-2d\x1b[0m ", len(u)+start-k-1) + fmt.Printf("\x1b[1;%dm%s\x1b[0m/\x1b[1;39m%-25s\t\t\x1b[0m", f(i.Repository), i.Repository, i.Name) if old.Version != new.Version { - fmt.Printf("\x1b[31m%10s\x1b[0m-%d -> \x1b[1;32m%s\x1b[0m-%d\x1b[0m", + fmt.Printf("\x1b[31m%18s\x1b[0m-%d -> \x1b[1;32m%s\x1b[0m-%d\x1b[0m", old.Version, old.Pkgrel, new.Version, new.Pkgrel) } else { - fmt.Printf("\x1b[0m%10s-\x1b[31m%d\x1b[0m -> %s-\x1b[32m%d\x1b[0m", + fmt.Printf("\x1b[0m%18s-\x1b[31m%d\x1b[0m -> %s-\x1b[32m%d\x1b[0m", old.Version, old.Pkgrel, new.Version, new.Pkgrel) } @@ -92,7 +129,7 @@ func Print(start int, u []Upgrade) { } // List returns lists of packages to upgrade from each source. -func List() (aurUp []Upgrade, repoUp []Upgrade, err error) { +func List() (aurUp Slice, repoUp Slice, err error) { err = config.PassToPacman("-Sy", nil, nil) if err != nil { return @@ -145,7 +182,7 @@ loop: // aur gathers foreign packages and checks if they have new versions. // Output: Upgrade type package list. -func aur(remote []alpm.Package, remoteNames []string) (toUpgrade []Upgrade, err error) { +func aur(remote []alpm.Package, remoteNames []string) (toUpgrade Slice, err error) { var j int var routines int var routineDone int @@ -195,7 +232,6 @@ func aur(remote []alpm.Package, remoteNames []string) (toUpgrade []Upgrade, err for { select { case pkg := <-packageC: - fmt.Println("Package Received") toUpgrade = append(toUpgrade, pkg) case <-done: routineDone++ @@ -209,13 +245,13 @@ func aur(remote []alpm.Package, remoteNames []string) (toUpgrade []Upgrade, err // repo gathers local packages and checks if they have new versions. // Output: Upgrade type package list. -func repo(local []alpm.Package) ([]Upgrade, error) { +func repo(local []alpm.Package) (Slice, error) { dbList, err := config.AlpmHandle.SyncDbs() if err != nil { return nil, err } - slice := []Upgrade{} + slice := Slice{} for _, pkg := range local { newPkg := pkg.NewVersion(dbList) if newPkg != nil {