Codebase list golang-github-vbauerster-mpb / 468fc57
spinner filler with Meta Vladimir Bauer 2 years ago
1 changed file(s) with 21 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
33 "io"
44 "strings"
55
6 "github.com/acarl005/stripansi"
76 "github.com/mattn/go-runewidth"
87 "github.com/vbauerster/mpb/v8/decor"
98 "github.com/vbauerster/mpb/v8/internal"
1413 positionRight
1514 )
1615
16 var defaultSpinnerStyle = []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}
17
1718 // SpinnerStyleComposer interface.
1819 type SpinnerStyleComposer interface {
1920 BarFillerBuilder
2021 PositionLeft() SpinnerStyleComposer
2122 PositionRight() SpinnerStyleComposer
23 Meta(func(string) string) SpinnerStyleComposer
2224 }
2325
2426 type sFiller struct {
27 position uint
2528 count uint
26 position uint
2729 frames []string
30 meta func(string) string
2831 }
2932
3033 type spinnerStyle struct {
3134 position uint
3235 frames []string
36 meta func(string) string
3337 }
3438
3539 // SpinnerStyle constructs default spinner style which can be altered via
3640 // SpinnerStyleComposer interface.
3741 func SpinnerStyle(frames ...string) SpinnerStyleComposer {
38 ss := new(spinnerStyle)
42 ss := &spinnerStyle{
43 meta: func(s string) string {
44 return s
45 },
46 }
3947 if len(frames) != 0 {
40 ss.frames = append(ss.frames, frames...)
48 ss.frames = frames
4149 } else {
42 ss.frames = []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}
50 ss.frames = defaultSpinnerStyle
4351 }
4452 return ss
4553 }
5462 return s
5563 }
5664
65 func (s *spinnerStyle) Meta(fn func(string) string) SpinnerStyleComposer {
66 s.meta = fn
67 return s
68 }
69
5770 func (s *spinnerStyle) Build() BarFiller {
5871 sf := &sFiller{
5972 position: s.position,
6073 frames: s.frames,
74 meta: s.meta,
6175 }
6276 return sf
6377 }
6680 width := internal.CheckRequestedWidth(stat.RequestedWidth, stat.AvailableWidth)
6781
6882 frame := s.frames[s.count%uint(len(s.frames))]
69 frameWidth := runewidth.StringWidth(stripansi.Strip(frame))
83 frameWidth := runewidth.StringWidth(frame)
84 frame = s.meta(frame)
7085
7186 if width < frameWidth {
7287 return nil