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
|
package termui
import (
"fmt"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/cache"
"github.com/jroimartin/gocui"
)
type bugTable struct {
cache cache.RepoCacher
bugs []*bug.Snapshot
cursor int
}
func newBugTable(cache cache.RepoCacher) *bugTable {
return &bugTable{
cache: cache,
cursor: 0,
}
}
func (bt *bugTable) paginate(max int) error {
allIds, err := bt.cache.AllBugIds()
if err != nil {
return err
}
return bt.doPaginate(allIds, max)
}
func (bt *bugTable) nextPage(max int) error {
allIds, err := bt.cache.AllBugIds()
if err != nil {
return err
}
if bt.cursor+max > len(allIds) {
return nil
}
bt.cursor += max
return bt.doPaginate(allIds, max)
}
func (bt *bugTable) previousPage(max int) error {
allIds, err := bt.cache.AllBugIds()
if err != nil {
return err
}
bt.cursor = maxInt(0, bt.cursor-max)
return bt.doPaginate(allIds, max)
}
func (bt *bugTable) doPaginate(allIds []string, max int) error {
// clamp the cursor
bt.cursor = maxInt(bt.cursor, 0)
bt.cursor = minInt(bt.cursor, len(allIds)-1)
// slice the data
nb := minInt(len(allIds)-bt.cursor, max)
ids := allIds[bt.cursor:nb]
bt.bugs = make([]*bug.Snapshot, len(ids))
for i, id := range ids {
b, err := bt.cache.ResolveBug(id)
if err != nil {
return err
}
bt.bugs[i] = b.Snapshot()
}
return nil
}
func (bt *bugTable) layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
v, err := g.SetView("header", -1, -1, maxX, 3)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Frame = false
}
v.Clear()
ui.bugTable.renderHeader(v, maxX)
v, err = g.SetView("table", -1, 1, maxX, maxY-2)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Frame = false
v.Highlight = true
v.SelBgColor = gocui.ColorWhite
v.SelFgColor = gocui.ColorBlack
_, err = g.SetCurrentView("table")
if err != nil {
return err
}
}
_, tableHeight := v.Size()
err = bt.paginate(tableHeight)
if err != nil {
return err
}
v.Clear()
ui.bugTable.render(v, maxX)
v, err = g.SetView("footer", -1, maxY-3, maxX, maxY)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Frame = false
}
v.Clear()
ui.bugTable.renderFooter(v, maxX)
v, err = g.SetView("instructions", -1, maxY-2, maxX, maxY)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Frame = false
v.BgColor = gocui.ColorBlue
fmt.Fprintf(v, "[q] Quit [h] Go back [j] Down [k] Up [l] Go forward [m] Load Additional [p] Play [enter] Play and Exit")
}
return nil
}
func (bt *bugTable) getTableLength() int {
return len(bt.bugs)
}
func (bt *bugTable) render(v *gocui.View, maxX int) {
for _, b := range bt.bugs {
fmt.Fprintln(v, b.Title)
}
}
func (bt *bugTable) renderHeader(v *gocui.View, maxX int) {
fmt.Fprintf(v, "header")
}
func (bt *bugTable) renderFooter(v *gocui.View, maxX int) {
fmt.Fprintf(v, "footer")
}
func maxInt(a, b int) int {
if a > b {
return a
}
return b
}
func minInt(a, b int) int {
if a > b {
return b
}
return a
}
|