-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
162 lines (152 loc) · 4.86 KB
/
main.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
package main
import (
"fmt"
"log"
"time"
"github.com/Simon-Busch/go_github_assistant/github"
"github.com/Simon-Busch/go_github_assistant/ui"
"github.com/Simon-Busch/go_github_assistant/utils"
termui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
)
func main() {
ghUserName, ghToken, err := utils.InitEnv()
if err != nil {
panic("please provide a .env file with GITHUB_USERNAME and GITHUB_TOKEN")
}
issuesResponse, err := github.FetchIssues(ghUserName, ghToken)
if err != nil {
log.Fatalf("Error fetching issues: %v", err)
}
prToReview, err := github.FetchReviewRequests(ghUserName, ghToken)
if err != nil {
log.Fatalf("Error fetching PRs: %v", err)
}
openIssues := issuesResponse.GetAllOpenedIssues()
closedIssues := issuesResponse.GetAllClosedIssues()
var (
selectedIndex = 0
showComments = false
commentsText = ""
showHelper = false
showClosedIssues = false
showPr = false
help *widgets.Paragraph
issuesList *widgets.List
currentIssues []github.IssueOrPR
)
if err := termui.Init(); err != nil {
log.Fatal(err)
}
defer termui.Close()
ui.RenderWaitingScreen(len(openIssues), len(closedIssues), ghUserName, prToReview.TotalCount)
time.Sleep(1 * time.Second)
termui.Clear()
actionsTabs := ui.RenderHeader(ghUserName)
issueDetails := ui.RenderIssueDetails()
footer := ui.RenderFooter(len(openIssues), len(closedIssues), ghUserName, prToReview.TotalCount)
if showClosedIssues {
currentIssues = closedIssues
issuesList = ui.RenderIssues(closedIssues)
ui.UpdateIssueDetails(currentIssues,selectedIndex, showComments, commentsText, issueDetails, issuesList)
} else {
currentIssues = openIssues
issuesList = ui.RenderIssues(openIssues)
ui.UpdateIssueDetails(currentIssues,selectedIndex, showComments, commentsText, issueDetails, issuesList)
}
termui.Render(actionsTabs, issuesList, issueDetails, footer)
uiEvents := termui.PollEvents()
for {
e := <-uiEvents
switch e.ID {
case "q":// Quit on 'q'
return
case "<Down>":
if selectedIndex < len(currentIssues)-1 {
selectedIndex++
issuesList.ScrollDown()
ui.UpdateIssueDetails(currentIssues, selectedIndex, showComments, commentsText, issueDetails, issuesList)
}
if selectedIndex == len(currentIssues)-1 {
selectedIndex = 0
issuesList.ScrollTop()
ui.UpdateIssueDetails(currentIssues, selectedIndex, showComments, commentsText, issueDetails, issuesList)
}
commentsText = ""
showComments = false
case "<Up>":
if selectedIndex > 0 {
selectedIndex--
issuesList.ScrollUp()
ui.UpdateIssueDetails(currentIssues, selectedIndex, showComments, commentsText, issueDetails, issuesList)
}
if selectedIndex == 0 {
selectedIndex = len(currentIssues) - 1
issuesList.ScrollBottom()
ui.UpdateIssueDetails(currentIssues, selectedIndex, showComments, commentsText, issueDetails, issuesList)
}
commentsText = ""
showComments = false
case "<Enter>":
issue := currentIssues[selectedIndex]
err := ui.OpenBrowser(issue.URL)
if err != nil {
log.Printf("Failed to open browser: %v", err)
}
case "h":
showHelper = !showHelper
if showHelper {
help = ui.RenderHelp()
} else {
help = nil // Clear the help when toggling off
}
case "<C-o>":
showClosedIssues = !showClosedIssues
if showClosedIssues {
currentIssues = closedIssues
issuesList = ui.RenderIssues(closedIssues)
} else {
currentIssues = openIssues
issuesList = ui.RenderIssues(openIssues)
}
selectedIndex = 0
ui.UpdateIssueDetails(currentIssues, selectedIndex, showComments, commentsText, issueDetails, issuesList)
termui.Render(actionsTabs, issuesList, issueDetails, footer)
case "<C-p>":
showPr = !showPr
if showPr {
currentIssues = prToReview.Items
issuesList = ui.RenderIssues(prToReview.Items)
} else {
currentIssues = openIssues
issuesList = ui.RenderIssues(openIssues)
}
selectedIndex = 0
ui.UpdateIssueDetails(currentIssues, selectedIndex, showComments, commentsText, issueDetails, issuesList)
termui.Render(actionsTabs, issuesList, issueDetails, footer)
case "<C-c>":
issue := currentIssues[selectedIndex]
if !showComments {
comments, err := github.FetchComments(issue.CommentsURL, ghUserName, ghToken)
if err != nil {
log.Printf("Error fetching comments for issue %s: %v", issue.URL, err)
commentsText = "Error fetching comments."
} else {
commentsText = ""
for _, comment := range comments {
if (comment.User.Login != "vercel[bot]") {
commentsText += fmt.Sprintf("Comment by %s at %s:\n%s\n\n",
comment.User.Login, comment.CreatedAt, comment.Body)
}
}
}
}
showComments = !showComments
ui.UpdateIssueDetails(currentIssues, selectedIndex, showComments, commentsText, issueDetails, issuesList)
}
termui.Render(issuesList, issueDetails)
if showHelper && help != nil {
termui.Render(help)
}
}
}