UPDATE: Update howto, fix bug bytes humman format
This commit is contained in:
@@ -13,16 +13,34 @@ import (
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
)
|
||||
|
||||
func HumanFormat(bytes float64) string {
|
||||
for _, unit := range []string{"", "Ki", "Mi", "Gi"} {
|
||||
if math.Abs(bytes) < 1024.0 {
|
||||
return fmt.Sprintf("%3.1f%sB", bytes, unit)
|
||||
}
|
||||
bytes /= 1024.0
|
||||
if math.IsNaN(bytes) || math.IsInf(bytes, 0) {
|
||||
return fmt.Sprintf("%v", bytes)
|
||||
}
|
||||
return fmt.Sprintf("%.1fTiB", bytes)
|
||||
if bytes == 0 {
|
||||
return "0B"
|
||||
}
|
||||
|
||||
neg := bytes < 0
|
||||
if neg {
|
||||
bytes = -bytes
|
||||
}
|
||||
|
||||
units := []string{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}
|
||||
i := 0
|
||||
for bytes >= 1024 && i < len(units)-1 {
|
||||
bytes /= 1024
|
||||
i++
|
||||
}
|
||||
|
||||
res := fmt.Sprintf("%.1f%s", bytes, units[i])
|
||||
if neg {
|
||||
res = "-" + res
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
type WriteCounter struct {
|
||||
@@ -53,14 +71,13 @@ func (wc *WriteCounter) PrintProgress() {
|
||||
if elapsed < 0.001 {
|
||||
elapsed = 0.001
|
||||
}
|
||||
speed := float64(wc.Total) / 1024 / 1024 / elapsed
|
||||
speed := float64(wc.Total) / elapsed
|
||||
percent := float64(wc.Total) / float64(wc.TotalSize) * 100
|
||||
if wc.OnEmit != nil {
|
||||
wc.OnEmit(percent, fmt.Sprintf("%s/s", HumanFormat(speed)))
|
||||
}
|
||||
}
|
||||
|
||||
// --- DownloadFileParallel ---
|
||||
func (g *GitService) downloadFileParallel(filePath, url string, numParts int, onEmit func(percent float64, speed string)) (tmpPath string, err error) {
|
||||
resp, err := http.Head(url)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user