forked from mikepea/go-jira-ui
/
ticket_show_page.go
206 lines (190 loc) · 4.7 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
package jiraui
import (
"fmt"
"regexp"
ui "github.com/gizak/termui"
)
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.selectedLine + n + increment) % n
for i := startLine; i != p.selectedLine; i = (i + increment + n) % n {
if s.re.MatchString(p.cachedResults[i]) {
p.SetSelectedLine(i)
p.Update()
break
}
}
}
func (p *TicketShowPage) SelectItem() {
selected := p.cachedResults[p.selectedLine]
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)
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) PreviousPara() {
newDisplayLine := 0
if p.selectedLine == 0 {
return
}
for i := p.selectedLine - 1; i > 0; i-- {
if ok, _ := regexp.MatchString(`^\s*$`, p.cachedResults[i]); ok {
newDisplayLine = i
break
}
}
p.PreviousLine(p.selectedLine - newDisplayLine)
}
func (p *TicketShowPage) NextPara() {
newDisplayLine := len(p.cachedResults) - 1
if p.selectedLine == newDisplayLine {
return
}
for i := p.selectedLine + 1; i < len(p.cachedResults); i++ {
if ok, _ := regexp.MatchString(`^\s*$`, p.cachedResults[i]); ok {
newDisplayLine = i
break
}
}
p.NextLine(newDisplayLine - p.selectedLine)
}
func (p *TicketShowPage) GoBack() {
if len(p.TicketTrail) == 0 {
if ticketListPage != nil {
currentPage = ticketListPage
} else {
currentPage = ticketQueryPage
}
} 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() {
ls := p.uiList
log.Debugf("TicketShowPage.Update(): self: %s (%p), ls: (%p)", p.Id(), p, ls)
p.markActiveLine()
ls.Items = p.displayLines[p.firstDisplayLine:]
ui.Render(ls)
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 == "" {
p.TicketId = ticketListPage.GetSelectedTicketId()
}
if p.MaxWrapWidth == 0 {
if m := p.opts["max_wrap"]; m != nil {
p.MaxWrapWidth = uint(m.(int64))
} else {
p.MaxWrapWidth = defaultMaxWrapWidth
}
}
ui.Clear()
ls := ui.NewList()
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)
p.displayLines = make([]string, len(p.cachedResults))
if p.selectedLine >= len(p.cachedResults) {
p.selectedLine = len(p.cachedResults) - 1
}
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()
}