MCPcopy
hub / github.com/pterm/pterm / getString

Method getString

progressbar_printer.go:205–265  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

203}
204
205func (p *ProgressbarPrinter) getString() string {
206 if !p.IsActive {
207 return ""
208 }
209 if p.TitleStyle == nil {
210 p.TitleStyle = NewStyle()
211 }
212 if p.BarStyle == nil {
213 p.BarStyle = NewStyle()
214 }
215 if p.Total == 0 {
216 return ""
217 }
218
219 var before string
220 var after string
221 var width int
222
223 if p.MaxWidth <= 0 {
224 width = GetTerminalWidth()
225 } else if GetTerminalWidth() < p.MaxWidth {
226 width = GetTerminalWidth()
227 } else {
228 width = p.MaxWidth
229 }
230
231 if p.ShowTitle {
232 before += p.TitleStyle.Sprint(p.Title) + " "
233 }
234 if p.ShowCount {
235 padding := 1 + int(math.Log10(float64(p.Total)))
236 before += Gray("[") + LightWhite(fmt.Sprintf("%0*d", padding, p.Current)) + Gray("/") + LightWhite(p.Total) + Gray("]") + " "
237 }
238
239 after += " "
240
241 if p.ShowPercentage {
242 currentPercentage := int(internal.PercentageRound(float64(int64(p.Total)), float64(int64(p.Current))))
243 decoratorCurrentPercentage := color.RGB(NewRGB(255, 0, 0).Fade(0, float32(p.Total), float32(p.Current), NewRGB(0, 255, 0)).GetValues()).
244 Sprintf("%3d%%", currentPercentage)
245 after += decoratorCurrentPercentage + " "
246 }
247 if p.ShowElapsedTime {
248 after += "| " + p.parseElapsedTime()
249 }
250
251 barMaxLength := width - len(RemoveColorFromString(before)) - len(RemoveColorFromString(after)) - 1
252
253 barCurrentLength := (p.Current * barMaxLength) / p.Total
254 var barFiller string
255 if barMaxLength-barCurrentLength > 0 {
256 barFiller = strings.Repeat(p.BarFiller, barMaxLength-barCurrentLength)
257 }
258
259 bar := barFiller
260 if barCurrentLength > 0 {
261 bar = p.BarStyle.Sprint(strings.Repeat(p.BarCharacter, barCurrentLength)+p.LastCharacter) + bar
262 }

Callers 1

updateProgressMethod · 0.95

Calls 10

parseElapsedTimeMethod · 0.95
PercentageRoundFunction · 0.92
NewStyleFunction · 0.85
GetTerminalWidthFunction · 0.85
NewRGBFunction · 0.85
RemoveColorFromStringFunction · 0.85
GetValuesMethod · 0.80
FadeMethod · 0.80
SprintMethod · 0.65
SprintfMethod · 0.65

Tested by

no test coverage detected