Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft of a fix for proper behaviour on terminal window downsize #112

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 31 additions & 9 deletions progressbar.go
Expand Up @@ -47,9 +47,10 @@ type state struct {
counterNumSinceLast int64
counterLastTenRates []float64

maxLineWidth int
currentBytes float64
finished bool
prevLineWidth int
maxLineWidth int
currentBytes float64
finished bool

rendered string
}
Expand Down Expand Up @@ -565,12 +566,10 @@ func (p *ProgressBar) render() error {
return nil
}

if !p.config.useANSICodes {
// first, clear the existing progress bar
err := clearProgressBar(p.config, p.state)
if err != nil {
return err
}
// first, clear the existing progress bar
err := clearProgressBar(p.config, p.state)
if err != nil {
return err
}

// check if the progress bar is finished
Expand Down Expand Up @@ -741,6 +740,7 @@ func renderProgressBar(c config, s *state) (int, error) {
}
}

s.prevLineWidth = width
c.width = width - getStringWidth(c, c.description, true) - 14 - len(bytesString) - len(leftBrac) - len(rightBrac)
s.currentSaucerSize = int(float64(s.currentPercent) / 100.0 * float64(c.width))
}
Expand Down Expand Up @@ -821,6 +821,28 @@ func renderProgressBar(c config, s *state) (int, error) {

func clearProgressBar(c config, s state) error {
if c.useANSICodes {
if c.fullWidth && !c.ignoreLength {
width, _, err := terminal.GetSize(int(os.Stdout.Fd()))

// if a terminal window was downsized just now, we need to clear more than one line
if err == nil && width < s.prevLineWidth {
stringsToClear := s.prevLineWidth / width
if s.prevLineWidth%width != 0 {
stringsToClear++
}

var clearSequence strings.Builder

for i := 0; i < stringsToClear-1; i++ {
clearSequence.WriteString("\033[2K\r")
clearSequence.WriteString("\033[1A\r")
}
clearSequence.WriteString("\033[2K\r")

return writeString(c, clearSequence.String())
}
}

// write the "clear current line" ANSI escape sequence
return writeString(c, "\033[2K\r")
}
Expand Down