mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Create abtract scrollable widget
This cleans up a bunch of boilerplate for scrollable items and standardizes their usage
This commit is contained in:
@@ -4,13 +4,13 @@ import "github.com/gdamore/tcell"
|
||||
|
||||
func (widget *Widget) initializeKeyboardControls() {
|
||||
widget.SetKeyboardChar("/", widget.ShowHelp)
|
||||
widget.SetKeyboardChar("j", widget.next)
|
||||
widget.SetKeyboardChar("k", widget.prev)
|
||||
widget.SetKeyboardChar("j", widget.Next)
|
||||
widget.SetKeyboardChar("k", widget.Prev)
|
||||
widget.SetKeyboardChar("o", widget.openJob)
|
||||
widget.SetKeyboardChar("r", widget.Refresh)
|
||||
|
||||
widget.SetKeyboardKey(tcell.KeyDown, widget.next)
|
||||
widget.SetKeyboardKey(tcell.KeyDown, widget.Next)
|
||||
widget.SetKeyboardKey(tcell.KeyEnter, widget.openJob)
|
||||
widget.SetKeyboardKey(tcell.KeyEsc, widget.unselect)
|
||||
widget.SetKeyboardKey(tcell.KeyUp, widget.prev)
|
||||
widget.SetKeyboardKey(tcell.KeyEsc, widget.Unselect)
|
||||
widget.SetKeyboardKey(tcell.KeyUp, widget.Prev)
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@ package jenkins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"regexp"
|
||||
|
||||
"github.com/rivo/tview"
|
||||
@@ -27,33 +25,25 @@ const HelpText = `
|
||||
type Widget struct {
|
||||
wtf.HelpfulWidget
|
||||
wtf.KeyboardWidget
|
||||
wtf.TextWidget
|
||||
wtf.ScrollableWidget
|
||||
|
||||
app *tview.Application
|
||||
|
||||
selected int
|
||||
settings *Settings
|
||||
view *View
|
||||
}
|
||||
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
KeyboardWidget: wtf.NewKeyboardWidget(),
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
KeyboardWidget: wtf.NewKeyboardWidget(),
|
||||
ScrollableWidget: wtf.NewScrollableWidget(app, settings.common, true),
|
||||
|
||||
app: app,
|
||||
settings: settings,
|
||||
}
|
||||
|
||||
widget.SetRenderFunction(widget.Render)
|
||||
widget.initializeKeyboardControls()
|
||||
widget.View.SetInputCapture(widget.InputCapture)
|
||||
|
||||
widget.unselect()
|
||||
|
||||
widget.View.SetScrollable(true)
|
||||
widget.View.SetRegions(true)
|
||||
|
||||
widget.HelpfulWidget.SetView(widget.View)
|
||||
|
||||
return &widget
|
||||
@@ -78,12 +68,14 @@ func (widget *Widget) Refresh() {
|
||||
return
|
||||
}
|
||||
|
||||
widget.display()
|
||||
widget.SetItemCount(len(widget.view.Jobs))
|
||||
|
||||
widget.Render()
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) display() {
|
||||
func (widget *Widget) Render() {
|
||||
if widget.view == nil {
|
||||
return
|
||||
}
|
||||
@@ -91,9 +83,6 @@ func (widget *Widget) display() {
|
||||
title := fmt.Sprintf("%s: [red]%s", widget.CommonSettings.Title, widget.view.Name)
|
||||
|
||||
widget.Redraw(title, widget.contentFrom(widget.view), false)
|
||||
widget.app.QueueUpdateDraw(func() {
|
||||
widget.View.Highlight(strconv.Itoa(widget.selected)).ScrollToHighlight()
|
||||
})
|
||||
}
|
||||
|
||||
func (widget *Widget) contentFrom(view *View) string {
|
||||
@@ -105,7 +94,7 @@ func (widget *Widget) contentFrom(view *View) string {
|
||||
str = str + fmt.Sprintf(
|
||||
`["%d"][%s] [%s]%-6s[white][""]`,
|
||||
idx,
|
||||
widget.rowColor(idx),
|
||||
widget.RowColor(idx),
|
||||
widget.jobColor(&job),
|
||||
job.Name,
|
||||
)
|
||||
@@ -117,14 +106,6 @@ func (widget *Widget) contentFrom(view *View) string {
|
||||
return str
|
||||
}
|
||||
|
||||
func (widget *Widget) rowColor(idx int) string {
|
||||
if widget.View.HasFocus() && (idx == widget.selected) {
|
||||
return widget.settings.common.DefaultFocussedRowColor()
|
||||
}
|
||||
|
||||
return widget.settings.common.RowColor(idx)
|
||||
}
|
||||
|
||||
func (widget *Widget) jobColor(job *Job) string {
|
||||
switch job.Color {
|
||||
case "blue":
|
||||
@@ -137,33 +118,10 @@ func (widget *Widget) jobColor(job *Job) string {
|
||||
}
|
||||
}
|
||||
|
||||
func (widget *Widget) next() {
|
||||
widget.selected++
|
||||
if widget.view != nil && widget.selected >= len(widget.view.Jobs) {
|
||||
widget.selected = 0
|
||||
}
|
||||
|
||||
widget.display()
|
||||
}
|
||||
|
||||
func (widget *Widget) prev() {
|
||||
widget.selected--
|
||||
if widget.selected < 0 && widget.view != nil {
|
||||
widget.selected = len(widget.view.Jobs) - 1
|
||||
}
|
||||
|
||||
widget.display()
|
||||
}
|
||||
|
||||
func (widget *Widget) openJob() {
|
||||
sel := widget.selected
|
||||
sel := widget.GetSelected()
|
||||
if sel >= 0 && widget.view != nil && sel < len(widget.view.Jobs) {
|
||||
job := &widget.view.Jobs[widget.selected]
|
||||
job := &widget.view.Jobs[sel]
|
||||
wtf.OpenFile(job.Url)
|
||||
}
|
||||
}
|
||||
|
||||
func (widget *Widget) unselect() {
|
||||
widget.selected = -1
|
||||
widget.display()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user