-
Notifications
You must be signed in to change notification settings - Fork 0
/
spinner.go
104 lines (95 loc) · 2.06 KB
/
spinner.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (C) 2015-2020 the Gprovision Authors. All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// SPDX-License-Identifier: BSD-3-Clause
//
package cfa
import (
"gprovision/pkg/log"
"time"
)
//Spinner is a message + ASCII progress spinner. Start with Display(), update
//with spinner.Next().
//
//NOTE - will not prevent, detect, or recover gracefully from something else
//changing the display.
type Spinner struct {
Msg string
seq int
loc, max Coord
last time.Time
Lcd *Lcd
}
var spinnerSprites []byte
func init() {
/* chars (x is approx): | / - + * x \ */
spinnerSprites = []byte{0xfe, '/', '-', '+', '*', 0x24, 0xfb}
}
//clear display and show s.Msg
//also calculate coords for spinner
//put a space between message and spinner when possible
func (s *Spinner) Display() error {
if s.Lcd == nil {
log.Logf("unable to construct spinner with nil Lcd")
return ENil
}
var err error
s.max = s.Lcd.MaxCursorPos()
s.loc, err = s.Lcd.Msg(s.Msg)
if err != nil {
return err
}
s.loc.Col += 2
if s.loc.Col > s.max.Col {
if s.loc.Row < s.max.Row {
s.loc.Row++
s.loc.Col = 0
} else {
s.loc.Col = s.max.Col
}
}
return nil
}
//Advance spinner to next state. Rate limited to < 1/s
func (s *Spinner) Next() {
if s.Lcd == nil {
return
}
if s.last.Add(time.Second).After(time.Now()) {
return
}
if s.seq >= len(spinnerSprites) {
s.seq = 0
}
err := s.Lcd.write(s.loc, []byte{spinnerSprites[s.seq]}, false)
if err != nil && s.Lcd.DbgGeneral {
log.Logf("Spinner error: %s", err)
}
s.seq++
s.last = time.Now()
}
//Displays spinner until 'done' is closed. Interval must be at least 1s.
func (l *Lcd) SpinnerUntil(msg string, interval time.Duration, done chan struct{}) (err error) {
if l == nil {
<-done
return
}
sp := Spinner{
Msg: msg,
Lcd: l,
}
err = sp.Display()
l.mutex.Lock()
defer l.mutex.Unlock()
for {
select {
case <-done:
return
case <-time.After(interval):
if err == nil {
sp.Next()
}
}
}
}