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		switch op.(type) {
183
184		case operations.CreateOperation:
185			create := op.(operations.CreateOperation)
186			content, lines := util.TextWrap(create.Message, maxX)
187
188			v, err := sb.createOpView(g, viewName, x0, y0, maxX+1, lines, true)
189			if err != nil {
190				return err
191			}
192			fmt.Fprint(v, content)
193			y0 += lines + 2
194
195		case operations.AddCommentOperation:
196			comment := op.(operations.AddCommentOperation)
197
198			header := fmt.Sprintf("%s commented on %s",
199				comment.Author.Name, comment.Time().Format(timeLayout))
200			header = util.LeftPaddedString(header, maxX, 6)
201			message, lines := util.TextWrap(comment.Message, maxX)
202
203			v, err := sb.createOpView(g, viewName, x0, y0, maxX+1, lines+2, true)
204			if err != nil {
205				return err
206			}
207			fmt.Fprint(v, header, "\n\n", message)
208			y0 += lines + 3
209		}
210	}
211
212	return nil
213}
214
215func (sb *showBug) createOpView(g *gocui.Gui, name string, x0 int, y0 int, maxX int, height int, selectable bool) (*gocui.View, error) {
216	v, err := g.SetView(name, x0, y0, maxX, y0+height+1)
217
218	if err != nil && err != gocui.ErrUnknownView {
219		return nil, err
220	}
221
222	sb.childViews = append(sb.childViews, name)
223
224	if selectable {
225		sb.selectableView = append(sb.selectableView, name)
226	}
227
228	v.Frame = sb.selected == name
229
230	v.Clear()
231
232	return v, nil
233}
234
235func (sb *showBug) renderSidebar(v *gocui.View) {
236	maxX, _ := v.Size()
237	snap := sb.bug.Snapshot()
238
239	title := util.LeftPaddedString("LABEL", maxX, 2)
240	fmt.Fprintf(v, title+"\n\n")
241
242	for _, label := range snap.Labels {
243		fmt.Fprintf(v, util.LeftPaddedString(label.String(), maxX, 2))
244		fmt.Fprintln(v)
245	}
246}
247
248func (sb *showBug) back(g *gocui.Gui, v *gocui.View) error {
249	ui.activateWindow(ui.bugTable)
250	return nil
251}
252
253func (sb *showBug) scrollUp(g *gocui.Gui, v *gocui.View) error {
254	return nil
255}
256
257func (sb *showBug) scrollDown(g *gocui.Gui, v *gocui.View) error {
258	return nil
259}
260
261func (sb *showBug) selectPrevious(g *gocui.Gui, v *gocui.View) error {
262	if len(sb.selectableView) == 0 {
263		return nil
264	}
265
266	defer sb.focusView(g)
267
268	for i, name := range sb.selectableView {
269		if name == sb.selected {
270			// special case to scroll up to the top
271			if i == 0 {
272				sb.scroll = 0
273			}
274
275			sb.selected = sb.selectableView[maxInt(i-1, 0)]
276			return nil
277		}
278	}
279
280	if sb.selected == "" {
281		sb.selected = sb.selectableView[0]
282	}
283
284	return nil
285}
286
287func (sb *showBug) selectNext(g *gocui.Gui, v *gocui.View) error {
288	if len(sb.selectableView) == 0 {
289		return nil
290	}
291
292	defer sb.focusView(g)
293
294	for i, name := range sb.selectableView {
295		if name == sb.selected {
296			sb.selected = sb.selectableView[minInt(i+1, len(sb.selectableView)-1)]
297			return nil
298		}
299	}
300
301	if sb.selected == "" {
302		sb.selected = sb.selectableView[0]
303	}
304
305	return nil
306}
307
308func (sb *showBug) focusView(g *gocui.Gui) error {
309	mainView, err := g.View(showBugView)
310	if err != nil {
311		return err
312	}
313
314	_, maxY := mainView.Size()
315
316	_, vy0, _, _, err := g.ViewPosition(sb.selected)
317	if err != nil {
318		return err
319	}
320
321	v, err := g.View(sb.selected)
322	if err != nil {
323		return err
324	}
325
326	_, vMaxY := v.Size()
327
328	vy1 := vy0 + vMaxY
329
330	if vy0 < 0 {
331		sb.scroll += vy0
332		return nil
333	}
334
335	if vy1 > maxY {
336		sb.scroll -= maxY - vy1
337	}
338
339	return nil
340}
341
342func (sb *showBug) comment(g *gocui.Gui, v *gocui.View) error {
343	return addCommentWithEditor(sb.bug)
344}
345
346func (sb *showBug) setTitle(g *gocui.Gui, v *gocui.View) error {
347	return setTitleWithEditor(sb.bug)
348}