mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Gitlab: allow for opening items in browser
This commit is contained in:
parent
cc8f5f76ba
commit
dbab6be1d4
@ -2,12 +2,29 @@ package gitlab
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/xanzy/go-gitlab"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (widget *Widget) display() {
|
func (widget *Widget) display() {
|
||||||
widget.Redraw(widget.content)
|
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) {
|
func (widget *Widget) content() (string, string, bool) {
|
||||||
|
|
||||||
project := widget.currentGitlabProject()
|
project := widget.currentGitlabProject()
|
||||||
@ -15,6 +32,10 @@ func (widget *Widget) content() (string, string, bool) {
|
|||||||
return widget.CommonSettings().Title, " Gitlab project data is unavailable ", true
|
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))
|
title := fmt.Sprintf("%s - %s", widget.CommonSettings().Title, widget.title(project))
|
||||||
|
|
||||||
_, _, width, _ := widget.View.GetRect()
|
_, _, width, _ := widget.View.GetRect()
|
||||||
@ -39,60 +60,60 @@ func (widget *Widget) content() (string, string, bool) {
|
|||||||
|
|
||||||
func (widget *Widget) displayMyMergeRequests(project *GitlabProject, username string) string {
|
func (widget *Widget) displayMyMergeRequests(project *GitlabProject, username string) string {
|
||||||
mrs := project.myMergeRequests(username)
|
mrs := project.myMergeRequests(username)
|
||||||
|
return widget.renderMergeRequests(mrs, 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (widget *Widget) displayMyAssignedMergeRequests(project *GitlabProject, username string) string {
|
func (widget *Widget) displayMyAssignedMergeRequests(project *GitlabProject, username string) string {
|
||||||
mrs := project.myAssignedMergeRequests(username)
|
mrs := project.myAssignedMergeRequests(username)
|
||||||
|
return widget.renderMergeRequests(mrs, 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (widget *Widget) displayMyAssignedIssues(project *GitlabProject, username string) string {
|
func (widget *Widget) displayMyAssignedIssues(project *GitlabProject, username string) string {
|
||||||
issues := project.myAssignedIssues(username)
|
issues := project.myAssignedIssues(username)
|
||||||
|
return widget.renderIssues(issues, 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (widget *Widget) displayMyIssues(project *GitlabProject, username string) string {
|
func (widget *Widget) displayMyIssues(project *GitlabProject, username string) string {
|
||||||
issues := project.myIssues(username)
|
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"
|
return " [grey]none[white]\n"
|
||||||
}
|
}
|
||||||
|
maxItems := widget.GetItemCount()
|
||||||
|
|
||||||
str := ""
|
str := ""
|
||||||
for _, issue := range issues {
|
for idx, issue := range mrs {
|
||||||
str += fmt.Sprintf(" [green]%4d[white] %s\n", issue.IID, issue.Title)
|
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
|
return str
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,24 @@
|
|||||||
package gitlab
|
package gitlab
|
||||||
|
|
||||||
import "github.com/gdamore/tcell"
|
import (
|
||||||
|
"github.com/gdamore/tcell"
|
||||||
|
)
|
||||||
|
|
||||||
func (widget *Widget) initializeKeyboardControls() {
|
func (widget *Widget) initializeKeyboardControls() {
|
||||||
widget.InitializeCommonControls(widget.Refresh)
|
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("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.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")
|
||||||
}
|
}
|
||||||
|
@ -1,23 +1,37 @@
|
|||||||
package gitlab
|
package gitlab
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
|
"github.com/wtfutil/wtf/utils"
|
||||||
"github.com/wtfutil/wtf/view"
|
"github.com/wtfutil/wtf/view"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ContentItem struct {
|
||||||
|
Type string
|
||||||
|
ID int
|
||||||
|
}
|
||||||
|
|
||||||
type Widget struct {
|
type Widget struct {
|
||||||
view.KeyboardWidget
|
|
||||||
view.MultiSourceWidget
|
view.MultiSourceWidget
|
||||||
|
view.KeyboardWidget
|
||||||
view.TextWidget
|
view.TextWidget
|
||||||
|
|
||||||
GitlabProjects []*GitlabProject
|
GitlabProjects []*GitlabProject
|
||||||
|
|
||||||
context *context
|
context *context
|
||||||
settings *Settings
|
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 {
|
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||||
context, _ := newContext(settings)
|
context, err := newContext(settings)
|
||||||
|
|
||||||
widget := Widget{
|
widget := Widget{
|
||||||
KeyboardWidget: view.NewKeyboardWidget(app, pages, settings.common),
|
KeyboardWidget: view.NewKeyboardWidget(app, pages, settings.common),
|
||||||
@ -26,14 +40,19 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
|||||||
|
|
||||||
context: context,
|
context: context,
|
||||||
settings: settings,
|
settings: settings,
|
||||||
|
|
||||||
|
configError: err,
|
||||||
}
|
}
|
||||||
|
|
||||||
widget.GitlabProjects = widget.buildProjectCollection(context, settings.projects)
|
widget.GitlabProjects = widget.buildProjectCollection(context, settings.projects)
|
||||||
|
|
||||||
widget.initializeKeyboardControls()
|
widget.initializeKeyboardControls()
|
||||||
|
widget.View.SetRegions(true)
|
||||||
widget.View.SetInputCapture(widget.InputCapture)
|
widget.View.SetInputCapture(widget.InputCapture)
|
||||||
widget.SetDisplayFunction(widget.display)
|
widget.SetDisplayFunction(widget.display)
|
||||||
|
|
||||||
|
widget.Unselect()
|
||||||
|
|
||||||
widget.KeyboardWidget.SetView(widget.View)
|
widget.KeyboardWidget.SetView(widget.View)
|
||||||
|
|
||||||
return &widget
|
return &widget
|
||||||
@ -42,6 +61,11 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
|||||||
/* -------------------- Exported Functions -------------------- */
|
/* -------------------- Exported Functions -------------------- */
|
||||||
|
|
||||||
func (widget *Widget) Refresh() {
|
func (widget *Widget) Refresh() {
|
||||||
|
if widget.context == nil || widget.configError != nil {
|
||||||
|
widget.displayError()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
for _, project := range widget.GitlabProjects {
|
for _, project := range widget.GitlabProjects {
|
||||||
project.Refresh()
|
project.Refresh()
|
||||||
}
|
}
|
||||||
@ -53,6 +77,49 @@ func (widget *Widget) HelpText() string {
|
|||||||
return widget.KeyboardWidget.HelpText()
|
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 -------------------- */
|
/* -------------------- Unexported Functions -------------------- */
|
||||||
|
|
||||||
func (widget *Widget) buildProjectCollection(context *context, projectData []string) []*GitlabProject {
|
func (widget *Widget) buildProjectCollection(context *context, projectData []string) []*GitlabProject {
|
||||||
@ -77,3 +144,54 @@ func (widget *Widget) currentGitlabProject() *GitlabProject {
|
|||||||
|
|
||||||
return widget.GitlabProjects[widget.Idx]
|
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)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user