Codebase list golang-github-vbauerster-mpb / 41ac57b
Decorator interface: signature change This is necessary to support meta wrappers Vladimir Bauer 2 years ago
1 changed file(s) with 17 addition(s) and 26 deletion(s). Raw diff Collapse all Expand all
33 "fmt"
44 "time"
55
6 "github.com/acarl005/stripansi"
76 "github.com/mattn/go-runewidth"
87 )
98
6564 // `DecorFunc` into a `Decorator` interface by using provided
6665 // `func Any(DecorFunc, ...WC) Decorator`.
6766 type Decorator interface {
68 Configurator
6967 Synchronizer
70 Decor(Statistics) string
68 Formatter
69 Decor(Statistics) (str string, viewWidth int)
7170 }
7271
7372 // DecorFunc func type.
8180 Sync() (chan int, bool)
8281 }
8382
84 // Configurator interface.
85 type Configurator interface {
86 GetConf() WC
87 SetConf(WC)
83 // Formatter interface.
84 // Format method needs to be called from within Decorator.Decor method
85 // in order to format string according to decor.WC settings.
86 // No need to implement manually as long as decor.WC is embedded.
87 type Formatter interface {
88 Format(string) (str string, viewWidth int)
8889 }
8990
9091 // Wrapper interface.
134135 wsync chan int
135136 }
136137
137 // FormatMsg formats final message according to WC.W and WC.C.
138 // Should be called by any Decorator implementation.
139 func (wc WC) FormatMsg(msg string) string {
140 pureWidth := runewidth.StringWidth(msg)
141 viewWidth := runewidth.StringWidth(stripansi.Strip(msg))
142 max := wc.W
138 // Format should be called by any Decorator implementation.
139 // Returns formatted string and its view (visual) width.
140 func (wc WC) Format(str string) (string, int) {
141 viewWidth := runewidth.StringWidth(str)
142 if wc.W > viewWidth {
143 viewWidth = wc.W
144 }
143145 if (wc.C & DSyncWidth) != 0 {
144 viewWidth := viewWidth
145146 if (wc.C & DextraSpace) != 0 {
146147 viewWidth++
147148 }
148149 wc.wsync <- viewWidth
149 max = <-wc.wsync
150 viewWidth = <-wc.wsync
150151 }
151 return wc.fill(msg, max-viewWidth+pureWidth)
152 return wc.fill(str, viewWidth), viewWidth
152153 }
153154
154155 // Init initializes width related config.
174175 return wc.wsync, (wc.C & DSyncWidth) != 0
175176 }
176177
177 // GetConf is implementation of Configurator interface.
178 func (wc *WC) GetConf() WC {
179 return *wc
180 }
181
182 // SetConf is implementation of Configurator interface.
183 func (wc *WC) SetConf(conf WC) {
184 *wc = conf.Init()
185 }
186
187178 func initWC(wcc ...WC) WC {
188179 var wc WC
189180 for _, nwc := range wcc {