summaryrefslogtreecommitdiff
path: root/vendor/github.com/vbauerster/mpb/v7/bar_filler_bar.go
blob: e30d4921c6315df7726218ea91bc88d909d50a33 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package mpb

import (
	"io"

	"github.com/acarl005/stripansi"
	"github.com/mattn/go-runewidth"
	"github.com/vbauerster/mpb/v7/decor"
	"github.com/vbauerster/mpb/v7/internal"
)

const (
	iLbound = iota
	iRbound
	iFiller
	iRefiller
	iPadding
	components
)

// BarStyleComposer interface.
type BarStyleComposer interface {
	BarFillerBuilder
	Lbound(string) BarStyleComposer
	Rbound(string) BarStyleComposer
	Filler(string) BarStyleComposer
	Refiller(string) BarStyleComposer
	Padding(string) BarStyleComposer
	Tip(...string) BarStyleComposer
	Reverse() BarStyleComposer
}

type bFiller struct {
	components [components]*component
	tip        struct {
		count  uint
		frames []*component
	}
	flush func(dst io.Writer, filling, padding [][]byte)
}

type component struct {
	width int
	bytes []byte
}

type barStyle struct {
	lbound   string
	rbound   string
	filler   string
	refiller string
	padding  string
	tip      []string
	rev      bool
}

// BarStyle constructs default bar style which can be altered via
// BarStyleComposer interface.
func BarStyle() BarStyleComposer {
	return &barStyle{
		lbound:   "[",
		rbound:   "]",
		filler:   "=",
		refiller: "+",
		padding:  "-",
		tip:      []string{">"},
	}
}

func (s *barStyle) Lbound(bound string) BarStyleComposer {
	s.lbound = bound
	return s
}

func (s *barStyle) Rbound(bound string) BarStyleComposer {
	s.rbound = bound
	return s
}

func (s *barStyle) Filler(filler string) BarStyleComposer {
	s.filler = filler
	return s
}

func (s *barStyle) Refiller(refiller string) BarStyleComposer {
	s.refiller = refiller
	return s
}

func (s *barStyle) Padding(padding string) BarStyleComposer {
	s.padding = padding
	return s
}

func (s *barStyle) Tip(tip ...string) BarStyleComposer {
	if len(tip) != 0 {
		s.tip = append(s.tip[:0], tip...)
	}
	return s
}

func (s *barStyle) Reverse() BarStyleComposer {
	s.rev = true
	return s
}

func (s *barStyle) Build() BarFiller {
	bf := new(bFiller)
	if s.rev {
		bf.flush = func(dst io.Writer, filling, padding [][]byte) {
			flush(dst, padding, filling)
		}
	} else {
		bf.flush = flush
	}
	bf.components[iLbound] = &component{
		width: runewidth.StringWidth(stripansi.Strip(s.lbound)),
		bytes: []byte(s.lbound),
	}
	bf.components[iRbound] = &component{
		width: runewidth.StringWidth(stripansi.Strip(s.rbound)),
		bytes: []byte(s.rbound),
	}
	bf.components[iFiller] = &component{
		width: runewidth.StringWidth(stripansi.Strip(s.filler)),
		bytes: []byte(s.filler),
	}
	bf.components[iRefiller] = &component{
		width: runewidth.StringWidth(stripansi.Strip(s.refiller)),
		bytes: []byte(s.refiller),
	}
	bf.components[iPadding] = &component{
		width: runewidth.StringWidth(stripansi.Strip(s.padding)),
		bytes: []byte(s.padding),
	}
	bf.tip.frames = make([]*component, len(s.tip))
	for i, t := range s.tip {
		bf.tip.frames[i] = &component{
			width: runewidth.StringWidth(stripansi.Strip(t)),
			bytes: []byte(t),
		}
	}
	return bf
}

func (s *bFiller) Fill(w io.Writer, width int, stat decor.Statistics) {
	width = internal.CheckRequestedWidth(width, stat.AvailableWidth)
	brackets := s.components[iLbound].width + s.components[iRbound].width
	if width < brackets {
		return
	}
	// don't count brackets as progress
	width -= brackets

	w.Write(s.components[iLbound].bytes)
	defer w.Write(s.components[iRbound].bytes)

	curWidth := int(internal.PercentageRound(stat.Total, stat.Current, width))
	refWidth, filled := 0, curWidth
	filling := make([][]byte, 0, curWidth)

	if curWidth > 0 && curWidth != width {
		tipFrame := s.tip.frames[s.tip.count%uint(len(s.tip.frames))]
		filling = append(filling, tipFrame.bytes)
		curWidth -= tipFrame.width
		s.tip.count++
	}

	if stat.Refill > 0 && curWidth > 0 {
		refWidth = int(internal.PercentageRound(stat.Total, int64(stat.Refill), width))
		if refWidth > curWidth {
			refWidth = curWidth
		}
		curWidth -= refWidth
	}

	for curWidth > 0 && curWidth >= s.components[iFiller].width {
		filling = append(filling, s.components[iFiller].bytes)
		curWidth -= s.components[iFiller].width
		if s.components[iFiller].width == 0 {
			break
		}
	}

	for refWidth > 0 && refWidth >= s.components[iRefiller].width {
		filling = append(filling, s.components[iRefiller].bytes)
		refWidth -= s.components[iRefiller].width
		if s.components[iRefiller].width == 0 {
			break
		}
	}

	filled -= curWidth + refWidth
	padWidth := width - filled
	padding := make([][]byte, 0, padWidth)
	for padWidth > 0 && padWidth >= s.components[iPadding].width {
		padding = append(padding, s.components[iPadding].bytes)
		padWidth -= s.components[iPadding].width
		if s.components[iPadding].width == 0 {
			break
		}
	}

	for padWidth > 0 {
		padding = append(padding, []byte("…"))
		padWidth--
	}

	s.flush(w, filling, padding)
}

func flush(dst io.Writer, filling, padding [][]byte) {
	for i := len(filling) - 1; i >= 0; i-- {
		dst.Write(filling[i])
	}
	for i := 0; i < len(padding); i++ {
		dst.Write(padding[i])
	}
}