Codebase list golang-github-vbauerster-mpb / 69d71ef
refactoring percentage accept any verb which is supported by AppendFloat Vladimir Bauer 3 years ago
1 changed file(s) with 16 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
1111 type percentageType float64
1212
1313 func (s percentageType) Format(st fmt.State, verb rune) {
14 var prec int
14 prec := -1
1515 switch verb {
16 case 'd':
17 case 's':
18 prec = -1
19 default:
16 case 'f', 'e', 'E':
17 prec = 6 // default prec of fmt.Printf("%f|%e|%E")
18 fallthrough
19 case 'b', 'g', 'G', 'x', 'X':
2020 if p, ok := st.Precision(); ok {
2121 prec = p
22 } else {
23 prec = 6
2422 }
23 default:
24 verb, prec = 'f', 0
2525 }
2626
27 b := strconv.AppendFloat(make([]byte, 0, 32), float64(s), 'f', prec, 64)
27 b := strconv.AppendFloat(make([]byte, 0, 16), float64(s), byte(verb), prec, 64)
2828 if st.Flag(' ') {
2929 b = append(b, ' ', '%')
3030 } else {
4343
4444 // NewPercentage percentage decorator with custom format string.
4545 //
46 // `format` printf compatible verb
47 //
48 // `wcc` optional WC config
49 //
4650 // format examples:
4751 //
52 // format="%d" output: "1%"
53 // format="% d" output: "1 %"
4854 // format="%.1f" output: "1.0%"
4955 // format="% .1f" output: "1.0 %"
50 // format="%d" output: "1%"
51 // format="% d" output: "1 %"
56 // format="%f" output: "1.000000%"
57 // format="% f" output: "1.000000 %"
5258 func NewPercentage(format string, wcc ...WC) Decorator {
5359 if format == "" {
5460 format = "% d"