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
|
package termui
import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/repository"
"github.com/jroimartin/gocui"
)
type termUI struct {
cache cache.RepoCacher
bugTable *bugTable
}
var ui *termUI
func Run(repo repository.Repo) error {
c := cache.NewRepoCache(repo)
ui = &termUI{
cache: c,
bugTable: newBugTable(c),
}
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
return err
}
defer g.Close()
g.SetManagerFunc(layout)
err = keybindings(g)
if err != nil {
return err
}
err = g.MainLoop()
if err != nil && err != gocui.ErrQuit {
return err
}
return nil
}
func layout(g *gocui.Gui) error {
//maxX, maxY := g.Size()
ui.bugTable.layout(g)
v, err := g.View("bugTable")
if err != nil {
return err
}
cursorClamp(v)
return nil
}
func keybindings(g *gocui.Gui) error {
if err := g.SetKeybinding("", 'q', gocui.ModNone, quit); err != nil {
return err
}
if err := g.SetKeybinding("bugTable", 'j', gocui.ModNone, cursorDown); err != nil {
return err
}
if err := g.SetKeybinding("bugTable", gocui.KeyArrowDown, gocui.ModNone, cursorDown); err != nil {
return err
}
if err := g.SetKeybinding("bugTable", 'k', gocui.ModNone, cursorUp); err != nil {
return err
}
if err := g.SetKeybinding("bugTable", gocui.KeyArrowUp, gocui.ModNone, cursorUp); err != nil {
return err
}
if err := g.SetKeybinding("bugTable", 'h', gocui.ModNone, previousPage); err != nil {
return err
}
if err := g.SetKeybinding("bugTable", gocui.KeyArrowLeft, gocui.ModNone, previousPage); err != nil {
return err
}
if err := g.SetKeybinding("bugTable", 'l', gocui.ModNone, nextPage); err != nil {
return err
}
if err := g.SetKeybinding("bugTable", gocui.KeyArrowRight, gocui.ModNone, nextPage); err != nil {
return err
}
//err = g.SetKeybinding("bugTable", 'p', gocui.ModNone, playSelected)
//err = g.SetKeybinding("bugTable", gocui.KeyEnter, gocui.ModNone, playSelectedAndExit)
//err = g.SetKeybinding("bugTable", 'm', gocui.ModNone, loadNextRecords)
return nil
}
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}
func cursorDown(g *gocui.Gui, v *gocui.View) error {
_, y := v.Cursor()
y = minInt(y+1, ui.bugTable.getTableLength()-1)
err := v.SetCursor(0, y)
if err != nil {
return err
}
return nil
}
func cursorUp(g *gocui.Gui, v *gocui.View) error {
_, y := v.Cursor()
y = maxInt(y-1, 0)
err := v.SetCursor(0, y)
if err != nil {
return err
}
return nil
}
func cursorClamp(v *gocui.View) error {
_, y := v.Cursor()
y = minInt(y, ui.bugTable.getTableLength()-1)
y = maxInt(y, 0)
err := v.SetCursor(0, y)
if err != nil {
return err
}
return nil
}
func nextPage(g *gocui.Gui, v *gocui.View) error {
_, maxY := v.Size()
return ui.bugTable.nextPage(maxY)
}
func previousPage(g *gocui.Gui, v *gocui.View) error {
_, maxY := v.Size()
return ui.bugTable.previousPage(maxY)
}
|