decor

package
v4.12.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2020 License: Unlicense Imports: 12 Imported by: 42

Documentation

Overview

 Package decor provides common decorators for "github.com/vbauerster/mpb/v4" module.

 Some decorators returned by this package might have a closure state. It is ok to use
 decorators concurrently, unless you share the same decorator among multiple
 *mpb.Bar instances. To avoid data races, create new decorator per *mpb.Bar instance.

 Don't:

	 p := mpb.New()
	 name := decor.Name("bar")
	 p.AddBar(100, mpb.AppendDecorators(name))
	 p.AddBar(100, mpb.AppendDecorators(name))

 Do:

	p := mpb.New()
	p.AddBar(100, mpb.AppendDecorators(decor.Name("bar1")))
	p.AddBar(100, mpb.AppendDecorators(decor.Name("bar2")))

Index

Constants

View Source
const (
	UnitKiB
	UnitKB
)
View Source
const (
	// DidentRight bit specifies identation direction.
	// |foo   |b     | With DidentRight
	// |   foo|     b| Without DidentRight
	DidentRight = 1 << iota

	// DextraSpace bit adds extra space, makes sense with DSyncWidth only.
	// When DidentRight bit set, the space will be added to the right,
	// otherwise to the left.
	DextraSpace

	// DSyncWidth bit enables same column width synchronization.
	// Effective with multiple bars only.
	DSyncWidth

	// DSyncWidthR is shortcut for DSyncWidth|DidentRight
	DSyncWidthR = DSyncWidth | DidentRight

	// DSyncSpace is shortcut for DSyncWidth|DextraSpace
	DSyncSpace = DSyncWidth | DextraSpace

	// DSyncSpaceR is shortcut for DSyncWidth|DextraSpace|DidentRight
	DSyncSpaceR = DSyncWidth | DextraSpace | DidentRight
)

Variables

View Source
var (
	WCSyncWidth  = WC{C: DSyncWidth}
	WCSyncWidthR = WC{C: DSyncWidthR}
	WCSyncSpace  = WC{C: DSyncSpace}
	WCSyncSpaceR = WC{C: DSyncSpaceR}
)

Global convenience instances of WC with sync width bit set.

Functions

func FmtAsSpeed added in v4.12.1

func FmtAsSpeed(input fmt.Formatter) fmt.Formatter

FmtAsSpeed adds "/s" to the end of the input formatter. To be used with SizeB1000 or SizeB1024 types, for example:

fmt.Printf("%.1f", FmtAsSpeed(SizeB1024(2048)))

func NewThreadSafeMovingAverage added in v4.12.2

func NewThreadSafeMovingAverage(average ewma.MovingAverage) ewma.MovingAverage

NewThreadSafeMovingAverage converts provided ewma.MovingAverage into thread safe ewma.MovingAverage.

Types

type AmountReceiver

type AmountReceiver interface {
	NextAmount(int64, ...time.Duration)
}

AmountReceiver interface. EWMA based decorators need to implement this one.

type AverageAdjuster added in v4.9.0

type AverageAdjuster interface {
	AverageAdjust(time.Time)
}

AverageAdjuster interface. Average decorators should implement this interface to provide start time adjustment facility, for resume-able tasks.

type CBFunc added in v4.9.0

type CBFunc func(Decorator)

CBFunc convenience call back func type.

type Configurator added in v4.11.0

type Configurator interface {
	GetConf() WC
	SetConf(WC)
}

Configurator interface.

type Decorator

type Decorator interface {
	Configurator
	Synchronizer
	Decor(*Statistics) string
}

Decorator interface. Implementors should embed WC type, that way only single method Decor(*Statistics) needs to be implemented, the rest will be handled by WC type.

func Any added in v4.12.1

func Any(f func(*Statistics) string, wcc ...WC) Decorator

Any decorator displays text, that can be changed during decorator's lifetime via provided func call back.

`f` call back which provides string to display

`wcc` optional WC config

func AverageETA

func AverageETA(style TimeStyle, wcc ...WC) Decorator

AverageETA decorator. It's wrapper of NewAverageETA.

