1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/gitlab/widget.go
Kirill Motkov f0771cd013 Some code improvements
* Some assignments simplified by using assignment operators
* Rewrite switch statement with only one case as if.
* Rewrite if-else chain as a switch statement.
* go fmt `modules/todoist/project.go` file.
2019-05-21 17:29:09 +03:00

104 lines
2.1 KiB
Go

package gitlab
import (
"github.com/rivo/tview"
"github.com/wtfutil/wtf/wtf"
glb "github.com/xanzy/go-gitlab"
)
type Widget struct {
wtf.KeyboardWidget
wtf.TextWidget
GitlabProjects []*GitlabProject
Idx int
gitlab *glb.Client
settings *Settings
}
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
baseURL := settings.domain
gitlab := glb.NewClient(nil, settings.apiKey)
if baseURL != "" {
gitlab.SetBaseURL(baseURL)
}
widget := Widget{
KeyboardWidget: wtf.NewKeyboardWidget(app, pages, settings.common),
TextWidget: wtf.NewTextWidget(app, settings.common, true),
Idx: 0,
gitlab: gitlab,
settings: settings,
}
widget.GitlabProjects = widget.buildProjectCollection(settings.projects)
widget.initializeKeyboardControls()
widget.View.SetInputCapture(widget.InputCapture)
widget.KeyboardWidget.SetView(widget.View)
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
for _, project := range widget.GitlabProjects {
project.Refresh()
}
widget.display()
}
func (widget *Widget) Next() {
widget.Idx++
if widget.Idx == len(widget.GitlabProjects) {
widget.Idx = 0
}
widget.display()
}
func (widget *Widget) Prev() {
widget.Idx--
if widget.Idx < 0 {
widget.Idx = len(widget.GitlabProjects) - 1
}
widget.display()
}
func (widget *Widget) HelpText() string {
return widget.KeyboardWidget.HelpText()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) buildProjectCollection(projectData map[string]interface{}) []*GitlabProject {
gitlabProjects := []*GitlabProject{}
for name, namespace := range projectData {
project := NewGitlabProject(name, namespace.(string), widget.gitlab)
gitlabProjects = append(gitlabProjects, project)
}
return gitlabProjects
}
func (widget *Widget) currentGitlabProject() *GitlabProject {
if len(widget.GitlabProjects) == 0 {
return nil
}
if widget.Idx < 0 || widget.Idx >= len(widget.GitlabProjects) {
return nil
}
return widget.GitlabProjects[widget.Idx]
}