From 3ebb4798dcf386f9676420f74f141da4685e6e1e Mon Sep 17 00:00:00 2001 From: Tony Blyler Date: Fri, 13 May 2016 19:03:55 -0400 Subject: [PATCH] Add pretty byte size output to download log --- queue/queue.go | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/queue/queue.go b/queue/queue.go index 7ea786a..e4b88cb 100644 --- a/queue/queue.go +++ b/queue/queue.go @@ -11,6 +11,7 @@ import ( "log" "os" "path/filepath" + "strconv" "strings" "sync" "time" @@ -49,6 +50,42 @@ type Queue struct { lock sync.RWMutex } +var prettyBytesValues = []int64{ + 1024, + 1024 * 1024, + 1024 * 1024 * 1024, + 1024 * 1024 * 1024 * 1024, + 1024 * 1024 * 1024 * 1024 * 1024, + 1024 * 1024 * 1024 * 1024 * 1024 * 1024, +} + +var prettyBytesNames = []string{ + "KiB", + "MiB", + "GiB", + "TiB", + "PiB", + "EiB", +} + +func prettyBytes(bytes int64) string { + output := strconv.FormatInt(bytes, 10) + "B" + for i, divisor := range prettyBytesValues { + newBytes := bytes / divisor + if newBytes > 1024 { + continue + } + + if newBytes < 1 { + break + } + + output = strconv.FormatInt(newBytes, 10) + prettyBytesNames[i] + } + + return output +} + // NewQueue establishes all connections and watchers func NewQueue(config *Config, logger *log.Logger) (*Queue, error) { if config.WatchDownloadPaths == nil || len(config.WatchDownloadPaths) == 0 { @@ -235,7 +272,7 @@ func (q *Queue) downloadTorrents(torrents []rtorrent.Torrent) { } go func(torrentFilePath string, downloadPath string, torrent rtorrent.Torrent) { - q.logger.Printf("Downloading '%s' (%s) to '%s' (%s)", torrent.Name, torrentFilePath, downloadPath, destDownloadPath) + q.logger.Printf("Downloading '%s' (%s) to '%s' (%s) %s", torrent.Name, torrentFilePath, downloadPath, destDownloadPath, prettyBytes(int64(torrent.Size))) err := q.sftpClient.Mirror(torrent.Path, downloadPath) if err != nil { q.logger.Printf("Failed to download '%s' to '%s' error '%s'", torrent.Path, downloadPath, err)