Debian Codegraph @

internal/gui/gui.go

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