1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Make the gerrit module interactive

* Add ability to select and open reviews
* Add more keyboard shortcuts
* Fix issue - focus shortcut letter wasn't displayed on the title area
* Cleanup some code
This commit is contained in:
Anand Sudhir Prayaga 2018-08-02 17:13:06 +02:00
parent 9154441c32
commit 70eb603449
7 changed files with 224 additions and 86 deletions

View File

@ -33,55 +33,87 @@ wtf/gerrit/
<span class="caption">Key:</span> `l` <br />
<span class="caption">Action:</span> Show the next project.
<span class="caption">Key:</span> `j` <br />
<span class="caption">Action:</span> Select the next review in the list.
<span class="caption">Key:</span> `k` <br />
<span class="caption">Action:</span> Select the previous review in the list.
<span class="caption">Key:</span> `r` <br />
<span class="caption">Action:</span> Refresh the data.
<span class="caption">Key:</span> `←` <br />
<span class="caption">Action:</span> Show the previous project.
<span class="caption">Key:</span> `→` <br />
<span class="caption">Action:</span> Show the next project.
<span class="caption">Key:</span> `↓` <br />
<span class="caption">Action:</span> Select the next review in the list.
<span class="caption">Key:</span> `↑` <br />
<span class="caption">Action:</span> Select the previous review in the list.
<span class="caption">Key:</span> `[return]` <br />
<span class="caption">Action:</span> Open the selected review in the browser.
## Configuration
```yaml
gerrit:
colors:
rows:
even: "lightblue"
odd: "white"
domain: https://gerrit-review.googlesource.com
enabled: true
password: "mypassword"
position:
top: 2
left: 3
height: 2
width: 2
refreshInterval: 300
domain: https://gerrit-review.googlesource.com
projects:
- org/test-project"
- dotfiles
password: "mypassword"
refreshInterval: 300
username: "myname"
verifyServerCertificate: false
```
### Attributes
`enabled` <br />
Determines whether or not this module is executed and if its data displayed onscreen. <br />
Values: `true`, `false`.
`colors.rows.even` <br />
Define the foreground color for even-numbered rows. <br />
Values: Any <a href="https://en.wikipedia.org/wiki/X11_color_names">X11
color name</a>.
`position` <br />
Defines where in the grid this module's widget will be displayed. <br />
`refreshInterval` <br />
How often, in seconds, this module will update its data. <br />
Values: A positive integer, `0..n`.
`colors.rows.odd` <br />
Define the foreground color for odd-numbered rows. <br />
Values: Any <a href="https://en.wikipedia.org/wiki/X11_color_names">X11
color name</a>.
`domain` <br />
Your Gerrit corporate domain. <br />
Values: A valid URI.
`projects` <br />
A list of Gerrit project names to fetch data for. <br />
`enabled` <br />
Determines whether or not this module is executed and if its data displayed onscreen. <br />
Values: `true`, `false`.
`password` <br />
Value: Your <a href="https://gerrit-review.googlesource.com/Documentation/user-upload.html#http">Gerrit HTTP Password</a>.
`position` <br />
Defines where in the grid this module's widget will be displayed. <br />
`projects` <br />
A list of Gerrit project names to fetch data for. <br />
`refreshInterval` <br />
How often, in seconds, this module will update its data. <br />
Values: A positive integer, `0..n`.
`username` <br />
Your Gerrit username.

View File

@ -85,8 +85,8 @@ My Outgoing Reviews All open reviews created by you.
Source Code wtf/gerrit/ Keyboard Commands Key: / Action: Open/close the widget&amp;rsquo;s help window.
Key: h Action: Show the previous project.
Key: l Action: Show the next project.
Key: ← Action: Show the previous project.
Key: → Action: Show the next project.</description>
Key: j Action: Select the next review in the list.
Key: k Action: Select the previous review in the list.</description>
</item>
<item>
@ -550,4 +550,4 @@ Example Configuration Files A couple of example config files are provided in the
</item>
</channel>
</rss>
</rss>

