mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Define help with keys This means that keys and help are automatically in sync This means that you can't define keys, but forget help This unfortunately also means that formatting may not be quite as good
109 lines
2.0 KiB
Go
109 lines
2.0 KiB
Go
package github
|
|
|
|
import (
|
|
"github.com/rivo/tview"
|
|
"github.com/wtfutil/wtf/wtf"
|
|
)
|
|
|
|
type Widget struct {
|
|
wtf.KeyboardWidget
|
|
wtf.TextWidget
|
|
|
|
GithubRepos []*GithubRepo
|
|
Idx int
|
|
|
|
app *tview.Application
|
|
settings *Settings
|
|
}
|
|
|
|
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
|
widget := Widget{
|
|
KeyboardWidget: wtf.NewKeyboardWidget(app, pages, settings.common),
|
|
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
|
|
|
Idx: 0,
|
|
|
|
app: app,
|
|
settings: settings,
|
|
}
|
|
|
|
widget.GithubRepos = widget.buildRepoCollection(widget.settings.repositories)
|
|
|
|
widget.initializeKeyboardControls()
|
|
widget.View.SetInputCapture(widget.InputCapture)
|
|
|
|
widget.KeyboardWidget.SetView(widget.View)
|
|
|
|
return &widget
|
|
}
|
|
|
|
/* -------------------- Exported Functions -------------------- */
|
|
|
|
func (widget *Widget) Refresh() {
|
|
for _, repo := range widget.GithubRepos {
|
|
repo.Refresh()
|
|
}
|
|
|
|
widget.app.QueueUpdateDraw(func() {
|
|
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),
|
|
widget.settings.apiKey,
|
|
widget.settings.baseURL,
|
|
widget.settings.uploadURL,
|
|
)
|
|
|
|
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) openRepo() {
|
|
repo := widget.currentGithubRepo()
|
|
|
|
if repo != nil {
|
|
repo.Open()
|
|
}
|
|
}
|