From 5afd4c077dc64fec72e03288a188f0f904a8d248 Mon Sep 17 00:00:00 2001 From: David Young Date: Tue, 31 Mar 2026 16:14:43 -0600 Subject: [PATCH] further fixes to hampack-manager --- hampack-manager-src/main.go | 77 ++++++++++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 10 deletions(-) diff --git a/hampack-manager-src/main.go b/hampack-manager-src/main.go index 03f979e..ebddee4 100644 --- a/hampack-manager-src/main.go +++ b/hampack-manager-src/main.go @@ -119,15 +119,69 @@ func parseFlatpaks() []string { // ── State ────────────────────────────────────────────────────────────────────── -func loadState() map[string]bool { - data, err := os.ReadFile(stateFilePath()) +// installedPacmanPackages returns a set of all installed pacman package names. +func installedPacmanPackages() map[string]bool { + out, err := exec.Command("pacman", "-Q").Output() if err != nil { return map[string]bool{} } - var state map[string]bool - if err := json.Unmarshal(data, &state); err != nil { + installed := make(map[string]bool) + for _, line := range strings.Split(string(out), "\n") { + if fields := strings.Fields(line); len(fields) > 0 { + installed[fields[0]] = true + } + } + return installed +} + +// installedFlatpaks returns a set of all installed Flatpak application IDs. +func installedFlatpaks() map[string]bool { + out, err := exec.Command("flatpak", "list", "--app", "--columns=application").Output() + if err != nil { return map[string]bool{} } + installed := make(map[string]bool) + for _, line := range strings.Split(string(out), "\n") { + if s := strings.TrimSpace(line); s != "" { + installed[s] = true + } + } + return installed +} + +// detectInitialState checks actual installation status for every item. +func detectInitialState( + utils, apps, flatpaks []string, + compiled, binaries map[string]map[string]string, +) map[string]bool { + pacman := installedPacmanPackages() + flatpak := installedFlatpaks() + state := make(map[string]bool) + + for _, name := range utils { + state["utility:"+name] = pacman[name] + } + for _, name := range apps { + state["application:"+name] = pacman[name] + } + for _, id := range flatpaks { + state["flatpak:"+id] = flatpak[id] + } + for name, fields := range compiled { + path := expand(fields["install"]) + _, err := os.Stat(path) + state["compiled:"+name] = err == nil + } + for name, fields := range binaries { + path := expand(fields["install"]) + _, err := os.Stat(path) + state["binary:"+name] = err == nil + } + for _, item := range windowsItems { + _, err := os.Stat(item[1]) + state["windows:"+item[0]] = err == nil + } + return state } @@ -137,6 +191,7 @@ func saveState(state map[string]bool) { os.WriteFile(stateFilePath(), data, 0644) } + // ── Operations ──────────────────────────────────────────────────────────────── type Op struct { @@ -435,11 +490,16 @@ type mainWin struct { } func newMainWin(app *adw.Application) *mainWin { + compiled := parseINI(filepath.Join(hampackDir(), "compile.conf")) + binaries := parseINI(filepath.Join(hampackDir(), "binaries.conf")) + utils, apps := parsePackages() + flatpaks := parseFlatpaks() + w := &mainWin{ ApplicationWindow: adw.NewApplicationWindow(&app.Application), - compiled: parseINI(filepath.Join(hampackDir(), "compile.conf")), - binaries: parseINI(filepath.Join(hampackDir(), "binaries.conf")), - state: loadState(), + compiled: compiled, + binaries: binaries, + state: detectInitialState(utils, apps, flatpaks, compiled, binaries), rows: make(map[string]*gtk.Switch), } w.SetTitle("HamPack Manager") @@ -459,9 +519,6 @@ func newMainWin(app *adw.Application) *mainWin { page := adw.NewPreferencesPage() toolbar.SetContent(page) - utils, apps := parsePackages() - flatpaks := parseFlatpaks() - windows := make([]string, len(windowsItems)) for i, item := range windowsItems { windows[i] = item[0]