forked from mikepea/go-jira-ui
/
ticket_list_page.go
119 lines (107 loc) · 2.66 KB
/
ticket_list_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
package jiraui
import (
"fmt"
"regexp"
ui "github.com/gizak/termui"
)
type TicketListPage struct {
BaseListPage
CommandBarFragment
StatusBarFragment
ActiveQuery Query
ActiveSort Sort
}
func (p *TicketListPage) 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 *TicketListPage) ActiveTicketId() string {
return p.GetSelectedTicketId()
}
func (p *TicketListPage) GetSelectedTicketId() string {
return findTicketIdInString(p.cachedResults[p.selectedLine])
}
func (p *TicketListPage) SelectItem() {
if len(p.cachedResults) == 0 {
return
}
q := new(TicketShowPage)
q.TicketId = p.GetSelectedTicketId()
currentPage = q
q.Create()
changePage()
}
func (p *TicketListPage) GoBack() {
currentPage = ticketQueryPage
changePage()
}
func (p *TicketListPage) EditTicket() {
runJiraCmdEdit(p.GetSelectedTicketId())
}
func (p *TicketListPage) Update() {
ls := p.uiList
log.Debugf("TicketListPage.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 *TicketListPage) Refresh() {
pDeref := &p
q := *pDeref
q.cachedResults = make([]string, 0)
ticketListPage = q
changePage()
q.Create()
}
func (p *TicketListPage) Create() {
log.Debugf("TicketListPage.Create(): self: %s (%p)", p.Id(), p)
log.Debugf("TicketListPage.Create(): currentPage: %s (%p)", currentPage.Id(), currentPage)
ui.Clear()
ls := ui.NewList()
p.uiList = ls
if p.statusBar == nil {
p.statusBar = new(StatusBar)
}
if p.commandBar == nil {
p.commandBar = commandBar
}
query := p.ActiveQuery.JQL
if sort := p.ActiveSort.JQL; sort != "" {
re := regexp.MustCompile(`(?i)\s+ORDER\s+BY.+$`)
query = re.ReplaceAllString(query, ``) + " " + sort
}
if len(p.cachedResults) == 0 {
p.cachedResults = JiraQueryAsStrings(query, p.ActiveQuery.Template)
}
if p.selectedLine >= len(p.cachedResults) {
p.selectedLine = len(p.cachedResults) - 1
}
p.displayLines = make([]string, len(p.cachedResults))
ls.ItemFgColor = ui.ColorYellow
ls.BorderLabel = fmt.Sprintf("%s: %s", p.ActiveQuery.Name, p.ActiveQuery.JQL)
ls.Height = ui.TermHeight() - 2
ls.Width = ui.TermWidth()
ls.Y = 0
p.statusBar.Create()
p.commandBar.Create()
p.Update()
}