Skip to content

Commit

Permalink
feat: show packages not in repos / AUR
Browse files Browse the repository at this point in the history
Show packages that are installed locally but do not exist
in any repositories nor in AUR.
  • Loading branch information
moson-mo committed Apr 28, 2022
1 parent f891f35 commit 3cb0fbc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
8 changes: 4 additions & 4 deletions internal/pacseek/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,7 @@ func (ps *UI) drawPackageInfo(i InfoRecord, width int) {

fields, order := getDetailFields(i)
for _, k := range order {
//_, _, w, _ := ps.details.GetInnerRect()
if v, ok := fields[k]; ok {
if v, ok := fields[k]; ok && v != "" {
if ln == 1 || k == "Last modified" {
r++
}
Expand Down Expand Up @@ -244,7 +243,6 @@ func getDetailFields(i InfoRecord) (map[string]string, []string) {
fields[order[3]] = strings.Join(i.Conflicts, ", ")
fields[order[4]] = strings.Join(i.License, ", ")
fields[order[5]] = i.Maintainer

fields[order[6]] = getDependenciesJoined(i)
fields[order[7]] = i.URL
if i.Source == "AUR" {
Expand All @@ -254,7 +252,9 @@ func getDetailFields(i InfoRecord) (map[string]string, []string) {
} else if util.StringSliceContains(archRepos, i.Source) {
fields[order[11]] = packageUrl + i.Source + "/" + i.Architecture + "/" + i.Name
}
fields[order[10]] = time.Unix(int64(i.LastModified), 0).UTC().Format("2006-01-02 - 15:04:05 (UTC)")
if i.LastModified != 0 {
fields[order[10]] = time.Unix(int64(i.LastModified), 0).UTC().Format("2006-01-02 - 15:04:05 (UTC)")
}

return fields, order
}
Expand Down
24 changes: 19 additions & 5 deletions internal/pacseek/pacman.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func initPacmanDbs(dbPath, confPath string) (*alpm.Handle, error) {
}

// searches the pacman databases and returns packages that could be found (starting with "term")
func searchRepos(h *alpm.Handle, term string, mode string, by string, maxResults int) ([]Package, error) {
func searchRepos(h *alpm.Handle, term string, mode string, by string, maxResults int, localOnly bool) ([]Package, error) {
packages := []Package{}
if h == nil {
return packages, errors.New("alpm handle is nil")
Expand All @@ -44,13 +44,17 @@ func searchRepos(h *alpm.Handle, term string, mode string, by string, maxResults
return packages, err
}

searchDbs := dbs.Slice()
if localOnly {
searchDbs = []alpm.IDB{local}
}

counter := 0
for _, db := range dbs.Slice() {
for _, db := range searchDbs {
for _, pkg := range db.PkgCache().Slice() {
if counter >= maxResults {
break
}

compFunc := strings.HasPrefix
if mode == "Contains" {
compFunc = strings.Contains
Expand Down Expand Up @@ -101,8 +105,15 @@ func infoPacman(h *alpm.Handle, pkgs []string) RpcResult {
return r
}

for _, db := range dbs.Slice() {
for _, pkg := range pkgs {
local, err := h.LocalDB()
if err != nil {
r.Error = err.Error()
return r
}
dbslice := append(dbs.Slice(), local)

for _, pkg := range pkgs {
for _, db := range dbslice {
p := db.Pkg(pkg)
if p == nil {
continue
Expand Down Expand Up @@ -139,6 +150,9 @@ func infoPacman(h *alpm.Handle, pkgs []string) RpcResult {
Source: db.Name(),
Architecture: p.Architecture(),
}
if db.Name() == "local" {
i.Description = p.Description() + "\n[red]* Package not found in repositories/AUR *"
}

r.Results = append(r.Results, i)
}
Expand Down
10 changes: 5 additions & 5 deletions internal/pacseek/pacseek_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,19 @@ func (suite *pacseekTestSuite) TestSearchPacmanDbs() {
suite.Nil(err, err)

// ok
p, err := searchRepos(h, "glibc", "StartsWith", "Name", 1)
p, err := searchRepos(h, "glibc", "StartsWith", "Name", 1, false)
suite.Nil(err, err)
suite.Equal(suite.ExpectedPackages, p, "Package not found")
p, err = searchRepos(h, "glibc", "StartsWith", "Name & Description", 1)
p, err = searchRepos(h, "glibc", "StartsWith", "Name & Description", 1, false)
suite.Nil(err, err)
suite.Equal(suite.ExpectedPackages, p, "Package not found")

// nok
p, err = searchRepos(h, "nonsense_nonsense", "StartsWith", "Name", 1)
p, err = searchRepos(h, "nonsense_nonsense", "StartsWith", "Name", 1, false)
suite.Nil(err, err)
suite.Equal([]Package{}, p, "[]Packages not empty")

p, err = searchRepos(nil, "nonsense_nonsense", "StartsWith", "Name", 1)
p, err = searchRepos(nil, "nonsense_nonsense", "StartsWith", "Name", 1, false)
suite.NotNil(err, err)
suite.Equal([]Package{}, p, "[]Packages not empty")
}
Expand All @@ -73,7 +73,7 @@ func (suite *pacseekTestSuite) TestInfoPacmanDbs() {
// ok
p := infoPacman(h, []string{"glibc"})
suite.Equal("", p.Error, "error not empty")
suite.Equal(1, len(p.Results), "Results not 1")
suite.Equal(2, len(p.Results), "Results not 2")
suite.Equal("glibc", p.Results[0].Name, "Name not glibc")

// nok
Expand Down
22 changes: 20 additions & 2 deletions internal/pacseek/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ func (ps *UI) showPackages(text string) {
defer ps.stopSpin()

var err error
packages, err = searchRepos(ps.alpmHandle, text, ps.conf.SearchMode, ps.conf.SearchBy, ps.conf.MaxResults)
packages, err = searchRepos(ps.alpmHandle, text, ps.conf.SearchMode, ps.conf.SearchBy, ps.conf.MaxResults, false)
if err != nil {
ps.app.QueueUpdateDraw(func() {
ps.showMessage(err.Error(), true)
})
}
localPackages, err := searchRepos(ps.alpmHandle, text, ps.conf.SearchMode, ps.conf.SearchBy, ps.conf.MaxResults, true)
if err != nil {
ps.app.QueueUpdateDraw(func() {
ps.showMessage(err.Error(), true)
Expand All @@ -42,6 +48,18 @@ func (ps *UI) showPackages(text string) {

packages = append(packages, aurPackages...)
}
for _, lpkg := range localPackages {
found := false
for _, pkg := range packages {
if pkg.Name == lpkg.Name {
found = true
break
}
}
if !found {
packages = append(packages, lpkg)
}
}

sort.Slice(packages, func(i, j int) bool {
return packages[i].Name < packages[j].Name
Expand Down Expand Up @@ -158,7 +176,7 @@ func (ps *UI) showPackageInfo(row, column int) {
errorMsg = info.Error
}
ps.details.SetTitle(" [red]Error ")
ps.details.SetCellSimple(0, 0, "[red]s"+errorMsg)
ps.details.SetCellSimple(0, 0, "[red]"+errorMsg)
return
}
ps.selectedPackage = &info.Results[0]
Expand Down

0 comments on commit 3cb0fbc

Please sign in to comment.