summaryrefslogtreecommitdiff
path: root/vendor/github.com/vbauerster/mpb/decor/percentage.go
blob: 078fbcf8931f735537d1aab178f0827d8174f13f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package decor

import (
	"fmt"

	"github.com/vbauerster/mpb/internal"
)

// Percentage returns percentage decorator.
//
//	`wcc` optional WC config
func Percentage(wcc ...WC) Decorator {
	var wc WC
	for _, widthConf := range wcc {
		wc = widthConf
	}
	wc.Init()
	d := &percentageDecorator{
		WC: wc,
	}
	return d
}

type percentageDecorator struct {
	WC
	completeMsg *string
}

func (d *percentageDecorator) Decor(st *Statistics) string {
	if st.Completed && d.completeMsg != nil {
		return d.FormatMsg(*d.completeMsg)
	}
	str := fmt.Sprintf("%d %%", internal.Percentage(st.Total, st.Current, 100))
	return d.FormatMsg(str)
}

func (d *percentageDecorator) OnCompleteMessage(msg string) {
	d.completeMsg = &msg
}