mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Merge branch 'master' into splituplogger
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package wtf
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/olebedev/config"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
@@ -177,6 +179,17 @@ func (tracker *FocusTracker) focusables() []Wtfable {
|
||||
}
|
||||
}
|
||||
|
||||
// Sort for deterministic ordering
|
||||
sort.SliceStable(focusable[:], func(i, j int) bool {
|
||||
if focusable[i].Top() < focusable[j].Top() {
|
||||
return true
|
||||
}
|
||||
if focusable[i].Top() == focusable[j].Top() {
|
||||
return focusable[i].Left() < focusable[j].Left()
|
||||
}
|
||||
return false
|
||||
})
|
||||
|
||||
return focusable
|
||||
}
|
||||
|
||||
|
||||
79
wtf/scrollable.go
Normal file
79
wtf/scrollable.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package wtf
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/cfg"
|
||||
)
|
||||
|
||||
type ScrollableWidget struct {
|
||||
TextWidget
|
||||
|
||||
selected int
|
||||
maxItems int
|
||||
RenderFunction func()
|
||||
}
|
||||
|
||||
func NewScrollableWidget(app *tview.Application, commonSettings *cfg.Common, focusable bool) ScrollableWidget {
|
||||
|
||||
widget := ScrollableWidget{
|
||||
TextWidget: NewTextWidget(app, commonSettings, focusable),
|
||||
}
|
||||
|
||||
widget.Unselect()
|
||||
widget.View.SetScrollable(true)
|
||||
widget.View.SetRegions(true)
|
||||
return widget
|
||||
}
|
||||
|
||||
func (widget *ScrollableWidget) SetRenderFunction(displayFunc func()) {
|
||||
widget.RenderFunction = displayFunc
|
||||
}
|
||||
|
||||
func (widget *ScrollableWidget) SetItemCount(items int) {
|
||||
widget.maxItems = items
|
||||
}
|
||||
|
||||
func (widget *ScrollableWidget) GetSelected() int {
|
||||
return widget.selected
|
||||
}
|
||||
|
||||
func (widget *ScrollableWidget) RowColor(idx int) string {
|
||||
if widget.View.HasFocus() && (idx == widget.selected) {
|
||||
return widget.CommonSettings.DefaultFocussedRowColor()
|
||||
}
|
||||
|
||||
return widget.CommonSettings.RowColor(idx)
|
||||
}
|
||||
|
||||
func (widget *ScrollableWidget) Next() {
|
||||
widget.selected++
|
||||
if widget.selected >= widget.maxItems {
|
||||
widget.selected = 0
|
||||
}
|
||||
widget.RenderFunction()
|
||||
}
|
||||
|
||||
func (widget *ScrollableWidget) Prev() {
|
||||
widget.selected--
|
||||
if widget.selected < 0 {
|
||||
widget.selected = widget.maxItems - 1
|
||||
}
|
||||
widget.RenderFunction()
|
||||
}
|
||||
|
||||
func (widget *ScrollableWidget) Unselect() {
|
||||
widget.selected = -1
|
||||
if widget.RenderFunction != nil {
|
||||
widget.RenderFunction()
|
||||
}
|
||||
}
|
||||
|
||||
func (widget *ScrollableWidget) Redraw(title, content string, wrap bool) {
|
||||
|
||||
widget.TextWidget.Redraw(title, content, wrap)
|
||||
widget.app.QueueUpdateDraw(func() {
|
||||
widget.View.Highlight(strconv.Itoa(widget.selected)).ScrollToHighlight()
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user