show_bug.go

  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] 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.back); 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	v, err := g.SetView(showBugHeaderView, x0, y0, maxX+1, y0+4)
159
160	if err != nil {
161		if err != gocui.ErrUnknownView {
162			return err
163		}
164
165		v.Frame = false
166	}
167	sb.childViews = append(sb.childViews, showBugHeaderView)
168
169	y0 += 4
170
171	v.Clear()
172	header1 := fmt.Sprintf("[%s] %s", snap.HumanId(), snap.Title)
173	fmt.Fprintf(v, util.LeftPaddedString(header1, maxX-1, 0)+"\n\n")
174
175	header2 := fmt.Sprintf("[%s] %s opened this bug on %s",
176		snap.Status, snap.Author.Name, snap.CreatedAt.Format(timeLayout))
177	fmt.Fprintf(v, util.LeftPaddedString(header2, maxX-1, 0))
178
179	for i, op := range snap.Operations {
180		viewName := fmt.Sprintf("op%d", i)
181
182		// TODO: me might skip the rendering of blocks that are outside of the view
183		// but to do that we need to rework how sb.selectableView is maintained
184
185		switch op.(type) {
186
187		case operations.CreateOperation:
188			create := op.(operations.CreateOperation)
189			content, lines := util.TextWrap(create.Message, maxX)
190
191			v, err := sb.createOpView(g, viewName, x0, y0, maxX+1, lines, true)
192			if err != nil {
193				return err
194			}
195			fmt.Fprint(v, content)
196			y0 += lines + 2
197
198		case operations.AddCommentOperation:
199			comment := op.(operations.AddCommentOperation)
200
201			header := fmt.Sprintf("%s commented on %s",
202				comment.Author.Name, comment.Time().Format(timeLayout))
203			header = util.LeftPaddedString(header, maxX, 6)
204			message, lines := util.TextWrap(comment.Message, maxX)
205
206			v, err := sb.createOpView(g, viewName, x0, y0, maxX+1, lines+2, true)
207			if err != nil {
208				return err
209			}
210			fmt.Fprint(v, header, "\n\n", message)
211			y0 += lines + 3
212		}
213	}
214
215	return nil
216}
217
218func (sb *showBug) createOpView(g *gocui.Gui, name string, x0 int, y0 int, maxX int, height int, selectable bool) (*gocui.View, error) {
219	v, err := g.SetView(name, x0, y0, maxX, y0+height+1)
220
221	if err != nil && err != gocui.ErrUnknownView {
222		return nil, err
223	}
224
225	sb.childViews = append(sb.childViews, name)
226
227	if selectable {
228		sb.selectableView = append(sb.selectableView, name)
229	}
230
231	v.Frame = sb.selected == name
232
233	v.Clear()
234
235	return v, nil
236}
237
238func (sb *showBug) renderSidebar(v *gocui.View) {
239	maxX, _ := v.Size()
240	snap := sb.bug.Snapshot()
241
242	title := util.LeftPaddedString("LABEL", maxX, 2)
243	fmt.Fprintf(v, title+"\n\n")
244
245	for _, label := range snap.Labels {
246		fmt.Fprintf(v, util.LeftPaddedString(label.String(), maxX, 2))
247		fmt.Fprintln(v)
248	}
249}
250
251func (sb *showBug) back(g *gocui.Gui, v *gocui.View) error {
252	ui.activateWindow(ui.bugTable)
253	return nil
254}
255
256func (sb *showBug) scrollUp(g *gocui.Gui, v *gocui.View) error {
257	mainView, err := g.View(showBugView)
258	if err != nil {
259		return err
260	}
261
262	_, maxY := mainView.Size()
263
264	sb.scroll -= maxY / 2
265
266	sb.scroll = maxInt(sb.scroll, 0)
267
268	return nil
269}
270
271func (sb *showBug) scrollDown(g *gocui.Gui, v *gocui.View) error {
272	_, maxY := v.Size()
273
274	lastViewName := sb.childViews[len(sb.childViews)-1]
275
276	lastView, err := g.View(lastViewName)
277	if err != nil {
278		return err
279	}
280
281	_, vMaxY := lastView.Size()
282
283	_, vy0, _, _, err := g.ViewPosition(lastViewName)
284	if err != nil {
285		return err
286	}
287
288	maxScroll := vy0 + sb.scroll + vMaxY - maxY
289
290	sb.scroll += maxY / 2
291
292	sb.scroll = minInt(sb.scroll, maxScroll)
293
294	return nil
295}
296
297func (sb *showBug) selectPrevious(g *gocui.Gui, v *gocui.View) error {
298	if len(sb.selectableView) == 0 {
299		return nil
300	}
301
302	defer sb.focusView(g)
303
304	for i, name := range sb.selectableView {
305		if name == sb.selected {
306			// special case to scroll up to the top
307			if i == 0 {
308				sb.scroll = 0
309			}
310
311			sb.selected = sb.selectableView[maxInt(i-1, 0)]
312			return nil
313		}
314	}
315
316	if sb.selected == "" {
317		sb.selected = sb.selectableView[0]
318	}
319
320	return nil
321}
322
323func (sb *showBug) selectNext(g *gocui.Gui, v *gocui.View) error {
324	if len(sb.selectableView) == 0 {
325		return nil
326	}
327
328	defer sb.focusView(g)
329
330	for i, name := range sb.selectableView {
331		if name == sb.selected {
332			sb.selected = sb.selectableView[minInt(i+1, len(sb.selectableView)-1)]
333			return nil
334		}
335	}
336
337	if sb.selected == "" {
338		sb.selected = sb.selectableView[0]
339	}
340
341	return nil
342}
343
344func (sb *showBug) focusView(g *gocui.Gui) error {
345	mainView, err := g.View(showBugView)
346	if err != nil {
347		return err
348	}
349
350	_, maxY := mainView.Size()
351
352	_, vy0, _, _, err := g.ViewPosition(sb.selected)
353	if err != nil {
354		return err
355	}
356
357	v, err := g.View(sb.selected)
358	if err != nil {
359		return err
360	}
361
362	_, vMaxY := v.Size()
363
364	vy1 := vy0 + vMaxY
365
366	if vy0 < 0 {
367		sb.scroll += vy0
368		return nil
369	}
370
371	if vy1 > maxY {
372		sb.scroll -= maxY - vy1
373	}
374
375	return nil
376}
377
378func (sb *showBug) comment(g *gocui.Gui, v *gocui.View) error {
379	return addCommentWithEditor(sb.bug)
380}
381
382func (sb *showBug) setTitle(g *gocui.Gui, v *gocui.View) error {
383	return setTitleWithEditor(sb.bug)
384}