Sort package statistics using CGo

This commit is contained in:
Jguer 2016-12-15 01:18:07 +00:00
parent d8e8a7e6e4
commit 9dcc9194ee
2 changed files with 30 additions and 34 deletions

View File

@ -185,7 +185,7 @@ func Search(pkg string) (err error) {
// LocalStatistics returns installed packages statistics. // LocalStatistics returns installed packages statistics.
func LocalStatistics(version string) error { func LocalStatistics(version string) error {
pkgmap, info, err := pac.Statistics() info, err := pac.Statistics()
if err != nil { if err != nil {
return err return err
} }
@ -200,10 +200,7 @@ func LocalStatistics(version string) error {
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", size(info.TotalSize))
fmt.Println("\x1B[1;34m===========================================\x1B[0m") fmt.Println("\x1B[1;34m===========================================\x1B[0m")
fmt.Println("\x1B[1;32mTen biggest packages\x1B[0m") fmt.Println("\x1B[1;32mTen biggest packages\x1B[0m")
pac.BiggestPackages()
for name, psize := range pkgmap {
fmt.Printf("%s: \x1B[0;33m%s\x1B[0m\n", name, size(psize))
}
fmt.Println("\x1B[1;34m===========================================\x1B[0m") fmt.Println("\x1B[1;34m===========================================\x1B[0m")
keys := make([]string, len(foreignS)) keys := make([]string, len(foreignS))

View File

@ -405,12 +405,11 @@ func ForeignPackages() (foreign map[string]*struct {
} }
// Statistics returns statistics about packages installed in system // Statistics returns statistics about packages installed in system
func Statistics() (packages map[string]int64, info struct { func Statistics() (info struct {
Totaln int Totaln int
Expln int Expln int
TotalSize int64 TotalSize int64
}, err error) { }, err error) {
var pkgs [10]alpm.Package
var tS int64 // TotalSize var tS int64 // TotalSize
var nPkg int var nPkg int
var ePkg int var ePkg int
@ -426,40 +425,14 @@ func Statistics() (packages map[string]int64, info struct {
return return
} }
var k int for _, pkg := range localDb.PkgCache().Slice() {
for e, pkg := range localDb.PkgCache().Slice() {
tS += pkg.ISize() tS += pkg.ISize()
k = -1
nPkg++ nPkg++
if pkg.Reason() == 0 { if pkg.Reason() == 0 {
ePkg++ ePkg++
} }
if e < 10 {
pkgs[e] = pkg
continue
} }
for i, pkw := range pkgs {
if k == -1 {
if pkw.ISize() < pkg.ISize() {
k = i
}
} else {
if pkw.ISize() < pkgs[k].ISize() && pkw.ISize() < pkg.ISize() {
k = i
}
}
}
if k != -1 {
pkgs[k] = pkg
}
}
packages = make(map[string]int64)
for _, pkg := range pkgs {
packages[pkg.Name()] = pkg.ISize()
}
info = struct { info = struct {
Totaln int Totaln int
Expln int Expln int
@ -470,3 +443,29 @@ func Statistics() (packages map[string]int64, info struct {
return return
} }
// BiggestPackages prints the name of the ten biggest packages in the system.
func BiggestPackages() {
h, err := conf.CreateHandle()
defer h.Release()
if err != nil {
return
}
localDb, err := h.LocalDb()
if err != nil {
return
}
pkgCache := localDb.PkgCache()
pkgS := pkgCache.SortBySize().Slice()
if len(pkgS) < 10 {
return
}
for i := 0; i < 10; i++ {
fmt.Printf("%s: \x1B[0;33m%dMB\x1B[0m\n", pkgS[i].Name(), pkgS[i].ISize()/(1024*1024))
}
// Could implement size here as well, but we just want the general idea
}