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

+10
View File
@@ -12,6 +12,16 @@ const (
DownloadStatusDone
)
type EventKey string
const (
EventKeyStart = "start"
EventKeyPause = "pause"
EventKeyProgress = "progress"
EventKeyError = "error"
EventKeyDone = "done"
)
const (
HttpCodeOK = 200
HttpCodePartialContent = 206
+12
View File
@@ -142,3 +142,15 @@ func (d *DialerWarp) Dial(network, addr string) (c net.Conn, err error) {
}
return &ConnWarp{conn: conn}, nil
}
// 获取任务中各个文件的已下载字节数
type Progress []int64
// TotalDownloaded 获取任务总下载字节数
func (p Progress) TotalDownloaded() int64 {
total := int64(0)
for _, d := range p {
total += d
}
return total
}
+2
View File
@@ -11,6 +11,8 @@ type Fetcher interface {
Pause() (err error)
Continue() (err error)
// 获取任务下载进度
Progress() Progress
// 该方法会一直阻塞,直到任务下载结束
Wait() (err error)
}
+25
View File
@@ -0,0 +1,25 @@
package base
import "time"
// 计时器
type Timer struct {
t int64
used int64
}
func (t *Timer) Start() {
t.t = time.Now().UnixNano()
}
func (t *Timer) Pause() {
t.used += time.Now().UnixNano() - t.t
}
func (t *Timer) Continue() {
t.t = time.Now().UnixNano()
}
func (t *Timer) Used() int64 {
return (time.Now().UnixNano() - t.t) + t.used
}