diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-16 16:12:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-16 16:12:43 +0000 |
commit | e28e21a9397e402a78499ceeef63dd19989be29e (patch) | |
tree | f4b7f6aaa6bb1563cac5f10b0bde8b6333ac4e37 /bar_filler_spinner.go | |
parent | Initial commit. (diff) | |
download | golang-github-vbauerster-mpb-e28e21a9397e402a78499ceeef63dd19989be29e.tar.xz golang-github-vbauerster-mpb-e28e21a9397e402a78499ceeef63dd19989be29e.zip |
Adding upstream version 8.6.1.upstream/8.6.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'bar_filler_spinner.go')
-rw-r--r-- | bar_filler_spinner.go | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/bar_filler_spinner.go b/bar_filler_spinner.go new file mode 100644 index 0000000..c9fd463 --- /dev/null +++ b/bar_filler_spinner.go @@ -0,0 +1,103 @@ +package mpb + +import ( + "io" + "strings" + + "github.com/mattn/go-runewidth" + "github.com/vbauerster/mpb/v8/decor" + "github.com/vbauerster/mpb/v8/internal" +) + +const ( + positionLeft = 1 + iota + positionRight +) + +var defaultSpinnerStyle = [...]string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"} + +// SpinnerStyleComposer interface. +type SpinnerStyleComposer interface { + BarFillerBuilder + PositionLeft() SpinnerStyleComposer + PositionRight() SpinnerStyleComposer + Meta(func(string) string) SpinnerStyleComposer +} + +type sFiller struct { + frames []string + count uint + meta func(string) string + position func(string, int) string +} + +type spinnerStyle struct { + position uint + frames []string + meta func(string) string +} + +// SpinnerStyle constructs default spinner style which can be altered via +// SpinnerStyleComposer interface. +func SpinnerStyle(frames ...string) SpinnerStyleComposer { + ss := spinnerStyle{ + meta: func(s string) string { return s }, + } + if len(frames) != 0 { + ss.frames = frames + } else { + ss.frames = defaultSpinnerStyle[:] + } + return ss +} + +func (s spinnerStyle) PositionLeft() SpinnerStyleComposer { + s.position = positionLeft + return s +} + +func (s spinnerStyle) PositionRight() SpinnerStyleComposer { + s.position = positionRight + return s +} + +func (s spinnerStyle) Meta(fn func(string) string) SpinnerStyleComposer { + s.meta = fn + return s +} + +func (s spinnerStyle) Build() BarFiller { + sf := &sFiller{ + frames: s.frames, + meta: s.meta, + } + switch s.position { + case positionLeft: + sf.position = func(frame string, padWidth int) string { + return frame + strings.Repeat(" ", padWidth) + } + case positionRight: + sf.position = func(frame string, padWidth int) string { + return strings.Repeat(" ", padWidth) + frame + } + default: + sf.position = func(frame string, padWidth int) string { + return strings.Repeat(" ", padWidth/2) + frame + strings.Repeat(" ", padWidth/2+padWidth%2) + } + } + return sf +} + +func (s *sFiller) Fill(w io.Writer, stat decor.Statistics) error { + width := internal.CheckRequestedWidth(stat.RequestedWidth, stat.AvailableWidth) + frame := s.frames[s.count%uint(len(s.frames))] + frameWidth := runewidth.StringWidth(frame) + s.count++ + + if width < frameWidth { + return nil + } + + _, err := io.WriteString(w, s.position(s.meta(frame), width-frameWidth)) + return err +} |