feat: 命令行工具

This commit is contained in:
liwei committed 2021-03-23 19:03:46 +08:00
1 parent f150ec23c4
commit 046729bfaa
10 files changed
+195 -31

No files matched your search

+41 -1
View File
@@ -1,19 +1,39 @@
package main
import (
"fmt"
"github.com/monkeyWie/gopeed-core/download"
"github.com/monkeyWie/gopeed-core/download/base"
"github.com/monkeyWie/gopeed-core/download/http"
"github.com/superhawk610/bar"
"github.com/ttacon/chalk"
"strconv"
"sync"
)
func main() {
b := bar.NewWithOpts(
bar.WithDimensions(10000, 20),
bar.WithDisplay("[", "■", "■", "□", "]"),
bar.WithFormat(
fmt.Sprintf(
" %s:status%s :percent :bar%s :speedKB/s%s ",
chalk.Green,
chalk.Reset,
chalk.Blue,
chalk.Reset,
),
),
)
downloader := download.NewDownloader(http.FetcherBuilder)
res, err := downloader.Resolve(&base.Request{
URL: "http://192.168.200.163:8088/docker-compose",
URL: "https://dldir1.qq.com/weixin/Windows/WeChatSetup.exe",
})
if err != nil {
panic(err)
}
err = downloader.Create(res, &base.Options{
Path: "E:\\test\\gopeed\\http",
Connections: 4,
@@ -21,4 +41,24 @@ func main() {
if err != nil {
panic(err)
}
var wg sync.WaitGroup
wg.Add(1)
downloader.Listener(func(taskInfo *download.TaskInfo, eventKey base.EventKey) {
if eventKey == base.EventKeyProgress {
b.Update(int(10000*(float64(taskInfo.Progress.Downloaded)/float64(taskInfo.Res.TotalSize))), bar.Context{
bar.Ctx("status", "downloading..."),
bar.Ctx("speed", strconv.Itoa(int(taskInfo.Progress.Speed/1024))),
})
}
if eventKey == base.EventKeyDone {
b.Update(10000, bar.Context{
bar.Ctx("status", "complete"),
bar.Ctx("speed", strconv.Itoa(int(taskInfo.Progress.Speed/1024))),
})
wg.Done()
}
})
wg.Wait()
b.Done()
}