aboutsummaryrefslogtreecommitdiffstats
path: root/termui/show_bug.go
blob: a8be26dc818864477cde5233108ed29ec93073ef (plain) (blame)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package termui

import (
	"fmt"
	"github.com/MichaelMure/git-bug/bug/operations"
	"github.com/MichaelMure/git-bug/cache"
	"github.com/MichaelMure/git-bug/util"
	"github.com/jroimartin/gocui"
)

const showBugView = "showBugView"
const showBugSidebarView = "showBugSidebarView"
const showBugInstructionView = "showBugInstructionView"

const timeLayout = "Jan _2 2006"

type showBug struct {
	cache      cache.RepoCacher
	bug        cache.BugCacher
	childViews []string
}

func newShowBug(cache cache.RepoCacher) *showBug {
	return &showBug{
		cache: cache,
	}
}

func (sb *showBug) layout(g *gocui.Gui) error {
	maxX, maxY := g.Size()

	v, err := g.SetView(showBugView, 2, 0, maxX*2/3, maxY-2)

	if err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}

		sb.childViews = append(sb.childViews, showBugView)
		v.Frame = false
	}

	v.Clear()
	err = sb.renderMain(g, v)
	if err != nil {
		return err
	}

	v, err = g.SetView(showBugSidebarView, maxX*2/3+1, 0, maxX-1, maxY-2)

	if err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}

		sb.childViews = append(sb.childViews, showBugSidebarView)
		v.Frame = true
	}

	v.Clear()
	sb.renderSidebar(v)

	v, err = g.SetView(showBugInstructionView, -1, maxY-2, maxX, maxY)

	if err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}

		sb.childViews = append(sb.childViews, showBugInstructionView)
		v.Frame = false
		v.BgColor = gocui.ColorBlue

		fmt.Fprintf(v, "[q] Return [c] Comment [t] Change title")
	}

	_, err = g.SetCurrentView(showBugView)
	return err
}

func (sb *showBug) keybindings(g *gocui.Gui) error {
	// Return
	if err := g.SetKeybinding(showBugView, 'q', gocui.ModNone, sb.back); err != nil {
		return err
	}

	// Scrolling
	if err := g.SetKeybinding(showBugView, gocui.KeyPgup, gocui.ModNone,
		sb.scrollUp); err != nil {
		return err
	}
	if err := g.SetKeybinding(showBugView, gocui.KeyPgdn, gocui.ModNone,
		sb.scrollDown); err != nil {
		return err
	}

	// Comment
	if err := g.SetKeybinding(showBugView, 'c', gocui.ModNone,
		sb.comment); err != nil {
		return err
	}

	// Title
	if err := g.SetKeybinding(showBugView, 't', gocui.ModNone,
		sb.title); err != nil {
		return err
	}

	// Labels

	return nil
}

func (sb *showBug) disable(g *gocui.Gui) error {
	for _, view := range sb.childViews {
		if err := g.DeleteView(view); err != nil {
			return err
		}
	}
	return nil
}

func (sb *showBug) renderMain(g *gocui.Gui, mainView *gocui.View) error {
	maxX, _ := mainView.Size()
	x0, y0, _, _, _ := g.ViewPosition(mainView.Name())
	snap := sb.bug.Snapshot()

	v, err := g.SetView("showBugHeader", x0, y0, maxX+1, y0+4)

	if err != nil {
		if err != gocui.ErrUnknownView {
			return err
		}

		sb.childViews = append(sb.childViews, "showBugHeader")
		v.Frame = false
	}
	y0 += 4

	v.Clear()
	header1 := fmt.Sprintf("[%s] %s", snap.HumanId(), snap.Title)
	fmt.Fprintf(v, util.LeftPaddedString(header1, maxX, 0)+"\n\n")

	header2 := fmt.Sprintf("[%s] %s opened this bug on %s",
		snap.Status, snap.Author.Name, snap.CreatedAt.Format(timeLayout))
	fmt.Fprintf(v, util.LeftPaddedString(header2, maxX, 0))

	for i, op := range snap.Operations {
		viewName := fmt.Sprintf("op%d", i)

		switch op.(type) {

		case operations.CreateOperation:
			create := op.(operations.CreateOperation)
			content, lines := util.TextWrap(create.Message, maxX)

			v, err := sb.createOpView(g, viewName, x0, y0, maxX, lines)
			if err != nil {
				return err
			}
			fmt.Fprint(v, content)
			y0 += lines + 2

		case operations.AddCommentOperation:
			comment := op.(operations.AddCommentOperation)

			header := fmt.Sprintf("%s commented on %s",
				comment.Author.Name, comment.Time().Format(timeLayout))
			header = util.LeftPaddedString(header, maxX, 6)
			message, lines := util.TextWrap(comment.Message, maxX)

			v, err := sb.createOpView(g, viewName, x0, y0, maxX, lines+2)
			if err != nil {
				return err
			}
			fmt.Fprint(v, header, "\n\n", message)
			y0 += lines + 3
		}
	}

	return nil

}

func (sb *showBug) createOpView(g *gocui.Gui, name string, x0 int, y0 int, maxX int, height int) (*gocui.View, error) {
	v, err := g.SetView(name, x0, y0, maxX+3, y0+height+1)

	if err != nil {
		if err != gocui.ErrUnknownView {
			return nil, err
		}

		sb.childViews = append(sb.childViews, name)
		v.Frame = false
	}

	v.Clear()

	return v, nil
}

func (sb *showBug) renderSidebar(v *gocui.View) {
	maxX, _ := v.Size()
	snap := sb.bug.Snapshot()

	title := util.LeftPaddedString("LABEL", maxX, 2)
	fmt.Fprintf(v, title+"\n\n")

	for _, label := range snap.Labels {
		fmt.Fprintf(v, util.LeftPaddedString(label.String(), maxX, 2))
		fmt.Fprintln(v)
	}
}

func (sb *showBug) back(g *gocui.Gui, v *gocui.View) error {
	ui.activateWindow(ui.bugTable)
	return nil
}

func (sb *showBug) scrollUp(g *gocui.Gui, v *gocui.View) error {
	return nil
}

func (sb *showBug) scrollDown(g *gocui.Gui, v *gocui.View) error {
	return nil
}

func (sb *showBug) comment(g *gocui.Gui, v *gocui.View) error {
	return addCommentWithEditor(sb.bug)
}

func (sb *showBug) title(g *gocui.Gui, v *gocui.View) error {
	return setTitleWithEditor(sb.bug)
}