-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogressbar.go
More file actions
44 lines (37 loc) · 779 Bytes
/
Copy pathprogressbar.go
File metadata and controls
44 lines (37 loc) · 779 Bytes
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
package tui
import (
"fmt"
"time"
)
type ProgressBar struct {
Interval int
p *Printing
ticker *time.Ticker
Text string
}
func NewProgressBar(p *Printing) *ProgressBar {
return &ProgressBar{p: p, Interval: 500, Text: "Please wait"}
}
func (p *ProgressBar) Start() {
ticker := time.NewTicker(time.Millisecond * time.Duration(p.Interval))
p.ticker = ticker
x, y := p.p.Screen().Size()
p.p.putc(p.p.style.Default, x/2-len(p.Text)/2+1, y/2-1, p.Text)
i := 0
bar := []string{" | ", " / ", " - ", " \\ "}
for ok := true; ok; {
if i >= len(bar) {
i = 0
}
p.p.putc(styleTextHighlight, x/2, y/2, bar[i])
p.p.Show()
i++
_, ok = <-ticker.C
}
fmt.Println("exit")
}
func (p *ProgressBar) Stop() {
if p.ticker != nil {
p.ticker.Stop()
}
}