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

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. 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: h Action: Show the previous project.
Key: l Action: Show the next project. Key: l Action: Show the next project.
Key: ← Action: Show the previous project. Key: j Action: Select the next review in the list.
Key: → Action: Show the next project.</description> Key: k Action: Select the previous review in the list.</description>
</item> </item>
<item> <item>

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 /> <p><span class="caption">Key:</span> <code>l</code> <br />
<span class="caption">Action:</span> Show the next project.</p> <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 /> <p><span class="caption">Key:</span> <code></code> <br />
<span class="caption">Action:</span> Show the previous project.</p> <span class="caption">Action:</span> Show the previous project.</p>
<p><span class="caption">Key:</span> <code></code> <br /> <p><span class="caption">Key:</span> <code></code> <br />
<span class="caption">Action:</span> Show the next project.</p> <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> <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"> <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>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>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>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>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>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>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>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="w"> </span>-<span class="w"> </span>org/test-project<span class="s2">&#34;
</span><span class="s2"> - dotfiles </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="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> </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> <h3 id="attributes">Attributes</h3>
<p><code>enabled</code> <br /> <p><code>colors.rows.even</code> <br />
Determines whether or not this module is executed and if its data displayed onscreen. <br /> Define the foreground color for even-numbered rows. <br />
Values: <code>true</code>, <code>false</code>.</p> Values: Any <a href="https://en.wikipedia.org/wiki/X11_color_names">X11
color name</a>.</p>
<p><code>position</code> <br /> <p><code>colors.rows.odd</code> <br />
Defines where in the grid this module&rsquo;s widget will be displayed. <br /></p> Define the foreground color for odd-numbered rows. <br />
Values: Any <a href="https://en.wikipedia.org/wiki/X11_color_names">X11
<p><code>refreshInterval</code> <br /> color name</a>.</p>
How often, in seconds, this module will update its data. <br />
Values: A positive integer, <code>0..n</code>.</p>
<p><code>domain</code> <br /> <p><code>domain</code> <br />
Your Gerrit corporate domain. <br /> Your Gerrit corporate domain. <br />
Values: A valid URI.</p> Values: A valid URI.</p>
<p><code>projects</code> <br /> <p><code>enabled</code> <br />
A list of Gerrit project names to fetch data for. <br /></p> 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 /> <p><code>password</code> <br />
Value: Your <a href="https://gerrit-review.googlesource.com/Documentation/user-upload.html#http">Gerrit HTTP Password</a>.</p> 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 /> <p><code>username</code> <br />
Your Gerrit username.</p> Your Gerrit username.</p>

View File

@ -14,7 +14,7 @@ func (widget *Widget) display() {
return 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 := wtf.SigilStr(len(widget.GerritProjects), widget.Idx, widget.View) + "\n"
str = str + " [red]Stats[white]\n" str = str + " [red]Stats[white]\n"
@ -29,31 +29,27 @@ func (widget *Widget) display() {
widget.View.SetText(str) widget.View.SetText(str)
} }
func (widget *Widget) displayMyOutgoingReviews(project *GerritProject, username string) string { func (widget *Widget) displayMyIncomingReviews(project *GerritProject, username string) string {
ors := project.myOutgoingReviews(username) if len(project.IncomingReviews) == 0 {
if len(ors) == 0 {
return " [grey]none[white]\n" return " [grey]none[white]\n"
} }
str := "" str := ""
for _, r := range ors { for idx, r := range project.IncomingReviews {
str = str + fmt.Sprintf(" [green]%4s[white] %s\n", r.ChangeID, r.Subject) str = str + fmt.Sprintf(" [%s] [green]%d[white] [%s] %s\n", widget.rowColor(idx), r.Number, widget.rowColor(idx), r.Subject)
} }
return str return str
} }
func (widget *Widget) displayMyIncomingReviews(project *GerritProject, username string) string { func (widget *Widget) displayMyOutgoingReviews(project *GerritProject, username string) string {
irs := project.myIncomingReviews(username) if len(project.OutgoingReviews) == 0 {
if len(irs) == 0 {
return " [grey]none[white]\n" return " [grey]none[white]\n"
} }
str := "" str := ""
for _, r := range irs { for idx, r := range project.OutgoingReviews {
str = str + fmt.Sprintf(" [green]%4s[white] %s\n", r.ChangeID, r.Subject) 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 return str
@ -62,12 +58,22 @@ func (widget *Widget) displayMyIncomingReviews(project *GerritProject, username
func (widget *Widget) displayStats(project *GerritProject) string { func (widget *Widget) displayStats(project *GerritProject) string {
str := fmt.Sprintf( str := fmt.Sprintf(
" Reviews: %d\n", " Reviews: %d\n",
project.ReviewCount(), project.ReviewCount,
) )
return str 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 { func (widget *Widget) title(project *GerritProject) string {
return fmt.Sprintf("[green]%s [white]", project.Path) return fmt.Sprintf("[green]%s [white]", project.Path)
} }

View File

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

View File

@ -17,12 +17,18 @@ const HelpText = `
Keyboard commands for Gerrit: Keyboard commands for Gerrit:
/: Show/hide this help window /: Show/hide this help window
h: Previous project h: Show the previous project
l: Next 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 r: Refresh the data
arrow left: Previous project arrow left: Show the previous project
arrow right: Next 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 { type Widget struct {
@ -33,6 +39,7 @@ type Widget struct {
GerritProjects []*GerritProject GerritProjects []*GerritProject
Idx int Idx int
selected int
} }
var ( 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.GerritProjects = widget.buildProjectCollection(wtf.Config.UList("wtf.mods.gerrit.projects"))
widget.View.SetInputCapture(widget.keyboardIntercept) widget.View.SetInputCapture(widget.keyboardIntercept)
widget.unselect()
return &widget return &widget
} }
@ -100,25 +108,65 @@ func (widget *Widget) Refresh() {
widget.display() widget.display()
} }
func (widget *Widget) Next() { /* -------------------- Unexported Functions -------------------- */
func (widget *Widget) nextProject() {
widget.Idx = widget.Idx + 1 widget.Idx = widget.Idx + 1
widget.unselect()
if widget.Idx == len(widget.GerritProjects) { if widget.Idx == len(widget.GerritProjects) {
widget.Idx = 0 widget.Idx = 0
} }
widget.display() widget.unselect()
} }
func (widget *Widget) Prev() { func (widget *Widget) prevProject() {
widget.Idx = widget.Idx - 1 widget.Idx = widget.Idx - 1
if widget.Idx < 0 { if widget.Idx < 0 {
widget.Idx = len(widget.GerritProjects) - 1 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() 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 { func (widget *Widget) buildProjectCollection(projectData []interface{}) []*GerritProject {
gerritProjects := []*GerritProject{} gerritProjects := []*GerritProject{}
@ -149,10 +197,16 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
widget.ShowHelp() widget.ShowHelp()
return nil return nil
case "h": case "h":
widget.Prev() widget.prevProject()
return nil return nil
case "l": case "l":
widget.Next() widget.nextProject()
return nil
case "j":
widget.nextReview()
return nil
case "k":
widget.prevReview()
return nil return nil
case "r": case "r":
widget.Refresh() widget.Refresh()
@ -161,11 +215,23 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() { switch event.Key() {
case tcell.KeyLeft: case tcell.KeyLeft:
widget.Prev() widget.prevProject()
return nil return nil
case tcell.KeyRight: case tcell.KeyRight:
widget.Next() widget.nextProject()
return nil 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: default:
return event return event
} }