feat: pkgs =)

This commit is contained in:
Paulo 2023-07-20 11:13:28 -03:00
parent 2eb7b7d705
commit d29e4fc442
Signed by: pauloo27
GPG Key ID: 079BC1BF4562F663
4 changed files with 67 additions and 1 deletions

46
fetch/pkg.go Normal file
View File

@ -0,0 +1,46 @@
package fetch
import (
"fmt"
"os"
"strconv"
"strings"
)
const (
installedPkgsPath = "/var/lib/pacman/local/"
availableUpdatesPath = "/.cache/cupdates"
)
type PkgInfo struct {
Installed int
AvailableUpdates int
}
func Pkgs() (*PkgInfo, error) {
installed, err := listPkgsPacman()
if err != nil {
return nil, err
}
updates, err := listUpdatesPacman()
if err != nil {
return nil, err
}
return &PkgInfo{installed, updates}, nil
}
func listPkgsPacman() (int, error) {
files, err := os.ReadDir(installedPkgsPath)
return len(files), err
}
func listUpdatesPacman() (int, error) {
path := fmt.Sprintf("%s%s", os.Getenv("HOME"), availableUpdatesPath)
file, err := os.ReadFile(path)
if err != nil {
return 0, err
}
return strconv.Atoi(strings.TrimSuffix(string(file), "\n"))
}

View File

@ -7,4 +7,5 @@ const (
UptimeIcon = ""
MemoryIcon = "󰍛"
ColorsIcon = ""
PkgsIcon = ""
)

14
main.go
View File

@ -8,12 +8,24 @@ import (
func main() {
memory := must(fetch.Memory)
pkgs := must(fetch.Pkgs)
row(UserIcon, "user", must(fetch.User)+"@"+must(fetch.Hostname))
row(OsIcon, "os", must(fetch.Os))
row(KernelIcon, "kernel", must(fetch.Kernel))
row(UptimeIcon, "uptime", must(fetch.Uptime).String())
row(MemoryIcon, "memory", fmt.Sprintf("%d | %d MiB", memory.Used, memory.Total))
row(PkgsIcon, "pkgs",
conditional(pkgs.AvailableUpdates == 0,
fmt.Sprintf("%d", pkgs.Installed),
fmt.Sprintf("%d (%d)", pkgs.Installed, pkgs.AvailableUpdates),
),
)
row(MemoryIcon, "memory", fmt.Sprintf("%d / %d MiB", memory.Used, memory.Total))
show()
}

View File

@ -7,3 +7,10 @@ func must[T any](fn func() (T, error)) T {
}
return v
}
func conditional(cond bool, a, b string) string {
if cond {
return a
}
return b
}