diff --git a/modules/github/display.go b/modules/github/display.go index cb0f9232..a5b02153 100644 --- a/modules/github/display.go +++ b/modules/github/display.go @@ -12,6 +12,19 @@ func (widget *Widget) display() { func (widget *Widget) content() (string, string, bool) { repo := widget.currentGithubRepo() + username := widget.settings.username + + // Choses the correct place to scroll to when changing sources + if len(widget.View.GetHighlights()) > 0 { + widget.View.ScrollToHighlight() + } else { + widget.View.ScrollToBeginning() + } + + // initial maxItems count + widget.Items = make([]int, 0) + widget.SetItemCount(len(repo.myReviewRequests((username)))) + title := fmt.Sprintf("%s - %s", widget.CommonSettings().Title, widget.title(repo)) if repo == nil { return title, " GitHub repo data is unavailable ", false @@ -24,9 +37,9 @@ func (widget *Widget) content() (string, string, bool) { str += " [red]Stats[white]\n" str += widget.displayStats(repo) str += "\n [red]Open Review Requests[white]\n" - str += widget.displayMyReviewRequests(repo, widget.settings.username) + str += widget.displayMyReviewRequests(repo, username) str += "\n [red]My Pull Requests[white]\n" - str += widget.displayMyPullRequests(repo, widget.settings.username) + str += widget.displayMyPullRequests(repo, username) for _, customQuery := range widget.settings.customQueries { str += fmt.Sprintf("\n [red]%s[white]\n", customQuery.title) str += widget.displayCustomQuery(repo, customQuery.filter, customQuery.perPage) @@ -38,32 +51,50 @@ func (widget *Widget) content() (string, string, bool) { func (widget *Widget) displayMyPullRequests(repo *GithubRepo, username string) string { prs := repo.myPullRequests(username, widget.settings.enableStatus) - if len(prs) == 0 { + prLength := len(prs) + + if prLength == 0 { return " [grey]none[white]\n" } + maxItems := widget.GetItemCount() + str := "" - for _, pr := range prs { - str += fmt.Sprintf(" %s[green]%4d[white] %s\n", widget.mergeString(pr), *pr.Number, *pr.Title) + for idx, pr := range prs { + str += fmt.Sprintf(` %s[green]["%d"]%4d[""][white] %s`, widget.mergeString(pr), maxItems + idx, *pr.Number, *pr.Title) + str += "\n" + widget.Items = append(widget.Items, *pr.Number) } + widget.SetItemCount(maxItems + prLength) + return str } func (widget *Widget) displayCustomQuery(repo *GithubRepo, filter string, perPage int) string { res := repo.customIssueQuery(filter, perPage) + if res == nil { return " [grey]Invalid Query[white]\n" } - if len(res.Issues) == 0 { + issuesLength := len(res.Issues) + + if issuesLength == 0 { return " [grey]none[white]\n" } + + maxItems := widget.GetItemCount() str := "" - for _, issue := range res.Issues { - str += fmt.Sprintf(" [green]%4d[white] %s\n", *issue.Number, *issue.Title) + for idx, issue := range res.Issues { + str += fmt.Sprintf(` [green]["%d"]%4d[""][white] %s`, maxItems + idx , *issue.Number, *issue.Title) + str += "\n" + widget.Items = append(widget.Items, *issue.Number) } + + widget.SetItemCount(maxItems + issuesLength) + return str } @@ -75,8 +106,10 @@ func (widget *Widget) displayMyReviewRequests(repo *GithubRepo, username string) } str := "" - for _, pr := range prs { - str += fmt.Sprintf(" [green]%4d[white] %s\n", *pr.Number, *pr.Title) + for idx, pr := range prs { + str += fmt.Sprintf(` [green]["%d"]%4d[""][white] %s`, idx, *pr.Number, *pr.Title) + str += "\n" + widget.Items = append(widget.Items, *pr.Number) } return str diff --git a/modules/github/keyboard.go b/modules/github/keyboard.go index d3f39bec..ce3d2ace 100644 --- a/modules/github/keyboard.go +++ b/modules/github/keyboard.go @@ -7,11 +7,17 @@ import ( func (widget *Widget) initializeKeyboardControls() { widget.InitializeCommonControls(widget.Refresh) + widget.SetKeyboardChar("j", widget.Next, "Select next item") + widget.SetKeyboardChar("k", widget.Prev, "Select previous item") widget.SetKeyboardChar("l", widget.NextSource, "Select next source") widget.SetKeyboardChar("h", widget.PrevSource, "Select previous source") widget.SetKeyboardChar("o", widget.openRepo, "Open item in browser") + widget.SetKeyboardKey(tcell.KeyDown, widget.Next, "Select next item") + widget.SetKeyboardKey(tcell.KeyUp, widget.Prev, "Select previous item") widget.SetKeyboardKey(tcell.KeyRight, widget.NextSource, "Select next source") widget.SetKeyboardKey(tcell.KeyLeft, widget.PrevSource, "Select previous source") - widget.SetKeyboardKey(tcell.KeyEnter, widget.openRepo, "Open item in browser") + widget.SetKeyboardKey(tcell.KeyEnter, widget.openPr, "Open PR in browser") + widget.SetKeyboardKey(tcell.KeyInsert, widget.openRepo, "Open item in browser") + widget.SetKeyboardKey(tcell.KeyEsc, widget.Unselect, "Clear selection") } diff --git a/modules/github/widget.go b/modules/github/widget.go index 8e3003fb..008f3ea4 100644 --- a/modules/github/widget.go +++ b/modules/github/widget.go @@ -2,8 +2,10 @@ package github import ( "strings" + "strconv" "github.com/rivo/tview" + "github.com/wtfutil/wtf/utils" "github.com/wtfutil/wtf/view" ) @@ -15,6 +17,9 @@ type Widget struct { GithubRepos []*GithubRepo settings *Settings + Selected int + maxItems int + Items []int } func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget { @@ -29,9 +34,12 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) * widget.GithubRepos = widget.buildRepoCollection(widget.settings.repositories) widget.initializeKeyboardControls() + widget.View.SetRegions(true) widget.View.SetInputCapture(widget.InputCapture) widget.SetDisplayFunction(widget.display) + widget.Unselect() + widget.Sources = widget.settings.repositories widget.KeyboardWidget.SetView(widget.View) @@ -40,6 +48,42 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) * } /* -------------------- Exported Functions -------------------- */ +func (widget *Widget) SetItemCount(items int) { + widget.maxItems = items +} + +func (widget *Widget) GetItemCount() int { + return widget.maxItems +} + +func (widget *Widget) GetSelected() int { + if widget.Selected < 0 { + return 0 + } + return widget.Selected +} + +func (widget *Widget) Next() { + widget.Selected++ + if widget.Selected >= widget.maxItems { + widget.Selected = 0 + } + widget.View.Highlight(strconv.Itoa(widget.Selected)).ScrollToHighlight() +} + +func (widget *Widget) Prev() { + widget.Selected-- + if widget.Selected < 0 { + widget.Selected = widget.maxItems - 1 + } + widget.View.Highlight(strconv.Itoa(widget.Selected)).ScrollToHighlight() +} + +func (widget *Widget) Unselect() { + widget.Selected = -1 + widget.View.Highlight() + widget.View.ScrollToBeginning() +} func (widget *Widget) Refresh() { for _, repo := range widget.GithubRepos { @@ -87,6 +131,14 @@ func (widget *Widget) currentGithubRepo() *GithubRepo { return widget.GithubRepos[widget.Idx] } +func (widget *Widget) openPr() { + currentSelection := widget.View.GetHighlights() + if widget.Selected >= 0 && currentSelection[0] != "" { + url := (*widget.currentGithubRepo().RemoteRepo.HTMLURL + "/pull/" + strconv.Itoa(widget.Items[widget.Selected])) + utils.OpenFile(url) + } +} + func (widget *Widget) openRepo() { repo := widget.currentGithubRepo()