From 9dcc9194ee2edf826b399f107217b0dfa953226c Mon Sep 17 00:00:00 2001 From: Jguer Date: Thu, 15 Dec 2016 01:18:07 +0000 Subject: [PATCH] Sort package statistics using CGo --- actions.go | 7 ++---- pacman/pacman.go | 57 ++++++++++++++++++++++++------------------------ 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/actions.go b/actions.go index d22727bf..94949c3c 100644 --- a/actions.go +++ b/actions.go @@ -185,7 +185,7 @@ func Search(pkg string) (err error) { // LocalStatistics returns installed packages statistics. func LocalStatistics(version string) error { - pkgmap, info, err := pac.Statistics() + info, err := pac.Statistics() if err != nil { 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.Println("\x1B[1;34m===========================================\x1B[0m") fmt.Println("\x1B[1;32mTen biggest packages\x1B[0m") - - for name, psize := range pkgmap { - fmt.Printf("%s: \x1B[0;33m%s\x1B[0m\n", name, size(psize)) - } + pac.BiggestPackages() fmt.Println("\x1B[1;34m===========================================\x1B[0m") keys := make([]string, len(foreignS)) diff --git a/pacman/pacman.go b/pacman/pacman.go index fca6eac3..38709f34 100644 --- a/pacman/pacman.go +++ b/pacman/pacman.go @@ -405,12 +405,11 @@ func ForeignPackages() (foreign map[string]*struct { } // Statistics returns statistics about packages installed in system -func Statistics() (packages map[string]int64, info struct { +func Statistics() (info struct { Totaln int Expln int TotalSize int64 }, err error) { - var pkgs [10]alpm.Package var tS int64 // TotalSize var nPkg int var ePkg int @@ -426,40 +425,14 @@ func Statistics() (packages map[string]int64, info struct { return } - var k int - for e, pkg := range localDb.PkgCache().Slice() { + for _, pkg := range localDb.PkgCache().Slice() { tS += pkg.ISize() - k = -1 nPkg++ if pkg.Reason() == 0 { 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 { Totaln int Expln int @@ -470,3 +443,29 @@ func Statistics() (packages map[string]int64, info struct { 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 +}