-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathticket_show_page.go
242 lines (224 loc) · 5.27 KB
/
ticket_show_page.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package jiraui
import (
"fmt"
"regexp"
ui "gopkg.in/gizak/termui.v2"
)
const (
defaultMaxWrapWidth = 100
)
type TicketShowPage struct {
BaseListPage
CommandBarFragment
StatusBarFragment
MaxWrapWidth uint
TicketId string
Template string
apiBody interface{}
TicketTrail []*TicketShowPage // previously viewed tickets in drill-down
WrapWidth uint
opts map[string]interface{}
}
func (p *TicketShowPage) Search() {
s := p.ActiveSearch
n := len(p.cachedResults)
if s.command == "" {
return
}
increment := 1
if s.directionUp {
increment = -1
}
// we use modulo here so we can loop through every line.
// adding 'n' means we never have '-1 % n'.
startLine := (p.uiList.Cursor + n + increment) % n
for i := startLine; i != p.uiList.Cursor; i = (i + increment + n) % n {
if s.re.MatchString(p.cachedResults[i]) {
p.uiList.SetCursorLine(i)
p.Update()
break
}
}
}
func (p *TicketShowPage) SelectItem() {
selected := p.cachedResults[p.uiList.Cursor]
if ok, _ := regexp.MatchString(`^epic_links:`, selected); ok {
q := new(TicketListPage)
q.ActiveQuery.Name = fmt.Sprintf("Open Tasks in Epic %s", p.TicketId)
q.ActiveQuery.JQL = fmt.Sprintf("\"Epic Link\" = %s AND resolution = Unresolved", p.TicketId)
previousPages = append(previousPages, currentPage)
currentPage = q
} else {
newTicketId := findTicketIdInString(selected)
if newTicketId == "" {
return
} else if newTicketId == p.TicketId {
return
}
q := new(TicketShowPage)
q.TicketId = newTicketId
q.TicketTrail = append(p.TicketTrail, p)
currentPage = q
}
changePage()
}
func (p *TicketShowPage) Id() string {
return p.TicketId
}
func (p *TicketShowPage) NextTicket() {
if len(previousPages) == 0 {
return
}
pp := previousPages[len(previousPages)-1]
switch pp := pp.(type) {
case *TicketListPage:
line := pp.uiList.Cursor
pp.uiList.SilentCursorDownLines(1)
if pp.uiList.Cursor == line {
return
}
q := new(TicketShowPage)
q.TicketId = pp.ActiveTicketId()
currentPage = q
changePage()
}
return
}
func (p *TicketShowPage) PrevTicket() {
if len(previousPages) == 0 {
return
}
pp := previousPages[len(previousPages)-1]
switch pp := pp.(type) {
case *TicketListPage:
line := pp.uiList.Cursor
pp.uiList.SilentCursorUpLines(1)
if pp.uiList.Cursor == line {
return
}
q := new(TicketShowPage)
q.TicketId = pp.ActiveTicketId()
currentPage = q
changePage()
}
return
}
func (p *TicketShowPage) PreviousPara() {
newDisplayLine := 0
sl := p.uiList.Cursor
if sl == 0 {
return
}
for i := sl - 1; i > 0; i-- {
if ok, _ := regexp.MatchString(`^\s*$`, p.cachedResults[i]); ok {
newDisplayLine = i
break
}
}
p.PreviousLine(sl - newDisplayLine)
}
func (p *TicketShowPage) NextPara() {
newDisplayLine := len(p.cachedResults) - 1
sl := p.uiList.Cursor
if sl == newDisplayLine {
return
}
for i := sl + 1; i < len(p.cachedResults); i++ {
if ok, _ := regexp.MatchString(`^\s*$`, p.cachedResults[i]); ok {
newDisplayLine = i
break
}
}
p.NextLine(newDisplayLine - sl)
}
func (p *TicketShowPage) GoBack() {
if len(p.TicketTrail) == 0 {
if len(previousPages) > 0 {
currentPage, previousPages = previousPages[len(previousPages)-1], previousPages[:len(previousPages)-1]
} else {
currentPage = new(QueryPage)
}
} else {
last := len(p.TicketTrail) - 1
currentPage = p.TicketTrail[last]
}
changePage()
}
func (p *TicketShowPage) EditTicket() {
runJiraCmdEdit(p.TicketId)
}
func (p *TicketShowPage) ActiveTicketId() string {
return p.TicketId
}
func (p *TicketShowPage) ticketTrailAsString() (trail string) {
for i := len(p.TicketTrail) - 1; i >= 0; i-- {
q := *p.TicketTrail[i]
trail = trail + " <- " + q.Id()
}
return trail
}
func (p *TicketShowPage) Refresh() {
pDeref := &p
q := *pDeref
q.cachedResults = make([]string, 0)
q.apiBody = nil
currentPage = q
changePage()
q.Create()
}
func (p *TicketShowPage) Update() {
ui.Render(p.uiList)
p.statusBar.Update()
p.commandBar.Update()
}
func (p *TicketShowPage) Create() {
log.Debugf("TicketShowPage.Create(): self: %s (%p)", p.Id(), p)
log.Debugf("TicketShowPage.Create(): currentPage: %s (%p)", currentPage.Id(), currentPage)
p.opts = getJiraOpts()
if p.TicketId == "" {
return
}
if p.MaxWrapWidth == 0 {
if m := p.opts["max_wrap"]; m != nil {
p.MaxWrapWidth = uint(m.(int64))
} else {
p.MaxWrapWidth = defaultMaxWrapWidth
}
}
ui.Clear()
ls := NewScrollableList()
if p.statusBar == nil {
p.statusBar = new(StatusBar)
}
if p.commandBar == nil {
p.commandBar = commandBar
}
p.uiList = ls
if p.Template == "" {
if templateOpt := p.opts["template"]; templateOpt == nil {
p.Template = "jira_ui_view"
} else {
p.Template = templateOpt.(string)
}
}
innerWidth := uint(ui.TermWidth()) - 3
if innerWidth < p.MaxWrapWidth {
p.WrapWidth = innerWidth
} else {
p.WrapWidth = p.MaxWrapWidth
}
if p.apiBody == nil {
p.apiBody, _ = FetchJiraTicket(p.TicketId)
}
p.cachedResults = WrapText(JiraTicketAsStrings(p.apiBody, p.Template), p.WrapWidth)
ls.Items = p.cachedResults
ls.ItemFgColor = ui.ColorYellow
ls.Height = ui.TermHeight() - 2
ls.Width = ui.TermWidth()
ls.Border = true
ls.BorderLabel = fmt.Sprintf("%s %s", p.TicketId, p.ticketTrailAsString())
ls.Y = 0
p.statusBar.Create()
p.commandBar.Create()
p.Update()
}