-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.go
More file actions
63 lines (59 loc) · 1.88 KB
/
example.go
File metadata and controls
63 lines (59 loc) · 1.88 KB
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 (
"flag"
"fmt"
"io"
"os"
"time"
"fortio.org/progressbar"
)
func PrintStuff(pb *progressbar.Bar, w io.Writer, every time.Duration) {
ticker := time.NewTicker(every)
i := 0
for {
i++
nowStr := time.Now().Format("15:04:05 ")
fmt.Fprintf(w, "[%d] Just an extra demo print every %v: %s\n", i, every, nowStr)
pb.UpdatePrefix(nowStr)
<-ticker.C
}
}
func main() {
colorFlag := flag.Bool("color", false, "Use color in the progress bar")
delayFlag := flag.Duration("delay", 50*time.Millisecond, "Delay between progress bar updates")
everyFlag := flag.Duration("every", 1*time.Second, "Print extra stuff every")
noAnsiFlag := flag.Bool("no-ansi", false, "Disable ANSI escape codes (colors and cursor movement)")
moveUpFlag := flag.Bool("moveup", false, "Demo in place move instead of writer")
noPercent := flag.Bool("no-percent", false, "Disable percent display")
noSuffix := flag.Bool("no-suffix", false, "Disable suffix display")
flag.Parse()
cfg := progressbar.DefaultConfig()
cfg.UseColors = *colorFlag
cfg.NoAnsi = *noAnsiFlag
cfg.NoPercent = *noPercent
cfg.ScreenWriter = os.Stdout // For playground, defaults to stderr otherwise.
pb := cfg.NewBar()
w := pb.Writer()
fmt.Fprintln(w, "Single progress bar example")
moveUpMode := *moveUpFlag
if moveUpMode {
fmt.Fprintln(w, "This line for space to demo MoveCursorUp mode")
} else {
// demonstrate concurrency safety:
go PrintStuff(pb, w, *everyFlag)
}
// exact number of 'pixels', just to demo every smooth step:
n := pb.Width * 8
for i := 0; i <= n; i++ {
if !*noSuffix {
pb.UpdateSuffix(fmt.Sprintf(" %d/%d", i, n))
}
pb.Progress(100. * float64(i) / float64(n))
if moveUpMode && i%63 == 0 {
pb.MoveCursorUp(1)
fmt.Printf("Just an extra demo print for %d\n", i)
}
time.Sleep(*delayFlag)
}
pb.End() // When done, prints a newline as the progress bar otherwise updates on same line.
}