-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteps.go
77 lines (63 loc) · 1.75 KB
/
steps.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
package ttyprogress
import (
"github.com/mandelsoft/goutils/stringutils"
"github.com/mandelsoft/object"
"github.com/mandelsoft/ttyprogress/specs"
)
// Steps can be used to visualize a sequence of steps.
type Steps interface {
specs.StepsInterface
}
type StepsDefinition struct {
specs.StepsDefinition[*StepsDefinition]
}
func NewSteps(steps ...string) *StepsDefinition {
d := &StepsDefinition{}
d.StepsDefinition = specs.NewStepsDefinition(specs.NewSelf(d), steps)
return d
}
func (d *StepsDefinition) Dup() *StepsDefinition {
dup := &StepsDefinition{}
dup.StepsDefinition = d.StepsDefinition.Dup(specs.NewSelf(dup))
return dup
}
func (d *StepsDefinition) Add(c Container) (Steps, error) {
return newSteps(c, d)
}
////////////////////////////////////////////////////////////////////////////////
type _Steps struct {
*IntBarBase[*_StepsImpl]
elem *_StepsImpl
}
func (s *_Steps) GetCurrentStep() string {
defer s.elem.Lock()()
return s.elem.Protected().GetCurrentStep()
}
type _StepsImpl struct {
*IntBarBaseImpl[*_StepsImpl]
steps []string
}
// NewSteps create a Steps progress information for a given
// list of sequential steps.
func newSteps(p Container, c specs.StepsConfiguration) (Steps, error) {
steps := stringutils.AlignLeft(c.GetSteps(), ' ')
e := &_StepsImpl{steps: steps}
o := &_Steps{elem: e}
b, s, err := newIntBar[*_StepsImpl](p, c, len(steps), object.NewSelf[*_StepsImpl, any](e, o))
if err != nil {
return nil, err
}
e.IntBarBaseImpl = s
o.IntBarBase = b
return o, nil
}
func (s *_StepsImpl) GetCurrentStep() string {
c := s.Current()
if c == 0 && !s.IsStarted() {
return stringutils.PadRight("", len(s.steps[0]), ' ')
}
if c < len(s.steps) {
return s.steps[c]
}
return stringutils.PadRight("", len(s.steps[0]), ' ')
}