diff --git a/decor/decorator.go b/decor/decorator.go index 9e69757..dc0e9f8 100644 --- a/decor/decorator.go +++ b/decor/decorator.go @@ -4,7 +4,6 @@ "fmt" "time" - "github.com/acarl005/stripansi" "github.com/mattn/go-runewidth" ) @@ -66,9 +65,9 @@ // `DecorFunc` into a `Decorator` interface by using provided // `func Any(DecorFunc, ...WC) Decorator`. type Decorator interface { - Configurator Synchronizer - Decor(Statistics) string + Formatter + Decor(Statistics) (str string, viewWidth int) } // DecorFunc func type. @@ -82,10 +81,12 @@ Sync() (chan int, bool) } -// Configurator interface. -type Configurator interface { - GetConf() WC - SetConf(WC) +// Formatter interface. +// Format method needs to be called from within Decorator.Decor method +// in order to format string according to decor.WC settings. +// No need to implement manually as long as decor.WC is embedded. +type Formatter interface { + Format(string) (str string, viewWidth int) } // Wrapper interface. @@ -135,21 +136,21 @@ wsync chan int } -// FormatMsg formats final message according to WC.W and WC.C. -// Should be called by any Decorator implementation. -func (wc WC) FormatMsg(msg string) string { - pureWidth := runewidth.StringWidth(msg) - viewWidth := runewidth.StringWidth(stripansi.Strip(msg)) - max := wc.W +// Format should be called by any Decorator implementation. +// Returns formatted string and its view (visual) width. +func (wc WC) Format(str string) (string, int) { + viewWidth := runewidth.StringWidth(str) + if wc.W > viewWidth { + viewWidth = wc.W + } if (wc.C & DSyncWidth) != 0 { - viewWidth := viewWidth if (wc.C & DextraSpace) != 0 { viewWidth++ } wc.wsync <- viewWidth - max = <-wc.wsync + viewWidth = <-wc.wsync } - return wc.fill(msg, max-viewWidth+pureWidth) + return wc.fill(str, viewWidth), viewWidth } // Init initializes width related config. @@ -175,16 +176,6 @@ return wc.wsync, (wc.C & DSyncWidth) != 0 } -// GetConf is implementation of Configurator interface. -func (wc *WC) GetConf() WC { - return *wc -} - -// SetConf is implementation of Configurator interface. -func (wc *WC) SetConf(conf WC) { - *wc = conf.Init() -} - func initWC(wcc ...WC) WC { var wc WC for _, nwc := range wcc {