`style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS]

`wcc` optional WC config

func AverageSpeed

func AverageSpeed(unit int, format string, wcc ...WC) Decorator

AverageSpeed decorator with dynamic unit measure adjustment. It's a wrapper of NewAverageSpeed.

func Counters

func Counters(unit int, pairFmt string, wcc ...WC) Decorator

Counters decorator with dynamic unit measure adjustment.

`unit` one of [0|UnitKiB|UnitKB] zero for no unit

`pairFmt` printf compatible verbs for current and total, like "%f" or "%d"

`wcc` optional WC config

pairFmt example if unit=UnitKB:

pairFmt="%.1f / %.1f"   output: "1.0MB / 12.0MB"
pairFmt="% .1f / % .1f" output: "1.0 MB / 12.0 MB"
pairFmt="%d / %d"       output: "1MB / 12MB"
pairFmt="% d / % d"     output: "1 MB / 12 MB"

func CountersKibiByte

func CountersKibiByte(pairFmt string, wcc ...WC) Decorator

CountersKibiByte is a wrapper around Counters with predefined unit UnitKiB (bytes/1024).

func CountersKiloByte

func CountersKiloByte(pairFmt string, wcc ...WC) Decorator

CountersKiloByte is a wrapper around Counters with predefined unit UnitKB (bytes/1000).

func CountersNoUnit

func CountersNoUnit(pairFmt string, wcc ...WC) Decorator

CountersNoUnit is a wrapper around Counters with no unit param.

func Elapsed

func Elapsed(style TimeStyle, wcc ...WC) 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 EwmaETA

func EwmaETA(style TimeStyle, age float64, wcc ...WC) Decorator

EwmaETA exponential-weighted-moving-average based ETA decorator. Note that it's necessary to supply bar.Incr* methods with incremental work duration as second argument, in order for this decorator to work correctly. This decorator is a wrapper of MovingAverageETA.

func EwmaSpeed

func EwmaSpeed(unit int, format string, age float64, wcc ...WC) Decorator

EwmaSpeed exponential-weighted-moving-average based speed decorator. Note that it's necessary to supply bar.Incr* methods with incremental work duration as second argument, in order for this decorator to work correctly. This decorator is a wrapper of MovingAverageSpeed.

func Merge added in v4.7.0

func Merge(decorator Decorator, placeholders ...WC) Decorator

Merge wraps its decorator argument with intention to sync width with several decorators of another bar. Visual example:

+----+--------+---------+--------+
| B1 |      MERGE(D, P1, Pn)     |
+----+--------+---------+--------+
| B2 |   D0   |   D1    |   Dn   |
+----+--------+---------+--------+

func MovingAverageETA

func MovingAverageETA(style TimeStyle, average MovingAverage, normalizer TimeNormalizer, wcc ...WC) Decorator

MovingAverageETA decorator relies on MovingAverage implementation to calculate its average.

`style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS]

`average` implementation of MovingAverage interface

`normalizer` available implementations are [FixedIntervalTimeNormalizer|MaxTolerateTimeNormalizer]

`wcc` optional WC config

func MovingAverageSpeed

func MovingAverageSpeed(unit int, format string, average MovingAverage, wcc ...WC) Decorator

MovingAverageSpeed decorator relies on MovingAverage implementation to calculate its average.

`unit` one of [0|UnitKiB|UnitKB] zero for no unit

`format` printf compatible verb for value, like "%f" or "%d"

`average` MovingAverage implementation

`wcc` optional WC config

format examples:

unit=UnitKiB, format="%.1f"  output: "1.0MiB/s"
unit=UnitKiB, format="% .1f" output: "1.0 MiB/s"
unit=UnitKB,  format="%.1f"  output: "1.0MB/s"
unit=UnitKB,  format="% .1f" output: "1.0 MB/s"

func Name

func Name(str string, wcc ...WC) Decorator

Name decorator displays text that is set once and can't be changed during decorator's lifetime.

`str` string to display

`wcc` optional WC config

func NewAverageETA added in v4.9.0

func NewAverageETA(style TimeStyle, startTime time.Time, normalizer TimeNormalizer, wcc ...WC) Decorator

NewAverageETA decorator with user provided start time.

`style` one of [ET_STYLE_GO|ET_STYLE_HHMMSS|ET_STYLE_HHMM|ET_STYLE_MMSS]

`startTime` start time

`normalizer` available implementations are [FixedIntervalTimeNormalizer|MaxTolerateTimeNormalizer]

`wcc` optional WC config

func NewAverageSpeed added in v4.9.0

func NewAverageSpeed(unit int, format string, startTime time.Time, wcc ...WC) Decorator

NewAverageSpeed decorator with dynamic unit measure adjustment and user provided start time.

`unit` one of [0|UnitKiB|UnitKB] zero for no unit

`format` printf compatible verb for value, like "%f" or "%d"

`startTime` start time

`wcc` optional WC config

format examples:

unit=UnitKiB, format="%.1f"  output: "1.0MiB/s"
unit=UnitKiB, format="% .1f" output: "1.0 MiB/s"
unit=UnitKB,  format="%.1f"  output: "1.0MB/s"
unit=UnitKB,  format="% .1f" output: "1.0 MB/s"

func NewElapsed added in v4.9.2

func NewElapsed(style TimeStyle, startTime time.Time, wcc ...WC) Decorator

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 NewPercentage added in v4.9.0

func NewPercentage(format string, wcc ...WC) Decorator

NewPercentage percentage decorator with custom format string.

format examples:

format="%.1f"  output: "1.0%"
format="% .1f" output: "1.0 %"
format="%d"    output: "1%"
format="% d"   output: "1 %"

