-
Notifications
You must be signed in to change notification settings - Fork 123
/
main.go
63 lines (51 loc) · 1.15 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"time"
"github.com/vbauerster/mpb"
"github.com/vbauerster/mpb/decor"
)
func main() {
url := "https://github.com/onivim/oni/releases/download/v0.3.4/Oni-0.3.4-amd64-linux.deb"
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("Server return non-200 status: %s\n", resp.Status)
return
}
size := resp.ContentLength
// create dest
destName := filepath.Base(url)
dest, err := os.Create(destName)
if err != nil {
fmt.Printf("Can't create %s: %v\n", destName, err)
return
}
defer dest.Close()
p := mpb.New(
mpb.WithWidth(60),
mpb.WithRefreshRate(180*time.Millisecond),
)
bar := p.AddBar(size, mpb.BarStyle("[=>-|"),
mpb.PrependDecorators(
decor.CountersKibiByte("% 6.1f / % 6.1f"),
),
mpb.AppendDecorators(
decor.EwmaETA(decor.ET_STYLE_MMSS, float64(size)/2048),
decor.Name(" ] "),
decor.AverageSpeed(decor.UnitKiB, "% .2f"),
),
)
// create proxy reader
reader := bar.ProxyReader(resp.Body)
// and copy from reader, ignoring errors
io.Copy(dest, reader)
p.Wait()
}