WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit c3ab5b4

Browse files
authored
Merge pull request #88 from Kirides/master
add option to support ANSI escape code compatible environments
2 parents 953dc78 + b404f80 commit c3ab5b4

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

progressbar.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ type config struct {
9393
invisible bool
9494

9595
onCompletion func()
96+
97+
// whether the render function should make use of ANSI codes to reduce console I/O
98+
useANSICodes bool
9699
}
97100

98101
// Theme defines the elements of the bar
@@ -229,6 +232,15 @@ func OptionShowBytes(val bool) Option {
229232
}
230233
}
231234

235+
// OptionUseANSICodes will use more optimized terminal i/o.
236+
//
237+
// Only useful in environments with support for ANSI escape sequences.
238+
func OptionUseANSICodes(val bool) Option {
239+
return func(p *ProgressBar) {
240+
p.config.useANSICodes = val
241+
}
242+
}
243+
232244
var defaultTheme = Theme{Saucer: "█", SaucerPadding: " ", BarStart: "|", BarEnd: "|"}
233245

234246
// NewOptions constructs a new instance of ProgressBar, with any options you specify
@@ -496,10 +508,12 @@ func (p *ProgressBar) render() error {
496508
return nil
497509
}
498510

499-
// first, clear the existing progress bar
500-
err := clearProgressBar(p.config, p.state)
501-
if err != nil {
502-
return err
511+
if !p.config.useANSICodes {
512+
// first, clear the existing progress bar
513+
err := clearProgressBar(p.config, p.state)
514+
if err != nil {
515+
return err
516+
}
503517
}
504518

505519
// check if the progress bar is finished
@@ -514,6 +528,13 @@ func (p *ProgressBar) render() error {
514528
}
515529
}
516530
if p.state.finished {
531+
// when using ANSI codes we don't pre-clean the current line
532+
if p.config.useANSICodes {
533+
err := clearProgressBar(p.config, p.state)
534+
if err != nil {
535+
return err
536+
}
537+
}
517538
return nil
518539
}
519540

@@ -724,11 +745,18 @@ func renderProgressBar(c config, s state) (int, error) {
724745
// character count of the string, as some runes span multiple characters.
725746
// see https://stackoverflow.com/a/12668840/2733724
726747
stringWidth := runewidth.StringWidth(cleanString)
727-
748+
if c.useANSICodes {
749+
// append the "clear rest of line" ANSI escape sequence
750+
str = str + "\033[0K"
751+
}
728752
return stringWidth, writeString(c, str)
729753
}
730754

731755
func clearProgressBar(c config, s state) error {
756+
if c.useANSICodes {
757+
// write the "clear current line" ANSI escape sequence
758+
return writeString(c, "\033[2K\r")
759+
}
732760
// fill the current line with enough spaces
733761
// to overwrite the progress bar and jump
734762
// back to the beginning of the line

0 commit comments

Comments
 (0)