-
Notifications
You must be signed in to change notification settings - Fork 1
/
statusbar.go
39 lines (29 loc) · 853 Bytes
/
statusbar.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
// Status bar serves to slow down requests in tests.
package goshikimori
import (
"fmt"
"time"
)
type conv float32
type StatusBar struct {
Percent, Current, Total int
Rate, Graph string
Wait time.Duration
}
func (s *StatusBar) settings(length int, symbol string, wait time.Duration) {
s.Total = length
s.Graph = symbol
s.Wait = wait
}
func (s *StatusBar) run() {
fmt.Printf("Too many requests at once, waiting %d seconds...\n", s.Total)
for i := 0; i <= s.Total; i++ {
s.Current = i
last := s.Percent
s.Percent = int((conv(s.Current)/conv(s.Total)) * 100)
if s.Percent != last && s.Percent%2 == 0 { s.Rate += s.Graph }
fmt.Printf("\r[%-5s] %5d%% %5d/%d", s.Rate, s.Percent, s.Current, s.Total)
time.Sleep(s.Wait * time.Second)
}
fmt.Println() // Is needed to move it to a new line at the end.
}