Skip to content

Commit

Permalink
[toru] this was a fun one 🎉 added spinners and pretty progress bars l…
Browse files Browse the repository at this point in the history
…ol :3
  • Loading branch information
sweetbbak committed Mar 2, 2024
1 parent e2b376a commit c5f6134
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 40 deletions.
20 changes: 6 additions & 14 deletions cmd/toru/download.go
Expand Up @@ -2,9 +2,8 @@ package main

import (
"fmt"
"time"

"github.com/dustin/go-humanize"
"github.com/pterm/pterm"
"github.com/sweetbbak/toru/pkg/libtorrent"
)

Expand All @@ -19,24 +18,17 @@ func DownloadTorrent(cl *libtorrent.Client) error {
return fmt.Errorf("download: missing argument (magnet|torrent|url) OR --torrent flag")
}

success, _ := pterm.DefaultSpinner.Start("getting torrent info")

t, err := cl.AddTorrent(tfile)
if err != nil {
return err
}

success.Success("Success!")

go func() {
name := t.Name()
fmt.Printf("Downloading: '%s'\n", name)
for !t.Complete.Bool() {
c := t.BytesCompleted()
total := t.Length()
s := humanize.Bytes(uint64(c))
x := humanize.Bytes(uint64(total))
numpeers := len(t.PeerConns())
fmt.Printf("\x1b[2K\rDownloaded (%v/%v) from [%v] Peers...", s, x, numpeers)
time.Sleep(time.Millisecond * 500)
}
println("Complete")
Progress(t)
}()

t.DownloadAll()
Expand Down
52 changes: 43 additions & 9 deletions cmd/toru/stream.go
Expand Up @@ -3,35 +3,69 @@ package main
import (
"fmt"
"os"
"strings"
"time"

"github.com/dustin/go-humanize"
"github.com/anacrolix/torrent"
"github.com/pterm/pterm"
"github.com/sweetbbak/toru/pkg/libtorrent"
"github.com/sweetbbak/toru/pkg/player"
"github.com/sweetbbak/toru/pkg/search"
)

func TruncateString(s string, length int) string {
var l int
var sb strings.Builder

// early return if string is shorter then requested length
if length >= len(s) {
return s
}

for _, r := range s {
if l <= length {
sb.WriteRune(r)
} else {

}
l++
}
return sb.String()
}

func Progress(t *torrent.Torrent) {
title := TruncateString(t.Name(), 33)
fmt.Println(title)

p, _ := pterm.DefaultProgressbar.WithTotal(100).Start()

for !t.Complete.Bool() {
pc := float64(t.BytesCompleted()) / float64(t.Length()) * 100
numpeers := len(t.PeerConns())
p.Increment().Current = int(pc)
p.UpdateTitle(fmt.Sprintf("peers [%v]", numpeers))
time.Sleep(time.Millisecond * 50)
}
}

// takes any type of torrent file/url/magnet, adds it to the client and streams it
func StreamTorrent(torfile string, cl *libtorrent.Client) (string, error) {
success, _ := pterm.DefaultSpinner.Start("getting torrent info")

t, err := cl.AddTorrent(torfile)
if err != nil {
return "", err
}

success.Success("Success!")

link := cl.ServeTorrent(t)

// consider deleting this as it sometimes conflicts with the fzf user interface
go func() {
for !t.Complete.Bool() {
c := t.BytesCompleted()
total := t.Length()
s := humanize.Bytes(uint64(c))
x := humanize.Bytes(uint64(total))
numpeers := len(t.PeerConns())
fmt.Printf("\x1b[2K\rDownloaded (%v/%v) from [%v] Peers...", s, x, numpeers)
time.Sleep(time.Millisecond * 500)
Progress(t)
}
println("Complete")
}()

fmt.Println(link)
Expand Down
7 changes: 7 additions & 0 deletions go.mod
Expand Up @@ -12,11 +12,15 @@ require (
github.com/jessevdk/go-flags v1.5.0
github.com/ktr0731/go-fuzzyfinder v0.8.0
github.com/pkg/errors v0.9.1
github.com/pterm/pterm v0.12.79
golang.org/x/net v0.21.0
golang.org/x/sys v0.17.0
)

require (
atomicgo.dev/cursor v0.2.0 // indirect
atomicgo.dev/keyboard v0.2.9 // indirect
atomicgo.dev/schedule v0.1.0 // indirect
github.com/RoaringBitmap/roaring v1.9.0 // indirect
github.com/ajwerner/btree v0.0.0-20211221152037-f427b3e689c0 // indirect
github.com/alecthomas/atomic v0.1.0-alpha2 // indirect
Expand Down Expand Up @@ -57,9 +61,11 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/ktr0731/go-ansisgr v0.1.0 // indirect
github.com/lithammer/fuzzysearch v1.1.8 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
Expand Down Expand Up @@ -93,6 +99,7 @@ require (
github.com/rs/dnscache v0.0.0-20230804202142-fc85eb664529 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/tidwall/btree v1.7.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
go.etcd.io/bbolt v1.3.9 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
Expand Down

0 comments on commit c5f6134

Please sign in to comment.