feat: integration torrent (#30)

This commit is contained in:
monkeyWie authored and GitHub committed 2022-08-11 14:38:16 +08:00
1 parent 43378c7dc8
commit 034137322f
20 files changed
+925 -139

No files matched your search

+1 -1
View File
@@ -20,7 +20,7 @@ func parse() *args {
}
var args args
args.connections = flag.Int("C", 16, "Concurrent connections.")
args.dir = flag.String("D", dir, "Save directory.")
args.dir = flag.String("D", dir, "Store directory.")
flag.Parse()
t := flag.Args()
if len(t) > 0 {
+24 -10
View File
@@ -5,7 +5,7 @@ import (
"github.com/monkeyWie/gopeed-core/pkg/base"
"github.com/monkeyWie/gopeed-core/pkg/download"
"github.com/monkeyWie/gopeed-core/pkg/util"
"path/filepath"
"strings"
"sync"
)
@@ -31,9 +31,9 @@ func main() {
printProgress(event.Task, title)
fmt.Println()
if event.Err != nil {
gPrint("reason: " + event.Err.Error())
fmt.Printf("reason: %s", event.Err.Error())
} else {
gPrint("saving file " + filepath.Join(*args.dir, event.Task.Res.Files[0].Name))
fmt.Printf("saving path: %s", *args.dir)
}
wg.Done()
}
@@ -49,18 +49,32 @@ func main() {
wg.Wait()
}
func printProgress(task *download.TaskInfo, title string) {
rate := float64(task.Progress.Downloaded) / float64(task.Res.TotalSize)
var (
lastLineLen = 0
sb = new(strings.Builder)
)
func printProgress(task *download.Task, title string) {
rate := float64(task.Progress.Downloaded) / float64(task.Res.Length)
completeWidth := int(progressWidth * rate)
speed := util.ByteFmt(task.Progress.Speed)
totalSize := util.ByteFmt(task.Res.TotalSize)
fmt.Printf("\r%s [", title)
totalSize := util.ByteFmt(task.Res.Length)
sb.WriteString(fmt.Sprintf("\r%s [", title))
for i := 0; i < progressWidth; i++ {
if i < completeWidth {
fmt.Print("■")
sb.WriteString("■")
} else {
fmt.Print("□")
sb.WriteString("□")
}
}
fmt.Printf("] %.1f%% %s/s %s", rate*100, speed, totalSize)
sb.WriteString(fmt.Sprintf("] %.1f%% %s/s %s", rate*100, speed, totalSize))
if lastLineLen != 0 {
paddingLen := lastLineLen - sb.Len()
if paddingLen > 0 {
sb.WriteString(strings.Repeat(" ", paddingLen))
}
}
lastLineLen = sb.Len()
fmt.Print(sb.String())
sb.Reset()
}