Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make github a multisource widget #450

Merged
merged 4 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions modules/github/settings.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package github

import (
"fmt"
"os"

"github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg"
"github.com/wtfutil/wtf/wtf"
)

const defaultTitle = "GitHub"
Expand All @@ -15,7 +17,7 @@ type Settings struct {
apiKey string
baseURL string
enableStatus bool
repositories map[string]interface{}
repositories []string
uploadURL string
username string
}
Expand All @@ -28,10 +30,25 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co
apiKey: ymlConfig.UString("apiKey", os.Getenv("WTF_GITHUB_TOKEN")),
baseURL: ymlConfig.UString("baseURL", os.Getenv("WTF_GITHUB_BASE_URL")),
enableStatus: ymlConfig.UBool("enableStatus", false),
repositories: ymlConfig.UMap("repositories"),
uploadURL: ymlConfig.UString("uploadURL", os.Getenv("WTF_GITHUB_UPLOAD_URL")),
username: ymlConfig.UString("username"),
}
settings.repositories = parseRepositories(ymlConfig)

return &settings
}

func parseRepositories(ymlConfig *config.Config) []string {

result := []string{}
repositories, err := ymlConfig.Map("repositories")
if err == nil {
for key, value := range repositories {
result = append(result, fmt.Sprintf("%s/%s", value, key))
}
return result
}

result = wtf.ToStrs(ymlConfig.UList("repositories"))
return result
}
42 changes: 14 additions & 28 deletions modules/github/widget.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
package github

import (
"strings"

"github.com/rivo/tview"
"github.com/wtfutil/wtf/wtf"
)

type Widget struct {
wtf.MultiSourceWidget
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,
KeyboardWidget: wtf.NewKeyboardWidget(app, pages, settings.common),
MultiSourceWidget: wtf.NewMultiSourceWidget(settings.common, "repository", "repositories"),
TextWidget: wtf.NewTextWidget(app, settings.common, true),

app: app,
settings: settings,
Expand All @@ -31,6 +32,9 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *

widget.initializeKeyboardControls()
widget.View.SetInputCapture(widget.InputCapture)
widget.SetDisplayFunction(widget.display)

widget.Sources = widget.settings.repositories

widget.KeyboardWidget.SetView(widget.View)

Expand All @@ -44,26 +48,6 @@ func (widget *Widget) Refresh() {
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()
}

Expand All @@ -73,13 +57,15 @@ func (widget *Widget) HelpText() string {

/* -------------------- Unexported Functions -------------------- */

func (widget *Widget) buildRepoCollection(repoData map[string]interface{}) []*GithubRepo {
func (widget *Widget) buildRepoCollection(repoData []string) []*GithubRepo {
githubRepos := []*GithubRepo{}

for name, owner := range repoData {
for _, repo := range repoData {
split := strings.Split(repo, "/")
owner, name := split[0], split[1]
repo := NewGithubRepo(
name,
owner.(string),
owner,
widget.settings.apiKey,
widget.settings.baseURL,
widget.settings.uploadURL,
Expand Down