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

Make Transmission widget scrollable

This commit is contained in:
Chris Cummer 2019-06-26 06:57:32 -07:00
parent 9244d60abe
commit 5dbce09e4f
4 changed files with 64 additions and 22 deletions

View File

@ -17,15 +17,15 @@ func (widget *Widget) display() {
title := fmt.Sprintf("[green]%s[white]", proj.Project.Name) title := fmt.Sprintf("[green]%s[white]", proj.Project.Name)
str := "" str := ""
for index, item := range proj.tasks { for idx, item := range proj.tasks {
row := fmt.Sprintf( row := fmt.Sprintf(
`[%s]| | %s[%s]`, `[%s]| | %s[%s]`,
widget.RowColor(index), widget.RowColor(idx),
tview.Escape(item.Content), tview.Escape(item.Content),
widget.RowColor(index), widget.RowColor(idx),
) )
str += wtf.HighlightableHelper(widget.View, row, index, len(item.Content)) str += wtf.HighlightableHelper(widget.View, row, idx, len(item.Content))
} }
widget.ScrollableWidget.Redraw(title, str, false) widget.ScrollableWidget.Redraw(title, str, false)

View File

@ -1,10 +1,17 @@
package transmission package transmission
// import "github.com/gdamore/tcell" import "github.com/gdamore/tcell"
func (widget *Widget) initializeKeyboardControls() { func (widget *Widget) initializeKeyboardControls() {
widget.SetKeyboardChar("/", widget.ShowHelp, "Show/hide this help prompt") widget.SetKeyboardChar("/", widget.ShowHelp, "Show/hide this help prompt")
widget.SetKeyboardChar("m", nil, "Add new magnet torrent") widget.SetKeyboardChar("j", widget.Prev, "Select previous item")
widget.SetKeyboardChar("k", widget.Next, "Select next item")
widget.SetKeyboardChar("n", nil, "Add new magnet URL")
widget.SetKeyboardChar("p", nil, "Pause torrent") widget.SetKeyboardChar("p", nil, "Pause torrent")
widget.SetKeyboardChar("r", nil, "Remove torrent from list") widget.SetKeyboardChar("r", nil, "Remove torrent from list")
widget.SetKeyboardChar("u", widget.Unselect, "Clear selection")
widget.SetKeyboardKey(tcell.KeyDown, widget.Next, "Select next item")
widget.SetKeyboardKey(tcell.KeyEsc, widget.Unselect, "Clear selection")
widget.SetKeyboardKey(tcell.KeyUp, widget.Prev, "Select previous item")
} }

View File

@ -12,25 +12,28 @@ import (
// Widget is the container for transmission data // Widget is the container for transmission data
type Widget struct { type Widget struct {
wtf.KeyboardWidget wtf.KeyboardWidget
wtf.TextWidget wtf.ScrollableWidget
settings *Settings settings *Settings
torrents []*transmissionrpc.Torrent
} }
// NewWidget creates a new instance of a widget // NewWidget creates a new instance of a widget
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget { func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
widget := &Widget{ widget := Widget{
KeyboardWidget: wtf.NewKeyboardWidget(app, pages, settings.common), KeyboardWidget: wtf.NewKeyboardWidget(app, pages, settings.common),
TextWidget: wtf.NewTextWidget(app, settings.common, true), ScrollableWidget: wtf.NewScrollableWidget(app, settings.common, true),
settings: settings, settings: settings,
} }
widget.SetRenderFunction(widget.display)
widget.initializeKeyboardControls() widget.initializeKeyboardControls()
widget.View.SetInputCapture(widget.InputCapture) widget.View.SetInputCapture(widget.InputCapture)
widget.KeyboardWidget.SetView(widget.View) widget.KeyboardWidget.SetView(widget.View)
return widget return &widget
} }
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
@ -53,15 +56,16 @@ func (widget *Widget) Fetch() ([]*transmissionrpc.Torrent, error) {
// Refresh updates the data for this widget and displays it onscreen // Refresh updates the data for this widget and displays it onscreen
func (widget *Widget) Refresh() { func (widget *Widget) Refresh() {
torrents, err := widget.Fetch() torrents, err := widget.Fetch()
var content string
if err != nil { if err != nil {
content = err.Error() widget.SetItemCount(0)
} else { widget.ScrollableWidget.Redraw(widget.CommonSettings.Title, err.Error(), false)
content = widget.contentFrom(torrents) return
} }
widget.Redraw(widget.CommonSettings.Title, content, false) widget.torrents = torrents
widget.SetItemCount(len(torrents))
widget.display()
} }
// HelpText returns the help text for this widget // HelpText returns the help text for this widget
@ -69,18 +73,49 @@ func (widget *Widget) HelpText() string {
return widget.KeyboardWidget.HelpText() return widget.KeyboardWidget.HelpText()
} }
// Next selects the next item in the list
func (widget *Widget) Next() {
widget.ScrollableWidget.Next()
}
// Prev selects the previous item in the list
func (widget *Widget) Prev() {
widget.ScrollableWidget.Prev()
}
// Unselect clears the selection of list items
func (widget *Widget) Unselect() {
widget.ScrollableWidget.Unselect()
widget.RenderFunction()
}
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */
func (widget *Widget) display() {
if len(widget.torrents) == 0 {
widget.ScrollableWidget.Redraw(widget.CommonSettings.Title, "no torrents", false)
return
}
content := widget.contentFrom(widget.torrents)
widget.ScrollableWidget.Redraw(widget.CommonSettings.Title, content, false)
}
func (widget *Widget) contentFrom(data []*transmissionrpc.Torrent) string { func (widget *Widget) contentFrom(data []*transmissionrpc.Torrent) string {
str := "" str := ""
for _, torrent := range data { for idx, torrent := range data {
str += fmt.Sprintf( torrName := *torrent.Name
" %s %s%s[white]\n",
row := fmt.Sprintf(
"[%s] %s %s%s[white]",
widget.RowColor(idx),
widget.torrentPercentDone(torrent), widget.torrentPercentDone(torrent),
widget.torrentState(torrent), widget.torrentState(torrent),
widget.prettyTorrentName(*torrent.Name), tview.Escape(widget.prettyTorrentName(torrName)),
) )
str += wtf.HighlightableHelper(widget.View, row, idx, len(torrName))
} }
return str return str

View File

@ -24,6 +24,7 @@ func NewScrollableWidget(app *tview.Application, commonSettings *cfg.Common, foc
widget.Unselect() widget.Unselect()
widget.View.SetScrollable(true) widget.View.SetScrollable(true)
widget.View.SetRegions(true) widget.View.SetRegions(true)
return widget return widget
} }
@ -71,7 +72,6 @@ func (widget *ScrollableWidget) Unselect() {
} }
func (widget *ScrollableWidget) Redraw(title, content string, wrap bool) { func (widget *ScrollableWidget) Redraw(title, content string, wrap bool) {
widget.TextWidget.Redraw(title, content, wrap) widget.TextWidget.Redraw(title, content, wrap)
widget.app.QueueUpdateDraw(func() { widget.app.QueueUpdateDraw(func() {
widget.View.Highlight(strconv.Itoa(widget.Selected)).ScrollToHighlight() widget.View.Highlight(strconv.Itoa(widget.Selected)).ScrollToHighlight()