Refactoring and Implemented Human sizes from alpm

This commit is contained in:
Jguer 2017-07-10 18:10:51 +01:00
parent 7608c6338d
commit 73f8c32176
6 changed files with 62 additions and 83 deletions

View file

@ -13,14 +13,20 @@ import (
func install(pkgs []string, flags []string) error {
aurs, repos, _ := pac.PackageSlices(pkgs)
err := config.PassToPacman("-S", repos, flags)
if err != nil {
fmt.Println("Error installing repo packages.")
if len(repos) != 0 {
err := config.PassToPacman("-S", repos, flags)
if err != nil {
fmt.Println("Error installing repo packages.")
}
}
err = aur.Install(aurs, flags)
return err
if len(aurs) != 0 {
err := aur.Install(aurs, flags)
if err != nil {
fmt.Println("Error installing aur packages.")
}
}
return nil
}
// Upgrade handles updating the cache and installing updates.

View file

@ -286,3 +286,16 @@ func PassToPacman(op string, pkgs []string, flags []string) error {
err := cmd.Run()
return err
}
// Human returns results in Human readable format.
func Human(size int64) string {
floatsize := float32(size)
units := [...]string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"}
for _, unit := range units {
if floatsize < 1024 {
return fmt.Sprintf("%.1f %sB", floatsize, unit)
}
floatsize /= 1024
}
return fmt.Sprintf("%d%s", size, "B")
}

View file

@ -306,7 +306,7 @@ func BiggestPackages() {
}
for i := 0; i < 10; i++ {
fmt.Printf("%s: \x1B[0;33m%.1fMiB\x1B[0m\n", pkgS[i].Name(), float32(pkgS[i].ISize())/(1024.0*1024.0))
fmt.Printf("%s: \x1B[0;33m%s\x1B[0m\n", pkgS[i].Name(), config.Human(pkgS[i].ISize()))
}
// Could implement size here as well, but we just want the general idea
}
@ -326,7 +326,7 @@ func HangingPackages() (hanging []string, err error) {
requiredby := pkg.ComputeRequiredBy()
if len(requiredby) == 0 {
hanging = append(hanging, pkg.Name())
fmt.Printf("%s: \x1B[0;33m%.2f KiB\x1B[0m\n", pkg.Name(), float32(pkg.ISize())/(1024.0))
fmt.Printf("%s: \x1B[0;33m%s\x1B[0m\n", pkg.Name(), config.Human(pkg.ISize()))
}
return nil
@ -361,7 +361,7 @@ big:
requiredby := pkg.ComputeRequiredBy()
if len(requiredby) == 0 {
hanging = append(hanging, pkgName)
fmt.Printf("%s: \x1B[0;33m%dMB\x1B[0m\n", pkg.Name(), pkg.ISize()/(1024*1024))
fmt.Printf("%s: \x1B[0;33m%s\x1B[0m\n", pkg.Name(), config.Human(pkg.ISize()))
}
}
}

View file

@ -107,7 +107,7 @@ func localStatistics(version string) error {
fmt.Printf("\x1B[1;32mTotal installed packages: \x1B[0;33m%d\x1B[0m\n", info.Totaln)
fmt.Printf("\x1B[1;32mTotal foreign installed packages: \x1B[0;33m%d\x1B[0m\n", len(foreignS))
fmt.Printf("\x1B[1;32mExplicitly installed packages: \x1B[0;33m%d\x1B[0m\n", info.Expln)
fmt.Printf("\x1B[1;32mTotal Size occupied by packages: \x1B[0;33m%s\x1B[0m\n", size(info.TotalSize))
fmt.Printf("\x1B[1;32mTotal Size occupied by packages: \x1B[0;33m%s\x1B[0m\n", config.Human(info.TotalSize))
fmt.Println("\x1B[1;34m===========================================\x1B[0m")
fmt.Println("\x1B[1;32mTen biggest packages\x1B[0m")
pac.BiggestPackages()

View file

@ -1,73 +0,0 @@
package main
import (
"fmt"
"io"
"math"
"os"
"time"
"github.com/jguer/yay/aur"
"github.com/jguer/yay/config"
pac "github.com/jguer/yay/pacman"
)
// Complete provides completion info for shells
func complete() (err error) {
path := os.Getenv("HOME") + "/.cache/yay/aur_" + config.YayConf.Shell + ".cache"
if info, err := os.Stat(path); os.IsNotExist(err) || time.Since(info.ModTime()).Hours() > 48 {
os.MkdirAll(os.Getenv("HOME")+"/.cache/yay/", 0755)
out, err := os.Create(path)
if err != nil {
return err
}
if aur.CreateAURList(out) != nil {
defer os.Remove(path)
}
err = pac.CreatePackageList(out)
out.Close()
return err
}
in, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return err
}
defer in.Close()
_, err = io.Copy(os.Stdout, in)
return err
}
// Function by pyk https://github.com/pyk/byten
func index(s int64) float64 {
x := math.Log(float64(s)) / math.Log(1024)
return math.Floor(x)
}
// Function by pyk https://github.com/pyk/byten
func countSize(s int64, i float64) float64 {
return float64(s) / math.Pow(1024, math.Floor(i))
}
// Size return a formated string from file size
// Function by pyk https://github.com/pyk/byten
func size(s int64) string {
symbols := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
i := index(s)
if s < 10 {
return fmt.Sprintf("%dB", s)
}
size := countSize(s, i)
format := "%.0f"
if size < 10 {
format = "%.1f"
}
return fmt.Sprintf(format+"%s", size, symbols[int(i)])
}

33
yay.go
View file

@ -3,9 +3,11 @@ package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
"github.com/jguer/yay/aur"
vcs "github.com/jguer/yay/aur/vcs"
@ -257,3 +259,34 @@ func numberMenu(pkgS []string, flags []string) (err error) {
return err
}
// Complete provides completion info for shells
func complete() (err error) {
path := os.Getenv("HOME") + "/.cache/yay/aur_" + config.YayConf.Shell + ".cache"
if info, err := os.Stat(path); os.IsNotExist(err) || time.Since(info.ModTime()).Hours() > 48 {
os.MkdirAll(os.Getenv("HOME")+"/.cache/yay/", 0755)
out, err := os.Create(path)
if err != nil {
return err
}
if aur.CreateAURList(out) != nil {
defer os.Remove(path)
}
err = pac.CreatePackageList(out)
out.Close()
return err
}
in, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return err
}
defer in.Close()
_, err = io.Copy(os.Stdout, in)
return err
}