github.com/wtfutil/wtf@v0.43.0/modules/transmission/display.go (about)

     1  package transmission
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hekmon/transmissionrpc/v2"
     8  	"github.com/rivo/tview"
     9  	"github.com/wtfutil/wtf/utils"
    10  )
    11  
    12  func (widget *Widget) display() {
    13  	widget.ScrollableWidget.Redraw(widget.content)
    14  }
    15  
    16  func (widget *Widget) content() (string, string, bool) {
    17  	widget.mu.Lock()
    18  	defer widget.mu.Unlock()
    19  
    20  	title := widget.CommonSettings().Title
    21  	if widget.err != nil {
    22  		return title, widget.err.Error(), true
    23  	}
    24  
    25  	if len(widget.torrents) == 0 {
    26  		return title, "No data", false
    27  	}
    28  
    29  	str := ""
    30  
    31  	for idx, torrent := range widget.torrents {
    32  		torrName := *torrent.Name
    33  
    34  		row := fmt.Sprintf(
    35  			"[%s] %s %s %s%s[white]",
    36  			widget.RowColor(idx),
    37  			widget.torrentPercentDone(torrent),
    38  			widget.torrentSeedRatio(torrent),
    39  			widget.torrentState(torrent),
    40  			tview.Escape(widget.prettyTorrentName(torrName)),
    41  		)
    42  
    43  		str += utils.HighlightableHelper(widget.View, row, idx, len(torrName))
    44  	}
    45  
    46  	return title, str, false
    47  }
    48  
    49  func (widget *Widget) prettyTorrentName(name string) string {
    50  	str := strings.ReplaceAll(name, "[", "(")
    51  	str = strings.ReplaceAll(str, "]", ")")
    52  
    53  	return str
    54  }
    55  
    56  func (widget *Widget) torrentPercentDone(torrent transmissionrpc.Torrent) string {
    57  	pctDone := *torrent.PercentDone
    58  	str := fmt.Sprintf("%3d%%↓", int(pctDone*100))
    59  
    60  	switch pctDone {
    61  	case 0.0:
    62  		str = "[gray::b]" + str
    63  	case 1.0:
    64  		str = "[green::b]" + str
    65  	default:
    66  		str = "[lightblue::b]" + str
    67  	}
    68  
    69  	return str + "[white]"
    70  }
    71  
    72  func (widget *Widget) torrentSeedRatio(torrent transmissionrpc.Torrent) string {
    73  	seedRatio := *torrent.UploadRatio
    74  
    75  	if seedRatio < 0 {
    76  		seedRatio = 0
    77  	}
    78  
    79  	return fmt.Sprintf("[green]%3d%%↑", int(seedRatio*100))
    80  }
    81  
    82  func (widget *Widget) torrentState(torrent transmissionrpc.Torrent) string {
    83  	str := ""
    84  
    85  	switch *torrent.Status {
    86  	case transmissionrpc.TorrentStatusStopped:
    87  		str += "[gray]"
    88  	case transmissionrpc.TorrentStatusDownload:
    89  		str += "[lightblue]"
    90  	case transmissionrpc.TorrentStatusSeed:
    91  		str += "[green]"
    92  	}
    93  
    94  	return str
    95  }