mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Add pause/unpause and delete functionality to Transmisson widget
This commit is contained in:
parent
5dbce09e4f
commit
3a65dad743
77
modules/transmission/display.go
Normal file
77
modules/transmission/display.go
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
package transmission
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/hekmon/transmissionrpc"
|
||||||
|
"github.com/rivo/tview"
|
||||||
|
"github.com/wtfutil/wtf/wtf"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (widget *Widget) contentFrom(data []*transmissionrpc.Torrent) string {
|
||||||
|
str := ""
|
||||||
|
|
||||||
|
for idx, torrent := range data {
|
||||||
|
torrName := *torrent.Name
|
||||||
|
|
||||||
|
row := fmt.Sprintf(
|
||||||
|
"[%s] %s %s%s[white]",
|
||||||
|
widget.RowColor(idx),
|
||||||
|
widget.torrentPercentDone(torrent),
|
||||||
|
widget.torrentState(torrent),
|
||||||
|
tview.Escape(widget.prettyTorrentName(torrName)),
|
||||||
|
)
|
||||||
|
|
||||||
|
str += wtf.HighlightableHelper(widget.View, row, idx, len(torrName))
|
||||||
|
}
|
||||||
|
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
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) prettyTorrentName(name string) string {
|
||||||
|
str := strings.Replace(name, "[", "(", -1)
|
||||||
|
str = strings.Replace(str, "]", ")", -1)
|
||||||
|
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
func (widget *Widget) torrentPercentDone(torrent *transmissionrpc.Torrent) string {
|
||||||
|
pctDone := *torrent.PercentDone
|
||||||
|
str := fmt.Sprintf("%3d", int(pctDone*100))
|
||||||
|
|
||||||
|
if pctDone == 0.0 {
|
||||||
|
str = "[gray::b]" + str
|
||||||
|
} else if pctDone == 1.0 {
|
||||||
|
str = "[green::b]" + str
|
||||||
|
} else {
|
||||||
|
str = "[lightblue::b]" + str
|
||||||
|
}
|
||||||
|
|
||||||
|
return str + "[white]"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (widget *Widget) torrentState(torrent *transmissionrpc.Torrent) string {
|
||||||
|
str := ""
|
||||||
|
|
||||||
|
switch *torrent.Status {
|
||||||
|
case transmissionrpc.TorrentStatusStopped:
|
||||||
|
str += "[gray]"
|
||||||
|
case transmissionrpc.TorrentStatusDownload:
|
||||||
|
str += "[lightblue]"
|
||||||
|
case transmissionrpc.TorrentStatusSeed:
|
||||||
|
str += "[green]"
|
||||||
|
}
|
||||||
|
|
||||||
|
return str
|
||||||
|
}
|
@ -6,12 +6,11 @@ func (widget *Widget) initializeKeyboardControls() {
|
|||||||
widget.SetKeyboardChar("/", widget.ShowHelp, "Show/hide this help prompt")
|
widget.SetKeyboardChar("/", widget.ShowHelp, "Show/hide this help prompt")
|
||||||
widget.SetKeyboardChar("j", widget.Prev, "Select previous item")
|
widget.SetKeyboardChar("j", widget.Prev, "Select previous item")
|
||||||
widget.SetKeyboardChar("k", widget.Next, "Select next 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("r", nil, "Remove torrent from list")
|
|
||||||
widget.SetKeyboardChar("u", widget.Unselect, "Clear selection")
|
widget.SetKeyboardChar("u", widget.Unselect, "Clear selection")
|
||||||
|
|
||||||
|
widget.SetKeyboardKey(tcell.KeyCtrlD, widget.deleteSelectedTorrent, "Delete selected torrent")
|
||||||
widget.SetKeyboardKey(tcell.KeyDown, widget.Next, "Select next item")
|
widget.SetKeyboardKey(tcell.KeyDown, widget.Next, "Select next item")
|
||||||
|
widget.SetKeyboardKey(tcell.KeyEnter, widget.pauseUnpauseTorrent, "Pause/unpause torrent")
|
||||||
widget.SetKeyboardKey(tcell.KeyEsc, widget.Unselect, "Clear selection")
|
widget.SetKeyboardKey(tcell.KeyEsc, widget.Unselect, "Clear selection")
|
||||||
widget.SetKeyboardKey(tcell.KeyUp, widget.Prev, "Select previous item")
|
widget.SetKeyboardKey(tcell.KeyUp, widget.Prev, "Select previous item")
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
package transmission
|
package transmission
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"errors"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/hekmon/transmissionrpc"
|
"github.com/hekmon/transmissionrpc"
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
@ -14,6 +13,7 @@ type Widget struct {
|
|||||||
wtf.KeyboardWidget
|
wtf.KeyboardWidget
|
||||||
wtf.ScrollableWidget
|
wtf.ScrollableWidget
|
||||||
|
|
||||||
|
client *transmissionrpc.Client
|
||||||
settings *Settings
|
settings *Settings
|
||||||
torrents []*transmissionrpc.Torrent
|
torrents []*transmissionrpc.Torrent
|
||||||
}
|
}
|
||||||
@ -33,6 +33,13 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
|||||||
|
|
||||||
widget.KeyboardWidget.SetView(widget.View)
|
widget.KeyboardWidget.SetView(widget.View)
|
||||||
|
|
||||||
|
// Create a persisten transmission client for use in the calls below
|
||||||
|
client, err := transmissionrpc.New(widget.settings.host, widget.settings.username, widget.settings.password, nil)
|
||||||
|
if err != nil {
|
||||||
|
client = nil
|
||||||
|
}
|
||||||
|
widget.client = client
|
||||||
|
|
||||||
return &widget
|
return &widget
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,12 +47,11 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
|||||||
|
|
||||||
// Fetch retrieves torrent data from the Transmission daemon
|
// Fetch retrieves torrent data from the Transmission daemon
|
||||||
func (widget *Widget) Fetch() ([]*transmissionrpc.Torrent, error) {
|
func (widget *Widget) Fetch() ([]*transmissionrpc.Torrent, error) {
|
||||||
transmissionbt, err := transmissionrpc.New(widget.settings.host, widget.settings.username, widget.settings.password, nil)
|
if widget.client == nil {
|
||||||
if err != nil {
|
return nil, errors.New("client could not be initialized")
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
torrents, err := transmissionbt.TorrentGetAll()
|
torrents, err := widget.client.TorrentGetAll()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -91,69 +97,54 @@ func (widget *Widget) Unselect() {
|
|||||||
|
|
||||||
/* -------------------- Unexported Functions -------------------- */
|
/* -------------------- Unexported Functions -------------------- */
|
||||||
|
|
||||||
func (widget *Widget) display() {
|
func (widget *Widget) currentTorrent() *transmissionrpc.Torrent {
|
||||||
if len(widget.torrents) == 0 {
|
if len(widget.torrents) == 0 {
|
||||||
widget.ScrollableWidget.Redraw(widget.CommonSettings.Title, "no torrents", false)
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return widget.torrents[widget.Selected]
|
||||||
|
}
|
||||||
|
|
||||||
|
// deleteSelected removes the selected torrent from transmission
|
||||||
|
// This action is non-destructive, it does not delete the files on the host
|
||||||
|
func (widget *Widget) deleteSelectedTorrent() {
|
||||||
|
if widget.client == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
content := widget.contentFrom(widget.torrents)
|
currTorrent := widget.currentTorrent()
|
||||||
widget.ScrollableWidget.Redraw(widget.CommonSettings.Title, content, false)
|
if currTorrent == nil {
|
||||||
}
|
return
|
||||||
|
|
||||||
func (widget *Widget) contentFrom(data []*transmissionrpc.Torrent) string {
|
|
||||||
str := ""
|
|
||||||
|
|
||||||
for idx, torrent := range data {
|
|
||||||
torrName := *torrent.Name
|
|
||||||
|
|
||||||
row := fmt.Sprintf(
|
|
||||||
"[%s] %s %s%s[white]",
|
|
||||||
widget.RowColor(idx),
|
|
||||||
widget.torrentPercentDone(torrent),
|
|
||||||
widget.torrentState(torrent),
|
|
||||||
tview.Escape(widget.prettyTorrentName(torrName)),
|
|
||||||
)
|
|
||||||
|
|
||||||
str += wtf.HighlightableHelper(widget.View, row, idx, len(torrName))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return str
|
ids := []int64{*currTorrent.ID}
|
||||||
|
|
||||||
|
removePayload := &transmissionrpc.TorrentRemovePayload{
|
||||||
|
IDs: ids,
|
||||||
|
DeleteLocalData: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
widget.client.TorrentRemove(removePayload)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (widget *Widget) prettyTorrentName(name string) string {
|
// pauseUnpauseTorrent either pauses or unpauses the downloading and seeding of the selected torrent
|
||||||
str := strings.Replace(name, "[", "(", -1)
|
func (widget *Widget) pauseUnpauseTorrent() {
|
||||||
str = strings.Replace(str, "]", ")", -1)
|
if widget.client == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
return str
|
currTorrent := widget.currentTorrent()
|
||||||
}
|
if currTorrent == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (widget *Widget) torrentPercentDone(torrent *transmissionrpc.Torrent) string {
|
ids := []int64{*currTorrent.ID}
|
||||||
pctDone := *torrent.PercentDone
|
|
||||||
str := fmt.Sprintf("%3d", int(pctDone*100))
|
|
||||||
|
|
||||||
if pctDone == 0.0 {
|
if *currTorrent.Status == transmissionrpc.TorrentStatusStopped {
|
||||||
str = "[gray::b]" + str
|
widget.client.TorrentStartIDs(ids)
|
||||||
} else if pctDone == 1.0 {
|
|
||||||
str = "[green::b]" + str
|
|
||||||
} else {
|
} else {
|
||||||
str = "[lightblue::b]" + str
|
widget.client.TorrentStopIDs(ids)
|
||||||
}
|
}
|
||||||
|
|
||||||
return str + "[white]"
|
widget.display()
|
||||||
}
|
|
||||||
|
|
||||||
func (widget *Widget) torrentState(torrent *transmissionrpc.Torrent) string {
|
|
||||||
str := ""
|
|
||||||
|
|
||||||
switch *torrent.Status {
|
|
||||||
case transmissionrpc.TorrentStatusStopped:
|
|
||||||
str += "[gray]"
|
|
||||||
case transmissionrpc.TorrentStatusDownload:
|
|
||||||
str += "[lightblue]"
|
|
||||||
case transmissionrpc.TorrentStatusSeed:
|
|
||||||
str += "[green]"
|
|
||||||
}
|
|
||||||
|
|
||||||
return str
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user