-
-
Notifications
You must be signed in to change notification settings - Fork 805
/
widget.go
157 lines (126 loc) · 3.81 KB
/
widget.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package jira
import (
"fmt"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view"
)
type Widget struct {
view.ScrollableWidget
result *SearchResult
settings *Settings
err error
}
func NewWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, settings *Settings) *Widget {
widget := Widget{
ScrollableWidget: view.NewScrollableWidget(tviewApp, redrawChan, pages, settings.Common),
settings: settings,
}
widget.SetRenderFunction(widget.Render)
widget.initializeKeyboardControls()
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
searchResult, err := widget.IssuesFor(
widget.settings.username,
widget.settings.projects,
widget.settings.jql,
)
if err != nil {
widget.err = err
widget.result = nil
widget.SetItemCount(0)
} else {
widget.err = nil
widget.result = searchResult
widget.SetItemCount(len(searchResult.Issues))
}
widget.Render()
}
func (widget *Widget) Render() {
widget.Redraw(widget.content)
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) openItem() {
sel := widget.GetSelected()
if sel >= 0 && widget.result != nil && sel < len(widget.result.Issues) {
issue := &widget.result.Issues[sel]
utils.OpenFile(widget.settings.domain + "/browse/" + issue.Key)
}
}
const MaxIssueTypeLength = 7
const MaxStatusNameLength = 14
func (widget *Widget) content() (string, string, bool) {
if widget.err != nil {
return widget.CommonSettings().Title, widget.err.Error(), true
}
title := widget.CommonSettings().Title
str := fmt.Sprintf(" [%s]Assigned Issues[white]\n", widget.settings.Colors.Subheading)
if widget.result == nil || len(widget.result.Issues) == 0 {
return title, "No results to display", false
}
longestIssueTypeLength, longestKeyLength, longestStatusNameLength := getLongestColumnLengths(widget.result.Issues)
for idx, issue := range widget.result.Issues {
row := fmt.Sprintf(
`[%s] [%s]%-*s[white] [green]%-*s[white] [yellow]%-*s[white] [%s]%s`,
widget.RowColor(idx),
widget.issueTypeColor(&issue),
longestIssueTypeLength+1,
trimToMaxLength(issue.IssueFields.IssueType.Name, MaxIssueTypeLength),
longestKeyLength+1,
issue.Key,
longestStatusNameLength+1,
trimToMaxLength(issue.IssueFields.IssueStatus.IName, MaxStatusNameLength),
widget.RowColor(idx),
tview.Escape(issue.IssueFields.Summary),
)
str += utils.HighlightableHelper(widget.View, row, idx, len(issue.IssueFields.Summary))
}
return title, str, false
}
func getLongestColumnLengths(issues []Issue) (int, int, int) {
longestIssueTypeLength := 0
longestKeyLength := 0
longestStatusNameLength := 0
for _, issue := range issues {
issueTypeLength := len(issue.IssueFields.IssueType.Name)
if issueTypeLength > longestIssueTypeLength {
longestIssueTypeLength = issueTypeLength
}
issueKeyLength := len(issue.Key)
if issueKeyLength > longestKeyLength {
longestKeyLength = len("WTF-XXX") // issueKeyLength
}
statusNameLength := len(issue.IssueFields.IssueStatus.IName)
if statusNameLength > longestStatusNameLength {
longestStatusNameLength = statusNameLength
}
}
if longestIssueTypeLength > MaxIssueTypeLength {
longestIssueTypeLength = MaxIssueTypeLength
}
if longestStatusNameLength > MaxStatusNameLength {
longestStatusNameLength = MaxStatusNameLength
}
return longestIssueTypeLength, longestKeyLength, longestStatusNameLength
}
func (*Widget) issueTypeColor(issue *Issue) string {
switch issue.IssueFields.IssueType.Name {
case "Bug":
return "red"
case "Story":
return "blue"
case "Task":
return "orange"
default:
return "white"
}
}
func trimToMaxLength(text string, maxLength int) string {
if len(text) <= maxLength {
return text
} else {
return text[:maxLength]
}
}