View File

@ -85,8 +85,8 @@ My Outgoing Reviews All open reviews created by you.
Source Code wtf/gerrit/ Keyboard Commands Key: / Action: Open/close the widget&amp;rsquo;s help window.
Key: h Action: Show the previous project.
Key: l Action: Show the next project.
Key: ← Action: Show the previous project.
Key: → Action: Show the next project.</description>
Key: j Action: Select the next review in the list.
Key: k Action: Select the previous review in the list.</description>
</item>
<item>
@ -550,4 +550,4 @@ Example Configuration Files A couple of example config files are provided in the
</item>
</channel>
</rss>
</rss>

View File

@ -162,51 +162,83 @@ height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<p><span class="caption">Key:</span> <code>l</code> <br />
<span class="caption">Action:</span> Show the next project.</p>
<p><span class="caption">Key:</span> <code>j</code> <br />
<span class="caption">Action:</span> Select the next review in the list.</p>
<p><span class="caption">Key:</span> <code>k</code> <br />
<span class="caption">Action:</span> Select the previous review in the list.</p>
<p><span class="caption">Key:</span> <code>r</code> <br />
<span class="caption">Action:</span> Refresh the data.</p>
<p><span class="caption">Key:</span> <code></code> <br />
<span class="caption">Action:</span> Show the previous project.</p>
<p><span class="caption">Key:</span> <code></code> <br />
<span class="caption">Action:</span> Show the next project.</p>
<p><span class="caption">Key:</span> <code></code> <br />
<span class="caption">Action:</span> Select the next review in the list.</p>
<p><span class="caption">Key:</span> <code></code> <br />
<span class="caption">Action:</span> Select the previous review in the list.</p>
<p><span class="caption">Key:</span> <code>[return]</code> <br />
<span class="caption">Action:</span> Open the selected review in the browser.</p>
<h2 id="configuration">Configuration</h2>
<div class="highlight"><pre class="chroma"><code class="language-yaml" data-lang="yaml">gerrit<span class="p">:</span><span class="w">
</span><span class="w"> </span>colors<span class="p">:</span><span class="w">
</span><span class="w"> </span>rows<span class="p">:</span><span class="w">
</span><span class="w"> </span>even<span class="p">:</span><span class="w"> </span><span class="s2">&#34;lightblue&#34;</span><span class="w">
</span><span class="w"> </span>odd<span class="p">:</span><span class="w"> </span><span class="s2">&#34;white&#34;</span><span class="w">
</span><span class="w"> </span>domain<span class="p">:</span><span class="w"> </span>https<span class="p">:</span>//gerrit-review.googlesource.com<span class="w">
</span><span class="w"> </span>enabled<span class="p">:</span><span class="w"> </span><span class="kc">true</span><span class="w">
</span><span class="w"> </span>password<span class="p">:</span><span class="w"> </span><span class="s2">&#34;mypassword&#34;</span><span class="w">
</span><span class="w"> </span>position<span class="p">:</span><span class="w">
</span><span class="w"> </span>top<span class="p">:</span><span class="w"> </span><span class="m">2</span><span class="w">
</span><span class="w"> </span>left<span class="p">:</span><span class="w"> </span><span class="m">3</span><span class="w">
</span><span class="w"> </span>height<span class="p">:</span><span class="w"> </span><span class="m">2</span><span class="w">
</span><span class="w"> </span>width<span class="p">:</span><span class="w"> </span><span class="m">2</span><span class="w">
</span><span class="w"> </span>refreshInterval<span class="p">:</span><span class="w"> </span><span class="m">300</span><span class="w">
</span><span class="w"> </span>domain<span class="p">:</span><span class="w"> </span>https<span class="p">:</span>//gerrit-review.googlesource.com<span class="w">
</span><span class="w"> </span>projects<span class="p">:</span><span class="w">
</span><span class="w"> </span>-<span class="w"> </span>org/test-project<span class="s2">&#34;
</span><span class="s2"> - dotfiles
</span><span class="s2"> password: &#34;</span>mypassword<span class="s2">&#34;
</span><span class="s2"> refreshInterval: 300
</span><span class="s2"> username: &#34;</span>myname&#34;<span class="w">
</span><span class="w"> </span>verifyServerCertificate<span class="p">:</span><span class="w"> </span><span class="kc">false</span></code></pre></div>
<h3 id="attributes">Attributes</h3>
<p><code>enabled</code> <br />
Determines whether or not this module is executed and if its data displayed onscreen. <br />
Values: <code>true</code>, <code>false</code>.</p>
<p><code>colors.rows.even</code> <br />
Define the foreground color for even-numbered rows. <br />
Values: Any <a href="https://en.wikipedia.org/wiki/X11_color_names">X11
color name</a>.</p>
<p><code>position</code> <br />
Defines where in the grid this module&rsquo;s widget will be displayed. <br /></p>
<p><code>refreshInterval</code> <br />
How often, in seconds, this module will update its data. <br />
Values: A positive integer, <code>0..n</code>.</p>
<p><code>colors.rows.odd</code> <br />
Define the foreground color for odd-numbered rows. <br />
Values: Any <a href="https://en.wikipedia.org/wiki/X11_color_names">X11
color name</a>.</p>
<p><code>domain</code> <br />
Your Gerrit corporate domain. <br />
Values: A valid URI.</p>
<p><code>projects</code> <br />
A list of Gerrit project names to fetch data for. <br /></p>
<p><code>enabled</code> <br />
Determines whether or not this module is executed and if its data displayed onscreen. <br />
Values: <code>true</code>, <code>false</code>.</p>
<p><code>password</code> <br />
Value: Your <a href="https://gerrit-review.googlesource.com/Documentation/user-upload.html#http">Gerrit HTTP Password</a>.</p>
<p><code>position</code> <br />
Defines where in the grid this module&rsquo;s widget will be displayed. <br /></p>
<p><code>projects</code> <br />
A list of Gerrit project names to fetch data for. <br /></p>
<p><code>refreshInterval</code> <br />
How often, in seconds, this module will update its data. <br />
Values: A positive integer, <code>0..n</code>.</p>
<p><code>username</code> <br />
Your Gerrit username.</p>

