forge/windowPatches.go

187 lines
4.1 KiB
Go
Raw Normal View History

2025-01-20 02:14:08 -06:00
package main
import (
"slices"
2025-01-28 13:20:10 -06:00
"strings"
2025-01-20 02:14:08 -06:00
"sync"
"go.wit.com/lib/gadgets"
2025-01-28 13:20:10 -06:00
"go.wit.com/lib/protobuf/forgepb"
2025-01-20 02:14:08 -06:00
"go.wit.com/log"
"go.wit.com/gui"
)
type patchesWindow struct {
2025-01-29 09:06:19 -06:00
once sync.Once // only init() the window once
win *gadgets.BasicWindow // the patches window
stack *gui.Node // the top box set as vertical
shelf *gui.Node // the first box in the stack, set as horizontal
2025-01-28 13:20:10 -06:00
grid *gui.Node // the list of available patches
summary *patchSummary // summary of current patches
setgrid *gui.Node // the list of each patchset
setlist map[string]*forgepb.Patchset // a map of the patch names to the protobuf
2025-01-29 09:06:19 -06:00
setwin map[string]*patchWindow // a map of the patch names to the protobuf
2025-01-20 02:14:08 -06:00
}
func (r *patchesWindow) Hidden() bool {
return r.win.Hidden()
}
func (r *patchesWindow) Toggle() {
if r.Hidden() {
r.Show()
} else {
2025-01-20 02:30:31 -06:00
r.Hide()
2025-01-20 02:14:08 -06:00
}
}
func (r *patchesWindow) Show() {
r.win.Show()
}
func (r *patchesWindow) Hide() {
r.win.Hide()
}
func (r *patchesWindow) Disable() {
2025-01-20 02:50:07 -06:00
r.stack.Disable()
2025-01-20 02:14:08 -06:00
}
func (r *patchesWindow) Enable() {
2025-01-20 02:50:07 -06:00
r.stack.Enable()
2025-01-20 02:14:08 -06:00
}
// you can only have one of these
func (r *patchesWindow) initWindow() {
// sync.Once()
2025-01-20 02:50:07 -06:00
r.win = gadgets.RawBasicWindow("Forge Patchesets")
2025-01-20 02:14:08 -06:00
r.win.Make()
2025-01-20 02:50:07 -06:00
r.stack = r.win.Box().NewBox("bw vbox", false)
2025-01-20 02:14:08 -06:00
// me.reposwin.Draw()
r.win.Custom = func() {
2025-01-20 02:50:07 -06:00
log.Warn("Patchset Window close. setting hidden=true")
2025-01-20 02:30:31 -06:00
// sets the hidden flag to false so Toggle() works
r.win.Hide()
2025-01-20 02:14:08 -06:00
}
2025-01-20 03:18:44 -06:00
r.grid = r.stack.NewGrid("", 0, 0)
2025-01-28 18:10:32 -06:00
/*
r.shelf = r.initGroup()
group1 := r.stack.NewGroup("stuff")
vbox := group1.Box()
vbox.Vertical()
*/
2025-01-28 13:20:10 -06:00
r.summary = r.submitPatchesBox(r.stack)
2025-01-20 02:14:08 -06:00
2025-01-28 18:10:32 -06:00
// update the stats about the repos and patches
r.summary.Update()
2025-01-20 02:14:08 -06:00
2025-01-28 18:10:32 -06:00
g := r.stack.NewGroup("PatchSet List")
2025-01-20 02:14:08 -06:00
2025-01-28 18:10:32 -06:00
// add the grid
r.setgrid = g.NewGrid("", 0, 0)
r.setlist = make(map[string]*forgepb.Patchset)
2025-01-29 09:06:19 -06:00
r.setwin = make(map[string]*patchWindow)
2025-01-20 02:14:08 -06:00
2025-01-28 18:10:32 -06:00
// query for current patchsets
lines, err := listPatches()
if err != nil {
log.Info(err)
return
}
slices.Reverse(lines)
2025-01-29 09:43:42 -06:00
count := 0
2025-01-28 18:10:32 -06:00
for i, line := range lines {
log.Info(i, line)
2025-01-29 09:43:42 -06:00
count += 1
if count < 10 {
r.addPatchset(line)
}
2025-01-20 02:14:08 -06:00
}
2025-01-29 09:43:42 -06:00
log.Info("Total patchsets:", count)
2025-01-28 13:20:10 -06:00
}
func (r *patchesWindow) addPatchset(line string) {
parts := strings.Split(line, "Author:")
author := parts[1]
parts = strings.Fields(parts[0])
name := parts[0]
subject := strings.Join(parts[1:], " ")
r.setgrid.NewLabel(name)
r.setgrid.NewLabel(subject)
r.setgrid.NewLabel(author)
2025-01-29 09:43:42 -06:00
/*
r.setgrid.NewButton("Download", func() {
pset, err := savePatch(name)
if err != nil {
log.Info(name, "failed to download", err)
return
}
r.setlist[name] = pset
})
*/
2025-01-29 09:06:19 -06:00
r.setgrid.NewButton("View", func() {
// has the window already been created?
win := r.setwin[name]
if win != nil {
win.Toggle()
log.Info("TRYING TO TOGGLE WINDOW")
return
}
// get the patch and make the window
2025-01-28 13:20:10 -06:00
pset, err := getPatch(name)
if err != nil {
log.Info(name, "failed to download", err)
return
}
r.setlist[name] = pset
2025-01-29 09:06:19 -06:00
r.setwin[name] = makePatchWindow(pset)
2025-01-29 09:43:42 -06:00
r.setwin[name].Show()
2025-01-28 13:20:10 -06:00
})
r.setgrid.NewButton("Dump", func() {
pset := r.setlist[name]
if pset == nil {
log.Info(name, "was nil")
return
}
if !dumpPatchset(pset) {
log.Info("Dump: some patches are bad", name)
return
}
})
2025-01-29 09:43:42 -06:00
r.setgrid.NewButton("Extract", func() {
2025-01-28 13:20:10 -06:00
pset := r.setlist[name]
if pset == nil {
log.Info(name, "was nil")
return
}
2025-01-28 21:50:07 -06:00
if err := savePatchset(pset); err != nil {
log.Info("Save: some patches are bad", name, err)
2025-01-28 13:20:10 -06:00
return
}
})
2025-01-28 18:10:32 -06:00
r.setgrid.NewButton("Apply", func() {
pset := r.setlist[name]
if pset == nil {
log.Info(name, "was nil")
return
}
if _, _, _, err := IsEverythingOnDevel(); err != nil {
log.Info("You can only apply patches to the devel branch")
return
}
if IsAnythingDirty() {
log.Info("You can't apply patches when repos are dirty")
me.forge.PrintHumanTable(me.found)
return
}
applyPatchset(pset)
})
2025-01-28 13:20:10 -06:00
r.setgrid.NextRow()
}