-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexamples_test.go
71 lines (53 loc) · 1.82 KB
/
examples_test.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
64
65
66
67
68
69
70
71
package progress_test
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2025 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"io"
"os"
"time"
"github.com/essentialkaos/ek/v13/progress"
"github.com/essentialkaos/ek/v13/req"
)
// ////////////////////////////////////////////////////////////////////////////////// //
func ExampleNew_simple() {
pb := progress.New(1000, "Counting…")
// You can use default settings as a starting point
pbs := progress.DefaultSettings
pbs.RefreshRate = 50 * time.Millisecond
pbs.IsSize = false
// You can update all settings except RefreshRate before and after
// calling Start method. RefreshRate must be set before calling
// Start method.
pb.UpdateSettings(pbs)
pb.Start() // Start async progress handling
for i := 0; i < 1000; i++ {
time.Sleep(time.Second / 100)
pb.Add(1)
}
pb.Finish() // Stop async progress handling
}
func ExampleNew_download() {
pb := progress.New(0, "file.zip")
resp, err := req.Request{URL: "https://domain.com/file.zip"}.Get()
if err != nil {
panic(err.Error())
}
if resp.StatusCode != 200 {
panic("Looks like something is wrong")
}
defer resp.Body.Close()
fd, err := os.OpenFile("file.zip", os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err.Error())
}
defer fd.Close()
// Set total size to content length
pb.SetTotal(resp.ContentLength)
pb.Start()
io.Copy(fd, pb.Reader(resp.Body))
pb.Finish()
}