-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelement.go
More file actions
83 lines (74 loc) · 1.68 KB
/
Copy pathelement.go
File metadata and controls
83 lines (74 loc) · 1.68 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package spinner
import (
"container/ring"
"fmt"
"github.com/mattn/go-runewidth"
"github.com/alecrabbit/go-cli-spinner/color"
)
// element ...
type element struct {
format string //
spacer string //
current string //
currentWidth int //
charSet *ring.Ring //
colorFormat *ring.Ring //
reversed bool //
emptyFormat string //
}
type elementSettings struct {
colorizingSet int
format string
spacer string
auxFormat string
charSet []string
}
func (el *element) update() {
if el.charSet != nil {
if el.reversed {
el.charSet = el.charSet.Prev()
} else {
el.charSet = el.charSet.Next()
}
el.current = el.charSet.Value.(string)
}
}
func (el *element) setCurrent(s string) {
el.current = s
if s == "" {
el.emptyFormat = ""
el.currentWidth = 0
return
}
el.currentWidth = runewidth.StringWidth(fmt.Sprintf(el.format+el.spacer, el.current))
}
func newElement(s *elementSettings) (*element, error) {
el := element{
format: s.format, //
spacer: s.spacer, //
}
el.colorFormat = createColorSet(color.Prototypes[s.colorizingSet], el.format+el.spacer)
if s.charSet != nil {
el.charSet = applyCharSet(s.charSet)
if el.charSet != nil {
el.currentWidth =
runewidth.StringWidth(el.charSet.Value.(string)) +
runewidth.StringWidth(fmt.Sprintf(el.format, el.spacer))
}
}
return &el, nil
}
// Colorize char
func (el *element) colorized() string {
// Note: external lock
if el.current == "" {
return ""
}
if el.colorFormat != nil {
// rotate
el.colorFormat = el.colorFormat.Next()
// apply
return fmt.Sprintf(el.colorFormat.Value.(string), el.current)
}
return el.current
}