View File

@ -14,7 +14,7 @@ func (widget *Widget) display() {
return
}
widget.View.SetTitle(fmt.Sprintf("%s- %s", widget.Name, widget.title(project)))
widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("%s- %s", widget.Name, widget.title(project))))
str := wtf.SigilStr(len(widget.GerritProjects), widget.Idx, widget.View) + "\n"
str = str + " [red]Stats[white]\n"
@ -29,31 +29,27 @@ func (widget *Widget) display() {
widget.View.SetText(str)
}
func (widget *Widget) displayMyOutgoingReviews(project *GerritProject, username string) string {
ors := project.myOutgoingReviews(username)
if len(ors) == 0 {
func (widget *Widget) displayMyIncomingReviews(project *GerritProject, username string) string {
if len(project.IncomingReviews) == 0 {
return " [grey]none[white]\n"
}
str := ""
for _, r := range ors {
str = str + fmt.Sprintf(" [green]%4s[white] %s\n", r.ChangeID, r.Subject)
for idx, r := range project.IncomingReviews {
str = str + fmt.Sprintf(" [%s] [green]%d[white] [%s] %s\n", widget.rowColor(idx), r.Number, widget.rowColor(idx), r.Subject)
}
return str
}
func (widget *Widget) displayMyIncomingReviews(project *GerritProject, username string) string {
irs := project.myIncomingReviews(username)
if len(irs) == 0 {
func (widget *Widget) displayMyOutgoingReviews(project *GerritProject, username string) string {
if len(project.OutgoingReviews) == 0 {
return " [grey]none[white]\n"
}
str := ""
for _, r := range irs {
str = str + fmt.Sprintf(" [green]%4s[white] %s\n", r.ChangeID, r.Subject)
for idx, r := range project.OutgoingReviews {
str = str + fmt.Sprintf(" [%s] [green]%d[white] [%s] %s\n", widget.rowColor(idx+len(project.IncomingReviews)), r.Number, widget.rowColor(idx+len(project.IncomingReviews)), r.Subject)
}
return str
@ -62,12 +58,22 @@ func (widget *Widget) displayMyIncomingReviews(project *GerritProject, username
func (widget *Widget) displayStats(project *GerritProject) string {
str := fmt.Sprintf(
" Reviews: %d\n",
project.ReviewCount(),
project.ReviewCount,
)
return str
}
func (widget *Widget) rowColor(index int) string {
if widget.View.HasFocus() && (index == widget.selected) {
foreColor := wtf.Config.UString("wtf.colors.highlight.fore", "black")
backColor := wtf.Config.UString("wtf.colors.highlight.back", "orange")
return fmt.Sprintf("%s:%s", foreColor, backColor)
}
return wtf.RowColor("gerrit", index)
}
func (widget *Widget) title(project *GerritProject) string {
return fmt.Sprintf("[green]%s [white]", project.Path)
}

View File

@ -2,13 +2,17 @@ package gerrit
import (
glb "github.com/andygrunwald/go-gerrit"
"github.com/senorprogrammer/wtf/wtf"
)
type GerritProject struct {
gerrit *glb.Client
Path string
Changes *[]glb.ChangeInfo
Changes *[]glb.ChangeInfo
ReviewCount int
IncomingReviews []glb.ChangeInfo
OutgoingReviews []glb.ChangeInfo
}
func NewGerritProject(path string, gerrit *glb.Client) *GerritProject {
@ -22,67 +26,65 @@ func NewGerritProject(path string, gerrit *glb.Client) *GerritProject {
// Refresh reloads the gerrit data via the Gerrit API
func (project *GerritProject) Refresh() {
username := wtf.Config.UString("wtf.mods.gerrit.username")
project.Changes, _ = project.loadChanges()
project.ReviewCount = project.countReviews(project.Changes)
project.IncomingReviews = project.myIncomingReviews(project.Changes, username)
project.OutgoingReviews = project.myOutgoingReviews(project.Changes, username)
}
/* -------------------- Counts -------------------- */
func (project *GerritProject) IssueCount() int {
if project.Changes == nil {
func (project *GerritProject) countReviews(changes *[]glb.ChangeInfo) int {
if changes == nil {
return 0
}
return len(*project.Changes)
}
func (project *GerritProject) ReviewCount() int {
if project.Changes == nil {
return 0
}
return len(*project.Changes)
return len(*changes)
}
/* -------------------- Unexported Functions -------------------- */
// myOutgoingReviews returns a list of my outgoing reviews created by username on this project
func (project *GerritProject) myOutgoingReviews(username string) []glb.ChangeInfo {
changes := []glb.ChangeInfo{}
func (project *GerritProject) myOutgoingReviews(changes *[]glb.ChangeInfo, username string) []glb.ChangeInfo {
var ors []glb.ChangeInfo
if project.Changes == nil {
return changes
if changes == nil {
return ors
}
for _, change := range *project.Changes {
for _, change := range *changes {
user := change.Owner
if user.Username == username {
changes = append(changes, change)
ors = append(ors, change)
}
}
return changes
return ors
}
// myIncomingReviews returns a list of merge requests for which username has been requested to ChangeInfo
func (project *GerritProject) myIncomingReviews(username string) []glb.ChangeInfo {
changes := []glb.ChangeInfo{}
func (project *GerritProject) myIncomingReviews(changes *[]glb.ChangeInfo, username string) []glb.ChangeInfo {
var irs []glb.ChangeInfo
if project.Changes == nil {
return changes
if changes == nil {
return irs
}
for _, change := range *project.Changes {
for _, change := range *changes {
reviewers := change.Reviewers
for _, reviewer := range reviewers["REVIEWER"] {
if reviewer.Username == username {
changes = append(changes, change)
irs = append(irs, change)
}
}
}
return changes
return irs
}
func (project *GerritProject) loadChanges() (*[]glb.ChangeInfo, error) {

View File

@ -17,12 +17,18 @@ const HelpText = `
Keyboard commands for Gerrit:
/: Show/hide this help window
h: Previous project
l: Next project
h: Show the previous project
l: Show the next project
j: Select the next review in the list
k: Select the previous review in the list
r: Refresh the data
arrow left: Previous project
arrow right: Next project
arrow left: Show the previous project
arrow right: Show the next project
arrow down: Select the next review in the list
arrow up: Select the previous review in the list
return: Open the selected review in a browser
`
type Widget struct {
@ -33,6 +39,7 @@ type Widget struct {
GerritProjects []*GerritProject
Idx int
selected int
}
var (
@ -85,6 +92,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
widget.GerritProjects = widget.buildProjectCollection(wtf.Config.UList("wtf.mods.gerrit.projects"))
widget.View.SetInputCapture(widget.keyboardIntercept)
widget.unselect()
return &widget
}
@ -100,25 +108,65 @@ func (widget *Widget) Refresh() {
widget.display()
}
func (widget *Widget) Next() {
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) nextProject() {
widget.Idx = widget.Idx + 1
widget.unselect()
if widget.Idx == len(widget.GerritProjects) {
widget.Idx = 0
}
widget.display()
widget.unselect()
}
func (widget *Widget) Prev() {
func (widget *Widget) prevProject() {
widget.Idx = widget.Idx - 1
if widget.Idx < 0 {
widget.Idx = len(widget.GerritProjects) - 1
}
widget.unselect()
}
func (widget *Widget) nextReview() {
widget.selected++
project := widget.GerritProjects[widget.Idx]
if widget.selected >= project.ReviewCount {
widget.selected = 0
}
widget.display()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) prevReview() {
widget.selected--
project := widget.GerritProjects[widget.Idx]
if widget.selected < 0 {
widget.selected = project.ReviewCount - 1
}
widget.display()
}
func (widget *Widget) openReview() {
sel := widget.selected
project := widget.GerritProjects[widget.Idx]
if sel >= 0 && sel < project.ReviewCount {
change := glb.ChangeInfo{}
if sel < len(project.IncomingReviews) {
change = project.IncomingReviews[sel]
} else {
change = project.OutgoingReviews[sel-len(project.IncomingReviews)]
}
wtf.OpenFile(fmt.Sprintf("%s/%s/%d", wtf.Config.UString("wtf.mods.gerrit.domain"), "#/c", change.Number))
}
}
func (widget *Widget) unselect() {
widget.selected = -1
widget.display()
}
func (widget *Widget) buildProjectCollection(projectData []interface{}) []*GerritProject {
gerritProjects := []*GerritProject{}
@ -149,10 +197,16 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
widget.ShowHelp()
return nil
case "h":
widget.Prev()
widget.prevProject()
return nil
case "l":
widget.Next()
widget.nextProject()
return nil
case "j":
widget.nextReview()
return nil
case "k":
widget.prevReview()
return nil
case "r":
widget.Refresh()
@ -161,11 +215,23 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyLeft:
widget.Prev()
widget.prevProject()
return nil
case tcell.KeyRight:
widget.Next()
widget.nextProject()
return nil
case tcell.KeyDown:
widget.nextReview()
return nil
case tcell.KeyUp:
widget.prevReview()
return nil
case tcell.KeyEnter:
widget.openReview()
return nil
case tcell.KeyEsc:
widget.unselect()
return event
default:
return event
}