1package termui
2
3import (
4 "fmt"
5
6 "github.com/MichaelMure/git-bug/bug/operations"
7 "github.com/MichaelMure/git-bug/cache"
8 "github.com/MichaelMure/git-bug/util"
9 "github.com/jroimartin/gocui"
10)
11
12const showBugView = "showBugView"
13const showBugSidebarView = "showBugSidebarView"
14const showBugInstructionView = "showBugInstructionView"
15const showBugHeaderView = "showBugHeaderView"
16
17const timeLayout = "Jan 2 2006"
18
19type showBug struct {
20 cache cache.RepoCacher
21 bug cache.BugCacher
22 childViews []string
23 selectableView []string
24 selected string
25 scroll int
26}
27
28func newShowBug(cache cache.RepoCacher) *showBug {
29 return &showBug{
30 cache: cache,
31 }
32}
33
34func (sb *showBug) layout(g *gocui.Gui) error {
35 maxX, maxY := g.Size()
36
37 v, err := g.SetView(showBugView, 0, 0, maxX*2/3, maxY-2)
38
39 if err != nil {
40 if err != gocui.ErrUnknownView {
41 return err
42 }
43
44 sb.childViews = append(sb.childViews, showBugView)
45 v.Frame = false
46 }
47
48 v.Clear()
49 err = sb.renderMain(g, v)
50 if err != nil {
51 return err
52 }
53
54 v, err = g.SetView(showBugSidebarView, maxX*2/3+1, 0, maxX-1, maxY-2)
55
56 if err != nil {
57 if err != gocui.ErrUnknownView {
58 return err
59 }
60
61 sb.childViews = append(sb.childViews, showBugSidebarView)
62 v.Frame = true
63 }
64
65 v.Clear()
66 sb.renderSidebar(v)
67
68 v, err = g.SetView(showBugInstructionView, -1, maxY-2, maxX, maxY)
69
70 if err != nil {
71 if err != gocui.ErrUnknownView {
72 return err
73 }
74
75 sb.childViews = append(sb.childViews, showBugInstructionView)
76 v.Frame = false
77 v.BgColor = gocui.ColorBlue
78
79 fmt.Fprintf(v, "[q] Save and return [c] Comment [t] Change title [↓,j] Down [↑,k] Up")
80 }
81
82 _, err = g.SetCurrentView(showBugView)
83 return err
84}
85
86func (sb *showBug) keybindings(g *gocui.Gui) error {
87 // Return
88 if err := g.SetKeybinding(showBugView, 'q', gocui.ModNone, sb.saveAndBack); err != nil {
89 return err
90 }
91
92 // Scrolling
93 if err := g.SetKeybinding(showBugView, gocui.KeyPgup, gocui.ModNone,
94 sb.scrollUp); err != nil {
95 return err
96 }
97 if err := g.SetKeybinding(showBugView, gocui.KeyPgdn, gocui.ModNone,
98 sb.scrollDown); err != nil {
99 return err
100 }
101
102 // Down
103 if err := g.SetKeybinding(showBugView, 'j', gocui.ModNone,
104 sb.selectNext); err != nil {
105 return err
106 }
107 if err := g.SetKeybinding(showBugView, gocui.KeyArrowDown, gocui.ModNone,
108 sb.selectNext); err != nil {
109 return err
110 }
111 // Up
112 if err := g.SetKeybinding(showBugView, 'k', gocui.ModNone,
113 sb.selectPrevious); err != nil {
114 return err
115 }
116 if err := g.SetKeybinding(showBugView, gocui.KeyArrowUp, gocui.ModNone,
117 sb.selectPrevious); err != nil {
118 return err
119 }
120
121 // Comment
122 if err := g.SetKeybinding(showBugView, 'c', gocui.ModNone,
123 sb.comment); err != nil {
124 return err
125 }
126
127 // Title
128 if err := g.SetKeybinding(showBugView, 't', gocui.ModNone,
129 sb.setTitle); err != nil {
130 return err
131 }
132
133 // Labels
134
135 return nil
136}
137
138func (sb *showBug) disable(g *gocui.Gui) error {
139 for _, view := range sb.childViews {
140 if err := g.DeleteView(view); err != nil {
141 return err
142 }
143 }
144 return nil
145}
146
147func (sb *showBug) renderMain(g *gocui.Gui, mainView *gocui.View) error {
148 maxX, _ := mainView.Size()
149 x0, y0, _, _, _ := g.ViewPosition(mainView.Name())
150
151 y0 -= sb.scroll
152
153 snap := sb.bug.Snapshot()
154
155 sb.childViews = nil
156 sb.selectableView = nil
157
158 header := fmt.Sprintf("[ %s ] %s\n\n[ %s ] %s opened this bug on %s",
159 util.Cyan(snap.HumanId()),
160 util.Bold(snap.Title),
161 util.Yellow(snap.Status),
162 util.Magenta(snap.Author.Name),
163 snap.CreatedAt.Format(timeLayout),
164 )
165 content, lines := util.TextWrap(header, maxX)
166
167 v, err := sb.createOpView(g, showBugHeaderView, x0, y0, maxX+1, lines, false)
168 if err != nil {
169 return err
170 }
171
172 fmt.Fprint(v, content)
173 y0 += lines + 1
174
175 for i, op := range snap.Operations {
176 viewName := fmt.Sprintf("op%d", i)
177
178 // TODO: me might skip the rendering of blocks that are outside of the view
179 // but to do that we need to rework how sb.selectableView is maintained
180
181 switch op.(type) {
182
183 case operations.CreateOperation:
184 create := op.(operations.CreateOperation)
185 content, lines := util.TextWrap(create.Message, maxX)
186
187 v, err := sb.createOpView(g, viewName, x0, y0, maxX+1, lines, true)
188 if err != nil {
189 return err
190 }
191 fmt.Fprint(v, content)
192 y0 += lines + 2
193
194 case operations.AddCommentOperation:
195 comment := op.(operations.AddCommentOperation)
196
197 content := fmt.Sprintf("%s commented on %s\n\n%s",
198 util.Magenta(comment.Author.Name),
199 comment.Time().Format(timeLayout),
200 comment.Message,
201 )
202 content, lines := util.TextWrapPadded(content, maxX, 6)
203
204 v, err := sb.createOpView(g, viewName, x0, y0, maxX+1, lines, true)
205 if err != nil {
206 return err
207 }
208 fmt.Fprint(v, content)
209 y0 += lines + 2
210
211 case operations.SetTitleOperation:
212 setTitle := op.(operations.SetTitleOperation)
213
214 content := fmt.Sprintf("%s changed the title to %s on %s",
215 util.Magenta(setTitle.Author.Name),
216 util.Bold(setTitle.Title),
217 setTitle.Time().Format(timeLayout),
218 )
219 content, lines := util.TextWrap(content, maxX)
220
221 v, err := sb.createOpView(g, viewName, x0, y0, maxX+1, lines, true)
222 if err != nil {
223 return err
224 }
225 fmt.Fprint(v, content)
226 y0 += lines + 2
227 }
228 }
229
230 return nil
231}
232
233func (sb *showBug) createOpView(g *gocui.Gui, name string, x0 int, y0 int, maxX int, height int, selectable bool) (*gocui.View, error) {
234 v, err := g.SetView(name, x0, y0, maxX, y0+height+1)
235
236 if err != nil && err != gocui.ErrUnknownView {
237 return nil, err
238 }
239
240 sb.childViews = append(sb.childViews, name)
241
242 if selectable {
243 sb.selectableView = append(sb.selectableView, name)
244 }
245
246 v.Frame = sb.selected == name
247
248 v.Clear()
249
250 return v, nil
251}
252
253func (sb *showBug) renderSidebar(v *gocui.View) {
254 maxX, _ := v.Size()
255 snap := sb.bug.Snapshot()
256
257 title := util.LeftPaddedString("LABEL", maxX, 2)
258 fmt.Fprintf(v, title+"\n\n")
259
260 for _, label := range snap.Labels {
261 fmt.Fprintf(v, util.LeftPaddedString(label.String(), maxX, 2))
262 fmt.Fprintln(v)
263 }
264}
265
266func (sb *showBug) saveAndBack(g *gocui.Gui, v *gocui.View) error {
267 err := sb.bug.CommitAsNeeded()
268 if err != nil {
269 return err
270 }
271 ui.activateWindow(ui.bugTable)
272 return nil
273}
274
275func (sb *showBug) scrollUp(g *gocui.Gui, v *gocui.View) error {
276 mainView, err := g.View(showBugView)
277 if err != nil {
278 return err
279 }
280
281 _, maxY := mainView.Size()
282
283 sb.scroll -= maxY / 2
284
285 sb.scroll = maxInt(sb.scroll, 0)
286
287 return nil
288}
289
290func (sb *showBug) scrollDown(g *gocui.Gui, v *gocui.View) error {
291 _, maxY := v.Size()
292
293 lastViewName := sb.childViews[len(sb.childViews)-1]
294
295 lastView, err := g.View(lastViewName)
296 if err != nil {
297 return err
298 }
299
300 _, vMaxY := lastView.Size()
301
302 _, vy0, _, _, err := g.ViewPosition(lastViewName)
303 if err != nil {
304 return err
305 }
306
307 maxScroll := vy0 + sb.scroll + vMaxY - maxY
308
309 sb.scroll += maxY / 2
310
311 sb.scroll = minInt(sb.scroll, maxScroll)
312
313 return nil
314}
315
316func (sb *showBug) selectPrevious(g *gocui.Gui, v *gocui.View) error {
317 if len(sb.selectableView) == 0 {
318 return nil
319 }
320
321 defer sb.focusView(g)
322
323 for i, name := range sb.selectableView {
324 if name == sb.selected {
325 // special case to scroll up to the top
326 if i == 0 {
327 sb.scroll = 0
328 }
329
330 sb.selected = sb.selectableView[maxInt(i-1, 0)]
331 return nil
332 }
333 }
334
335 if sb.selected == "" {
336 sb.selected = sb.selectableView[0]
337 }
338
339 return nil
340}
341
342func (sb *showBug) selectNext(g *gocui.Gui, v *gocui.View) error {
343 if len(sb.selectableView) == 0 {
344 return nil
345 }
346
347 defer sb.focusView(g)
348
349 for i, name := range sb.selectableView {
350 if name == sb.selected {
351 sb.selected = sb.selectableView[minInt(i+1, len(sb.selectableView)-1)]
352 return nil
353 }
354 }
355
356 if sb.selected == "" {
357 sb.selected = sb.selectableView[0]
358 }
359
360 return nil
361}
362
363func (sb *showBug) focusView(g *gocui.Gui) error {
364 mainView, err := g.View(showBugView)
365 if err != nil {
366 return err
367 }
368
369 _, maxY := mainView.Size()
370
371 _, vy0, _, _, err := g.ViewPosition(sb.selected)
372 if err != nil {
373 return err
374 }
375
376 v, err := g.View(sb.selected)
377 if err != nil {
378 return err
379 }
380
381 _, vMaxY := v.Size()
382
383 vy1 := vy0 + vMaxY
384
385 if vy0 < 0 {
386 sb.scroll += vy0
387 return nil
388 }
389
390 if vy1 > maxY {
391 sb.scroll -= maxY - vy1
392 }
393
394 return nil
395}
396
397func (sb *showBug) comment(g *gocui.Gui, v *gocui.View) error {
398 return addCommentWithEditor(sb.bug)
399}
400
401func (sb *showBug) setTitle(g *gocui.Gui, v *gocui.View) error {
402 return setTitleWithEditor(sb.bug)
403}