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

Make Jira module list interactable

Using the todo module as a template, this change adds interactivity
to the Jira module to allow selecting individual issues and opening
them in a web browser.

The j and k keys (or arrow keys) move up and down, and pressing
enter will open the URL for a given issue in the system's default
URL handler.
This commit is contained in:
Bryan Austin 2018-07-02 17:20:39 -07:00
parent 0d713325dc
commit 7b7971d6bf

View File

@ -3,18 +3,24 @@ package jira
import (
"fmt"
"github.com/gdamore/tcell"
"github.com/senorprogrammer/wtf/wtf"
)
type Widget struct {
wtf.TextWidget
result *SearchResult
selected int
}
func NewWidget() *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(" Jira ", "jira", false),
TextWidget: wtf.NewTextWidget(" Jira ", "jira", true),
}
widget.unselect()
widget.View.SetInputCapture(widget.keyboardIntercept)
return &widget
}
@ -29,12 +35,24 @@ func (widget *Widget) Refresh() {
widget.UpdateRefreshedAt()
var content string
if err != nil {
widget.result = nil
widget.View.SetWrap(true)
widget.View.SetTitle(widget.Name)
content = err.Error()
widget.View.SetText(err.Error())
} else {
widget.result = searchResult
}
widget.display()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) display() {
if widget.result == nil {
return
}
widget.View.SetWrap(false)
widget.View.SetTitle(
fmt.Sprintf(
@ -43,13 +61,34 @@ func (widget *Widget) Refresh() {
wtf.Config.UString("wtf.mods.jira.project"),
),
)
content = widget.contentFrom(searchResult)
widget.View.SetText(fmt.Sprintf("%s", widget.contentFrom(widget.result)))
}
widget.View.SetText(content)
func (widget *Widget) next() {
widget.selected++
if widget.result != nil && widget.selected >= len(widget.result.Issues) {
widget.selected = 0
}
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) prev() {
widget.selected--
if widget.selected < 0 && widget.result != nil {
widget.selected = len(widget.result.Issues) - 1
}
}
func (widget *Widget) openItem() {
sel := widget.selected
if sel >= 0 && widget.result != nil && sel < len(widget.result.Issues) {
issue := &widget.result.Issues[widget.selected]
wtf.OpenFile(wtf.Config.UString("wtf.mods.jira.domain") + "/browse/" + issue.Key)
}
}
func (widget *Widget) unselect() {
widget.selected = -1
}
func (widget *Widget) contentFrom(searchResult *SearchResult) string {
str := " [red]Assigned Issues[white]\n"
@ -60,7 +99,7 @@ func (widget *Widget) contentFrom(searchResult *SearchResult) string {
widget.issueTypeColor(&issue),
issue.IssueFields.IssueType.Name,
issue.Key,
wtf.RowColor("jira", idx),
widget.rowColor(idx),
issue.IssueFields.Summary,
)
}
@ -68,6 +107,15 @@ func (widget *Widget) contentFrom(searchResult *SearchResult) string {
return str
}
func (widget *Widget) rowColor(idx int) string {
if widget.View.HasFocus() && (idx == widget.selected) {
foreColor := wtf.Config.UString("wtf.mods.jira.colors.highlight.fore", "black")
backColor := wtf.Config.UString("wtf.mods.jira.colors.highlight.back", "white")
return fmt.Sprintf("%s:%s", foreColor, backColor)
}
return wtf.RowColor("jira", idx)
}
func (widget *Widget) issueTypeColor(issue *Issue) string {
switch issue.IssueFields.IssueType.Name {
case "Bug":
@ -98,3 +146,42 @@ func getProjects() []string {
}
return ret
}
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
switch string(event.Rune()) {
case "j":
// Select the next item down
widget.next()
widget.display()
return nil
case "k":
// Select the next item up
widget.prev()
widget.display()
return nil
}
switch event.Key() {
case tcell.KeyDown:
// Select the next item down
widget.next()
widget.display()
return nil
case tcell.KeyEnter:
widget.openItem()
return nil
case tcell.KeyEsc:
// Unselect the current row
widget.unselect()
widget.display()
return event
case tcell.KeyUp:
// Select the next item up
widget.prev()
widget.display()
return nil
default:
// Pass it along
return event
}
}