forked from wtfutil/wtf
/
widget.go
139 lines (108 loc) · 2.6 KB
/
widget.go
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
package github
import (
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"github.com/senorprogrammer/wtf/wtf"
)
const HelpText = `
Keyboard commands for Github:
/: Show/hide this help window
h: Previous git repository
l: Next git repository
r: Refresh the data
arrow left: Previous git repository
arrow right: Next git repository
`
type Widget struct {
wtf.TextWidget
app *tview.Application
pages *tview.Pages
GithubRepos []*GithubRepo
Idx int
}
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(" Github ", "github", true),
app: app,
Idx: 0,
pages: pages,
}
widget.GithubRepos = widget.buildRepoCollection(wtf.Config.UMap("wtf.mods.github.repositories"))
widget.View.SetInputCapture(widget.keyboardIntercept)
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
for _, repo := range widget.GithubRepos {
repo.Refresh()
}
widget.UpdateRefreshedAt()
widget.display()
}
func (widget *Widget) Next() {
widget.Idx = widget.Idx + 1
if widget.Idx == len(widget.GithubRepos) {
widget.Idx = 0
}
widget.display()
}
func (widget *Widget) Prev() {
widget.Idx = widget.Idx - 1
if widget.Idx < 0 {
widget.Idx = len(widget.GithubRepos) - 1
}
widget.display()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) buildRepoCollection(repoData map[string]interface{}) []*GithubRepo {
githubRepos := []*GithubRepo{}
for name, owner := range repoData {
repo := NewGithubRepo(name, owner.(string))
githubRepos = append(githubRepos, repo)
}
return githubRepos
}
func (widget *Widget) currentGithubRepo() *GithubRepo {
if len(widget.GithubRepos) == 0 {
return nil
}
if widget.Idx < 0 || widget.Idx >= len(widget.GithubRepos) {
return nil
}
return widget.GithubRepos[widget.Idx]
}
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
switch string(event.Rune()) {
case "/":
widget.showHelp()
return nil
case "h":
widget.Prev()
return nil
case "l":
widget.Next()
return nil
case "r":
widget.Refresh()
return nil
}
switch event.Key() {
case tcell.KeyLeft:
widget.Prev()
return nil
case tcell.KeyRight:
widget.Next()
return nil
default:
return event
}
}
func (widget *Widget) showHelp() {
closeFunc := func() {
widget.pages.RemovePage("help")
widget.app.SetFocus(widget.View)
}
modal := wtf.NewBillboardModal(HelpText, closeFunc)
widget.pages.AddPage("help", modal, false, true)
widget.app.SetFocus(modal)
}