Codebase list golang-github-vbauerster-mpb / f5d6cbe
MakeFillerTypeSpecificBarOption Vladimir Bauer 7 years ago
1 changed file(s) with 54 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
8888 }
8989 }
9090
91 // BarStyle sets custom bar style.
92 func BarStyle(style string) BarOption {
93 return func(s *bState) {
94 if style == "" {
95 return
96 }
97 if bf, ok := s.filler.(*barFiller); ok {
98 if !utf8.ValidString(style) {
99 panic("invalid style string")
100 }
101 defaultFormat := bf.format
102 bf.format = []rune(style)
103 if len(bf.format) < 5 {
104 bf.format = defaultFormat
105 }
106 }
107 }
108 }
109
110 // SpinnerStyle sets custom Spinner style.
111 func SpinnerStyle(frames []string) BarOption {
112 return func(s *bState) {
113 if len(frames) == 0 {
114 return
115 }
116 if bf, ok := s.filler.(*spinnerFiller); ok {
117 bf.frames = frames
118 }
119 }
120 }
121
12291 // TrimSpace trims bar's edge spaces.
12392 func TrimSpace() BarOption {
12493 return func(s *bState) {
12594 s.trimSpace = true
12695 }
12796 }
97
98 // BarStyle sets custom bar style.
99 // Effective when Filler type is bar.
100 func BarStyle(style string) BarOption {
101 chk := func(filler Filler) (interface{}, bool) {
102 if style == "" {
103 return nil, false
104 }
105 t, ok := filler.(*barFiller)
106 return t, ok
107 }
108 cb := func(t interface{}) {
109 bf := t.(*barFiller)
110 if !utf8.ValidString(style) {
111 panic("invalid style string")
112 }
113 defaultFormat := bf.format
114 bf.format = []rune(style)
115 if len(bf.format) < 5 {
116 bf.format = defaultFormat
117 }
118 }
119 return MakeFillerTypeSpecificBarOption(chk, cb)
120 }
121
122 // SpinnerStyle sets custom spinner style.
123 // Effective when Filler type is spinner.
124 func SpinnerStyle(frames []string) BarOption {
125 chk := func(filler Filler) (interface{}, bool) {
126 if len(frames) == 0 {
127 return nil, false
128 }
129 t, ok := filler.(*spinnerFiller)
130 return t, ok
131 }
132 cb := func(t interface{}) {
133 t.(*spinnerFiller).frames = frames
134 }
135 return MakeFillerTypeSpecificBarOption(chk, cb)
136 }
137
138 // MakeFillerTypeSpecificBarOption makes BarOption specific to Filler's actual type.
139 // If you implement your own Filler, so most probably you'll need this.
140 // See BarStyle or SpinnerStyle for example.
141 func MakeFillerTypeSpecificBarOption(
142 typeChecker func(Filler) (interface{}, bool),
143 cb func(interface{}),
144 ) BarOption {
145 return func(s *bState) {
146 if t, ok := typeChecker(s.filler); ok {
147 cb(t)
148 }
149 }
150 }