refactoring filler spinner
determine position at spinnerStyle Build time
Vladimir Bauer
2 years ago
| 0 | 0 | package mpb |
| 1 | 1 | |
| 2 | 2 | import ( |
| 3 | "fmt" | |
| 3 | 4 | "io" |
| 4 | 5 | "strings" |
| 5 | 6 | |
| 24 | 25 | } |
| 25 | 26 | |
| 26 | 27 | type sFiller struct { |
| 27 | position uint | |
| 28 | frames []string | |
| 28 | 29 | count uint |
| 29 | frames []string | |
| 30 | 30 | meta func(string) string |
| 31 | position func(string, int) string | |
| 31 | 32 | } |
| 32 | 33 | |
| 33 | 34 | type spinnerStyle struct { |
| 40 | 41 | // SpinnerStyleComposer interface. |
| 41 | 42 | func SpinnerStyle(frames ...string) SpinnerStyleComposer { |
| 42 | 43 | ss := spinnerStyle{ |
| 43 | meta: func(s string) string { | |
| 44 | return s | |
| 45 | }, | |
| 44 | meta: func(s string) string { return s }, | |
| 46 | 45 | } |
| 47 | 46 | if len(frames) != 0 { |
| 48 | 47 | ss.frames = frames |
| 69 | 68 | |
| 70 | 69 | func (s spinnerStyle) Build() BarFiller { |
| 71 | 70 | sf := &sFiller{ |
| 72 | position: s.position, | |
| 73 | frames: s.frames, | |
| 74 | meta: s.meta, | |
| 71 | frames: s.frames, | |
| 72 | meta: s.meta, | |
| 73 | } | |
| 74 | switch s.position { | |
| 75 | case positionLeft: | |
| 76 | sf.position = func(frame string, padWidth int) string { | |
| 77 | return fmt.Sprint(frame, strings.Repeat(" ", padWidth)) | |
| 78 | } | |
| 79 | case positionRight: | |
| 80 | sf.position = func(frame string, padWidth int) string { | |
| 81 | return fmt.Sprint(strings.Repeat(" ", padWidth), frame) | |
| 82 | } | |
| 83 | default: | |
| 84 | sf.position = func(frame string, padWidth int) string { | |
| 85 | return fmt.Sprint(strings.Repeat(" ", padWidth/2), frame, strings.Repeat(" ", padWidth/2+padWidth%2)) | |
| 86 | } | |
| 75 | 87 | } |
| 76 | 88 | return sf |
| 77 | 89 | } |
| 78 | 90 | |
| 79 | func (s *sFiller) Fill(w io.Writer, stat decor.Statistics) (err error) { | |
| 91 | func (s *sFiller) Fill(w io.Writer, stat decor.Statistics) error { | |
| 80 | 92 | width := internal.CheckRequestedWidth(stat.RequestedWidth, stat.AvailableWidth) |
| 81 | ||
| 82 | 93 | frame := s.frames[s.count%uint(len(s.frames))] |
| 83 | 94 | frameWidth := runewidth.StringWidth(frame) |
| 84 | frame = s.meta(frame) | |
| 95 | s.count++ | |
| 85 | 96 | |
| 86 | 97 | if width < frameWidth { |
| 87 | 98 | return nil |
| 88 | 99 | } |
| 89 | 100 | |
| 90 | rest := width - frameWidth | |
| 91 | switch s.position { | |
| 92 | case positionLeft: | |
| 93 | _, err = io.WriteString(w, frame+strings.Repeat(" ", rest)) | |
| 94 | case positionRight: | |
| 95 | _, err = io.WriteString(w, strings.Repeat(" ", rest)+frame) | |
| 96 | default: | |
| 97 | str := strings.Repeat(" ", rest/2) + frame + strings.Repeat(" ", rest/2+rest%2) | |
| 98 | _, err = io.WriteString(w, str) | |
| 99 | } | |
| 100 | s.count++ | |
| 101 | _, err := io.WriteString(w, s.position(s.meta(frame), width-frameWidth)) | |
| 101 | 102 | return err |
| 102 | 103 | } |