feat: pkgs =)
This commit is contained in:
parent
2eb7b7d705
commit
d29e4fc442
|
@ -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"))
|
||||
}
|
1
icons.go
1
icons.go
|
@ -7,4 +7,5 @@ const (
|
|||
UptimeIcon = ""
|
||||
MemoryIcon = ""
|
||||
ColorsIcon = ""
|
||||
PkgsIcon = ""
|
||||
)
|
||||
|
|
14
main.go
14
main.go
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue