1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
package gui
import (
"fmt"
"sort"
"sync"
"github.com/isacikgoz/gitbatch/internal/git"
"github.com/isacikgoz/gitbatch/internal/job"
"github.com/isacikgoz/gitbatch/internal/load"
"github.com/jroimartin/gocui"
)
// Gui struct hold the gocui struct along with the gui's state, also keybindings
// are tied with this struct in order to render those in different occasions
type Gui struct {
g *gocui.Gui
KeyBindings []*KeyBinding
State guiState
mutex *sync.Mutex
order Layout
}
// guiState struct holds the repositories, directories, mode and queue of the
// gui object. These values are not static
type guiState struct {
Repositories []*git.Repository
Directories []string
Mode mode
Queue *job.Queue
FailoverQueue *job.Queue
targetBranch string
totalBranches []*branchCountMap
}
// this struct encapsulates the name and title of a view. the name of a view is
// passed around so much it is added so that I don't need to write names again
type viewFeature struct {
Name string
Title string
}
// mode of the gui
type mode struct {
ModeID ModeID
DisplayString string
CommandString string
}
type branchCountMap struct {
BranchName string
Count int
}
// ModeID is the mode indicator for the gui
type ModeID string
// Layout tells the gui how to order views
type Layout int
const (
// FetchMode puts the gui in fetch state
FetchMode ModeID = "fetch"
// PullMode puts the gui in pull state
PullMode = "pull"
// MergeMode puts the gui in merge state
MergeMode = "merge"
// CheckoutMode checkout selected repositories
CheckoutMode = "checkout"
overview Layout = 0
focus Layout = 1
)
var (
mainViewFeature = viewFeature{Name: "main", Title: " Matched Repositories "}
mainViewFrameFeature = viewFeature{Name: "mainframe", Title: " Matched Repositories "}
branchViewFeature = viewFeature{Name: "branch", Title: " Branches "}
batchBranchViewFeature = viewFeature{Name: "batch-branch", Title: " Select Branch "}
suggestBranchViewFeature = viewFeature{Name: "suggest-branch", Title: " Enter New Branch Name "}
remoteViewFeature = viewFeature{Name: "remotes", Title: " Remotes "}
remoteBranchViewFeature = viewFeature{Name: "remotebranches", Title: " Remote Branches "}
commitViewFeature = viewFeature{Name: "commits", Title: " Commits "}
keybindingsViewFeature = viewFeature{Name: "keybindings", Title: " Keybindings "}
cheatSheetViewFeature = viewFeature{Name: "cheatsheet", Title: " Application Controls "}
errorViewFeature = viewFeature{Name: "error", Title: " Error "}
dynamicViewFeature = viewFeature{Name: "dynamic", Title: " Dynamic "}
stashViewFeature = viewFeature{Name: "stash", Title: " Stash "}
fetchMode = mode{ModeID: FetchMode, DisplayString: "Fetch", CommandString: "fetch"}
pullMode = mode{ModeID: PullMode, DisplayString: "Pull", CommandString: "pull"}
mergeMode = mode{ModeID: MergeMode, DisplayString: "Merge", CommandString: "merge"}
checkoutMode = mode{ModeID: CheckoutMode, DisplayString: "Checkout", CommandString: "checkout"}
modes = []mode{fetchMode, pullMode, mergeMode}
// mainViews = []viewFeature{mainViewFeature, commitViewFeature, dynamicViewFeature, remoteViewFeature, remoteBranchViewFeature, branchViewFeature, stashViewFeature}
loaded = make(chan bool)
)
// New creates a Gui object and fill it's state related entities
func New(mode string, directories []string) (*Gui, error) {
initialState := guiState{
Directories: directories,
Mode: fetchMode,
Queue: job.CreateJobQueue(),
FailoverQueue: job.CreateJobQueue(),
}
gui := &Gui{
State: initialState,
mutex: &sync.Mutex{},
}
for _, m := range modes {
if string(m.ModeID) == mode {
gui.State.Mode = m
break
}
}
return gui, nil
}
// Run function runs the main loop with initial values
func (gui *Gui) Run() error {
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
return err
}
defer g.Close()
gui.g = g
g.Highlight = true
g.SelFgColor = gocui.ColorGreen
g.InputEsc = true
g.SetManagerFunc(gui.layout)
// load repositories in background asynchronously
go func() {
_ = load.AsyncLoad(gui.State.Directories, gui.loadRepository, loaded)
}()
if err := gui.generateKeybindings(); err != nil {
return err
}
if err := gui.keybindings(g); err != nil {
return err
}
// mainViews = overviewViews
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
return err
}
return nil
}
// add repository to gui's own slice and register listeners
func (gui *Gui) loadRepository(r *git.Repository) {
rs := gui.State.Repositories
// insertion sort implementation
index := sort.Search(len(rs), func(i int) bool { return git.Less(r, rs[i]) })
rs = append(rs, &git.Repository{})
copy(rs[index+1:], rs[index:])
rs[index] = r
// add listener
r.On(git.RepositoryUpdated, gui.repositoryUpdated)
r.On(git.BranchUpdated, gui.branchUpdated)
// update gui
_ = gui.repositoryUpdated(nil)
_ = gui.renderTitle()
// take pointer back
gui.State.Repositories = rs
go func() {
if <-loaded {
v, err := gui.g.View(mainViewFrameFeature.Name)
if err != nil {
return
}
v.Title = mainViewFrameFeature.Title + fmt.Sprintf("(%d) ", len(gui.State.Repositories))
}
}()
}
// render title with loaded repository count
func (gui *Gui) renderTitle() error {
v, err := gui.g.View(mainViewFrameFeature.Name)
if err != nil {
return err
}
v.Title = mainViewFrameFeature.Title + fmt.Sprintf("(%d/%d) ", len(gui.State.Repositories), len(gui.State.Directories))
return nil
}
// set the layout and create views with their default size, name etc. values
// TODO: window sizes can be handled better
func (gui *Gui) layout(g *gocui.Gui) error {
if gui.order == overview {
return gui.overviewLayout(g)
} else if gui.order == focus {
return gui.focusLayout(g)
}
return nil
}
// quit from the gui and end its loop
func (gui *Gui) quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
|