func OnComplete

func OnComplete(decorator Decorator, message string) Decorator

OnComplete returns decorator, which wraps provided decorator, with sole purpose to display provided message on complete event.

`decorator` Decorator to wrap

`message` message to display on complete event

func Percentage

func Percentage(wcc ...WC) Decorator

Percentage returns percentage decorator. It's a wrapper of NewPercentage.

func Spinner added in v4.2.0

func Spinner(frames []string, wcc ...WC) Decorator

Spinner returns spinner decorator.

`frames` spinner frames, if nil or len==0, default is used

`wcc` optional WC config

type MovingAverage

type MovingAverage = ewma.MovingAverage

MovingAverage is the interface that computes a moving average over a time-series stream of numbers. The average may be over a window or exponentially decaying.

func NewMedian

func NewMedian() MovingAverage

NewMedian is fixed last 3 samples median MovingAverage.

type ShutdownListener

type ShutdownListener interface {
	Shutdown()
}

ShutdownListener interface. If decorator needs to be notified once upon bar shutdown event, so this is the right interface to implement.

type SizeB1000 added in v4.9.0

type SizeB1000 int64

SizeB1000 named type, which implements fmt.Formatter interface. It adjusts its value according to byte size multiple by 1000 and appends appropriate size marker (KB, MB, GB, TB).

func (SizeB1000) Format added in v4.9.0

func (self SizeB1000) Format(st fmt.State, verb rune)

func (SizeB1000) String added in v4.9.0

func (i SizeB1000) String() string

type SizeB1024 added in v4.9.0

type SizeB1024 int64

SizeB1024 named type, which implements fmt.Formatter interface. It adjusts its value according to byte size multiple by 1024 and appends appropriate size marker (KiB, MiB, GiB, TiB).

func (SizeB1024) Format added in v4.9.0

func (self SizeB1024) Format(st fmt.State, verb rune)

func (SizeB1024) String added in v4.9.0

func (i SizeB1024) String() string

type Statistics

type Statistics struct {
	ID        int
	Completed bool
	Total     int64
	Current   int64
}

Statistics consists of progress related statistics, that Decorator may need.

type Synchronizer added in v4.0.2

type Synchronizer interface {
	Sync() (chan int, bool)
}

Synchronizer interface. All decorators implement this interface implicitly. Its Sync method exposes width sync channel, if DSyncWidth bit is set.

type TimeNormalizer

type TimeNormalizer interface {
	Normalize(time.Duration) time.Duration
}

TimeNormalizer interface. Implementors could be passed into MovingAverageETA, in order to affect i.e. normalize its output.

func FixedIntervalTimeNormalizer

func FixedIntervalTimeNormalizer(updInterval int) TimeNormalizer

FixedIntervalTimeNormalizer returns implementation of TimeNormalizer.

func MaxTolerateTimeNormalizer

func MaxTolerateTimeNormalizer(maxTolerate time.Duration) TimeNormalizer

MaxTolerateTimeNormalizer returns implementation of TimeNormalizer.

type TimeNormalizerFunc added in v4.0.3

type TimeNormalizerFunc func(time.Duration) time.Duration

TimeNormalizerFunc is function type adapter to convert function into TimeNormalizer.

func (TimeNormalizerFunc) Normalize added in v4.0.3

func (f TimeNormalizerFunc) Normalize(src time.Duration) time.Duration

type TimeStyle

type TimeStyle int

TimeStyle enum.

const (
	ET_STYLE_GO TimeStyle = iota
	ET_STYLE_HHMMSS
	ET_STYLE_HHMM
	ET_STYLE_MMSS
)

TimeStyle kinds.

type WC

type WC struct {
	W int
	C int
	// contains filtered or unexported fields
}

WC is a struct with two public fields W and C, both of int type. W represents width and C represents bit set of width related config. A decorator should embed WC, to enable width synchronization.

func (*WC) FormatMsg

func (wc *WC) FormatMsg(msg string) string

FormatMsg formats final message according to WC.W and WC.C. Should be called by any Decorator implementation.

func (*WC) GetConf added in v4.11.0

func (wc *WC) GetConf() WC

GetConf is implementation of Configurator interface.

func (*WC) Init

func (wc *WC) Init() WC

Init initializes width related config.

func (*WC) SetConf added in v4.11.0

func (wc *WC) SetConf(conf WC)

SetConf is implementation of Configurator interface.

func (*WC) Sync added in v4.0.2

func (wc *WC) Sync() (chan int, bool)

Sync is implementation of Synchronizer interface.

type Wrapper added in v4.11.0

type Wrapper interface {
	Base() Decorator
}

Wrapper interface. If you're implementing custom Decorator by wrapping a built-in one, it is necessary to implement this interface to retain functionality of built-in Decorator.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL