diff --git a/decor/elapsed.go b/decor/elapsed.go index b2e7585..b4ca601 100644 --- a/decor/elapsed.go +++ b/decor/elapsed.go @@ -1,16 +1,26 @@ package decor import ( - "fmt" "time" ) -// Elapsed returns elapsed time decorator. +// Elapsed decorator. It's wrapper of NewElapsed. // // `style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS] // // `wcc` optional WC config func Elapsed(style TimeStyle, wcc ...WC) Decorator { + return NewElapsed(style, time.Now(), wcc...) +} + +// NewElapsed returns elapsed time decorator. +// +// `style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS] +// +// `startTime` start time +// +// `wcc` optional WC config +func NewElapsed(style TimeStyle, startTime time.Time, wcc ...WC) Decorator { var wc WC for _, widthConf := range wcc { wc = widthConf @@ -18,16 +28,16 @@ wc.Init() d := &elapsedDecorator{ WC: wc, - style: style, - startTime: time.Now(), + startTime: startTime, + producer: chooseTimeProducer(style), } return d } type elapsedDecorator struct { WC - style TimeStyle startTime time.Time + producer func(time.Duration) string msg string completeMsg *string } @@ -40,26 +50,7 @@ return d.FormatMsg(d.msg) } - timeElapsed := time.Since(d.startTime) - hours := int64((timeElapsed / time.Hour) % 60) - minutes := int64((timeElapsed / time.Minute) % 60) - seconds := int64((timeElapsed / time.Second) % 60) - - switch d.style { - case ET_STYLE_GO: - d.msg = fmt.Sprint(time.Duration(timeElapsed.Seconds()) * time.Second) - case ET_STYLE_HHMMSS: - d.msg = fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds) - case ET_STYLE_HHMM: - d.msg = fmt.Sprintf("%02d:%02d", hours, minutes) - case ET_STYLE_MMSS: - if hours > 0 { - d.msg = fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds) - } else { - d.msg = fmt.Sprintf("%02d:%02d", minutes, seconds) - } - } - + d.msg = d.producer(time.Since(d.startTime)) return d.FormatMsg(d.msg) } diff --git a/decor/eta.go b/decor/eta.go index 00ef8c9..dbe4b93 100644 --- a/decor/eta.go +++ b/decor/eta.go @@ -55,7 +55,7 @@ WC: wc, average: average, normalizer: normalizer, - producer: chooseEtaProducer(style), + producer: chooseTimeProducer(style), } return d } @@ -97,7 +97,7 @@ d.completeMsg = &msg } -// AverageETA decorator. +// AverageETA decorator. It's wrapper of NewAverageETA. // // `style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS] // @@ -122,7 +122,7 @@ d := &averageETA{ WC: wc, startTime: startTime, - producer: chooseEtaProducer(style), + producer: chooseTimeProducer(style), } return d } @@ -192,7 +192,7 @@ }) } -func chooseEtaProducer(style TimeStyle) func(time.Duration) string { +func chooseTimeProducer(style TimeStyle) func(time.Duration) string { switch style { case ET_STYLE_HHMMSS: return func(remaining time.Duration) string {