1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Merge branch 'mogensen-feature/gitlab-with-browser'

This commit is contained in:
Chris Cummer 2020-04-28 20:52:40 -07:00
commit d712769511
3 changed files with 192 additions and 42 deletions

View File

@ -2,12 +2,29 @@ package gitlab
import (
"fmt"
"github.com/xanzy/go-gitlab"
)
func (widget *Widget) display() {
widget.Redraw(widget.content)
}
func (widget *Widget) displayError() {
widget.Redraw(widget.contentError)
}
func (widget *Widget) contentError() (string, string, bool) {
title := fmt.Sprintf("%s - Error", widget.CommonSettings().Title)
if widget.configError != nil {
return title, fmt.Sprintf("Error: \n [red]%v[white]", widget.configError), false
}
return title, "Error", false
}
func (widget *Widget) content() (string, string, bool) {
project := widget.currentGitlabProject()
@ -15,6 +32,10 @@ func (widget *Widget) content() (string, string, bool) {
return widget.CommonSettings().Title, " Gitlab project data is unavailable ", true
}
// initial maxItems count
widget.Items = make([]ContentItem, 0)
widget.SetItemCount(0)
title := fmt.Sprintf("%s - %s", widget.CommonSettings().Title, widget.title(project))
_, _, width, _ := widget.View.GetRect()
@ -39,60 +60,60 @@ func (widget *Widget) content() (string, string, bool) {
func (widget *Widget) displayMyMergeRequests(project *GitlabProject, username string) string {
mrs := project.myMergeRequests(username)
if len(mrs) == 0 {
return " [grey]none[white]\n"
}
str := ""
for _, mr := range mrs {
str += fmt.Sprintf(" [green]%4d[white] %s\n", mr.IID, mr.Title)
}
return str
return widget.renderMergeRequests(mrs, username)
}
func (widget *Widget) displayMyAssignedMergeRequests(project *GitlabProject, username string) string {
mrs := project.myAssignedMergeRequests(username)
if len(mrs) == 0 {
return " [grey]none[white]\n"
}
str := ""
for _, mr := range mrs {
str += fmt.Sprintf(" [green]%4d[white] %s\n", mr.IID, mr.Title)
}
return str
return widget.renderMergeRequests(mrs, username)
}
func (widget *Widget) displayMyAssignedIssues(project *GitlabProject, username string) string {
issues := project.myAssignedIssues(username)
if len(issues) == 0 {
return " [grey]none[white]\n"
}
str := ""
for _, issue := range issues {
str += fmt.Sprintf(" [green]%4d[white] %s\n", issue.IID, issue.Title)
}
return str
return widget.renderIssues(issues, username)
}
func (widget *Widget) displayMyIssues(project *GitlabProject, username string) string {
issues := project.myIssues(username)
return widget.renderIssues(issues, username)
}
if len(issues) == 0 {
func (widget *Widget) renderMergeRequests(mrs []*gitlab.MergeRequest, username string) string {
length := len(mrs)
if length == 0 {
return " [grey]none[white]\n"
}
maxItems := widget.GetItemCount()
str := ""
for _, issue := range issues {
str += fmt.Sprintf(" [green]%4d[white] %s\n", issue.IID, issue.Title)
for idx, issue := range mrs {
str += fmt.Sprintf(` [green]["%d"]%4d[""][white] %s`, maxItems+idx, issue.IID, issue.Title)
str += "\n"
widget.Items = append(widget.Items, ContentItem{Type: "MR", ID: issue.IID})
}
widget.SetItemCount(maxItems + length)
return str
}
func (widget *Widget) renderIssues(issues []*gitlab.Issue, username string) string {
length := len(issues)
if length == 0 {
return " [grey]none[white]\n"
}
maxItems := widget.GetItemCount()
str := ""
for idx, issue := range issues {
str += fmt.Sprintf(` [green]["%d"]%4d[""][white] %s`, maxItems+idx, issue.IID, issue.Title)
str += "\n"
widget.Items = append(widget.Items, ContentItem{Type: "ISSUE", ID: issue.IID})
}
widget.SetItemCount(maxItems + length)
return str
}

View File

@ -1,13 +1,24 @@
package gitlab
import "github.com/gdamore/tcell"
import (
"github.com/gdamore/tcell"
)
func (widget *Widget) initializeKeyboardControls() {
widget.InitializeCommonControls(widget.Refresh)
widget.SetKeyboardChar("h", widget.PrevSource, "Select previous project")
widget.SetKeyboardChar("j", widget.Next, "Select next item")
widget.SetKeyboardChar("k", widget.Prev, "Select previous item")
widget.SetKeyboardChar("l", widget.NextSource, "Select next project")
widget.SetKeyboardChar("h", widget.PrevSource, "Select previous project")
widget.SetKeyboardChar("o", widget.openRepo, "Open item in browser")
widget.SetKeyboardChar("p", widget.openPulls, "Open merge requests in browser")
widget.SetKeyboardChar("i", widget.openIssues, "Open issues in browser")
widget.SetKeyboardKey(tcell.KeyLeft, widget.PrevSource, "Select previous project")
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 project")
widget.SetKeyboardKey(tcell.KeyLeft, widget.PrevSource, "Select previous project")
widget.SetKeyboardKey(tcell.KeyEnter, widget.openItemInBrowser, "Open item in browser")
widget.SetKeyboardKey(tcell.KeyEsc, widget.Unselect, "Clear selection")
}

View File

@ -1,23 +1,37 @@
package gitlab
import (
"strconv"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view"
)
type ContentItem struct {
Type string
ID int
}
type Widget struct {
view.KeyboardWidget
view.MultiSourceWidget
view.KeyboardWidget
view.TextWidget
GitlabProjects []*GitlabProject
context *context
settings *Settings
Selected int
maxItems int
Items []ContentItem
configError error
}
// NewWidget creates a new instance of the widget
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
context, _ := newContext(settings)
context, err := newContext(settings)
widget := Widget{
KeyboardWidget: view.NewKeyboardWidget(app, pages, settings.common),
@ -26,14 +40,19 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
context: context,
settings: settings,
configError: err,
}
widget.GitlabProjects = widget.buildProjectCollection(context, settings.projects)
widget.initializeKeyboardControls()
widget.View.SetRegions(true)
widget.View.SetInputCapture(widget.InputCapture)
widget.SetDisplayFunction(widget.display)
widget.Unselect()
widget.KeyboardWidget.SetView(widget.View)
return &widget
@ -42,6 +61,11 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
if widget.context == nil || widget.configError != nil {
widget.displayError()
return
}
for _, project := range widget.GitlabProjects {
project.Refresh()
}
@ -53,6 +77,49 @@ func (widget *Widget) HelpText() string {
return widget.KeyboardWidget.HelpText()
}
// SetItemCount sets the amount of PRs RRs and other PRs throughout the widgets display creation
func (widget *Widget) SetItemCount(items int) {
widget.maxItems = items
}
// GetItemCount returns the amount of PRs RRs and other PRs calculated so far as an int
func (widget *Widget) GetItemCount() int {
return widget.maxItems
}
// GetSelected returns the index of the currently highlighted item as an int
func (widget *Widget) GetSelected() int {
if widget.Selected < 0 {
return 0
}
return widget.Selected
}
// Next cycles the currently highlighted text down
func (widget *Widget) Next() {
widget.Selected++
if widget.Selected >= widget.maxItems {
widget.Selected = 0
}
widget.View.Highlight(strconv.Itoa(widget.Selected)).ScrollToHighlight()
}
// Prev cycles the currently highlighted text up
func (widget *Widget) Prev() {
widget.Selected--
if widget.Selected < 0 {
widget.Selected = widget.maxItems - 1
}
widget.View.Highlight(strconv.Itoa(widget.Selected)).ScrollToHighlight()
}
// Unselect stops highlighting the text and jumps the scroll position to the top
func (widget *Widget) Unselect() {
widget.Selected = -1
widget.View.Highlight()
widget.View.ScrollToBeginning()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) buildProjectCollection(context *context, projectData []string) []*GitlabProject {
@ -77,3 +144,54 @@ func (widget *Widget) currentGitlabProject() *GitlabProject {
return widget.GitlabProjects[widget.Idx]
}
func (widget *Widget) openItemInBrowser() {
currentSelection := widget.View.GetHighlights()
if widget.Selected >= 0 && currentSelection[0] != "" {
item := widget.Items[widget.Selected]
url := ""
project := widget.currentGitlabProject()
if project == nil {
// This is a problem. We will just bail out for now
return
}
switch item.Type {
case "MR":
url = (project.RemoteProject.WebURL + "/merge_requests/" + strconv.Itoa(item.ID))
case "ISSUE":
url = (project.RemoteProject.WebURL + "/issues/" + strconv.Itoa(item.ID))
}
utils.OpenFile(url)
}
}
func (widget *Widget) openRepo() {
project := widget.currentGitlabProject()
if project == nil {
return
}
url := project.RemoteProject.WebURL
utils.OpenFile(url)
}
func (widget *Widget) openPulls() {
project := widget.currentGitlabProject()
if project == nil {
return
}
url := project.RemoteProject.WebURL + "/merge_requests/"
utils.OpenFile(url)
}
func (widget *Widget) openIssues() {
project := widget.currentGitlabProject()
if project == nil {
return
}
url := project.RemoteProject.WebURL + "/issues/"
utils.OpenFile(url)
}