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

Multiple blank lines in the result of executing the example #102

Open
TCP404 opened this issue Jun 24, 2021 · 3 comments
Open

Multiple blank lines in the result of executing the example #102

TCP404 opened this issue Jun 24, 2021 · 3 comments

Comments

@TCP404
Copy link

TCP404 commented Jun 24, 2021

I ran this two example

bar := progressbar.Default(100)
for i := 0; i < 100; i++ {
    bar.Add(1)
    time.Sleep(40 * time.Millisecond)
}
req, _ := http.NewRequest("GET", "https://dl.google.com/go/go1.14.2.src.tar.gz", nil)
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()

f, _ := os.OpenFile("go1.14.2.src.tar.gz", os.O_CREATE|os.O_WRONLY, 0644)
defer f.Close()

bar := progressbar.DefaultBytes(
    resp.ContentLength,
    "downloading",
)
io.Copy(io.MultiWriter(f, bar), resp.Body)

and I got many blank lines

$ go run main.go











100% |█████████████████████████████████████| (100/100, 25bit/s) 
$ go run main.go







downloading 100% |██████████████████████████████████████████| (21/21 MB, 2.616 MB/s)

This problem occurred on both Manjaro and Win10.

When I modify the clearProgressBar()

func clearProgressBar(c config, s state) error {
	if c.useANSICodes {
		// write the "clear current line" ANSI escape sequence
		return writeString(c, "\033[2K\r")
	}
	// fill the empty content
	// to overwrite the progress bar and jump
	// back to the beginning of the line
	str := fmt.Sprintf("\r%s\r", strings.Repeat(" ", s.maxLineWidth))
	return writeString(c, str)
	// the following does not show correctly if the previous line is longer than subsequent line
	// return writeString(c, "\r")
}

to

func clearProgressBar(c config, s state) error {
	if c.useANSICodes {
		// write the "clear current line" ANSI escape sequence
		return writeString(c, "\033[2K\r")
	}
	// fill the empty content
	// to overwrite the progress bar and jump
	// back to the beginning of the line
	// str := fmt.Sprintf("\r%s\r", strings.Repeat(" ", s.maxLineWidth))
	// return writeString(c, str)
	// the following does not show correctly if the previous line is longer than subsequent line
	return writeString(c, "\r")
}

Done!
It's back to normal.

$ go run main.go
100% |█████████████████████████████████████| (100/100, 25bit/s) 

Now my problem was resolved. But I don't know whether it is a bug.

@TCP404 TCP404 changed the title The outcomt of the Example function have mulitply empty lines. Multiple blank lines in the result of executing the example Jun 24, 2021
@zhao1911
Copy link

zhao1911 commented Jan 14, 2022

I run into the same problem on win10. It works fine on macos.

I modify the clearProgressBar()

 func clearProgressBar(c config, s state) error {
 	if c.useANSICodes {
 		// write the "clear current line" ANSI escape sequence
 		return writeString(c, "\033[2K\r")
 	}
	// fill the empty content
 	// to overwrite the progress bar and jump
 	// back to the beginning of the line
 	str := fmt.Sprintf("\r%s\r", strings.Repeat(" ", s.maxLineWidth))
 	return writeString(c, str)
	// the following does not show correctly if the previous line is longer than subsequent line
 	// return writeString(c, "\r")
 }

to

func clearProgressBar(c config, s state) error {
	if c.useANSICodes {
		// write the "clear current line" ANSI escape sequence
		return writeString(c, "\033[2K\r")
	}
	fmt.Print("\x0D\x1B[2K")
        return nil
}

Then it works on both Win10 and mac.

@nicoxiang
Copy link

Same to me on Win11 Windows Terminal ( PowerShell ), fix it by set OptionUseANSICodes(true)

@jeyrce
Copy link

jeyrce commented Jul 4, 2022

Same to me on macos 11 and centos7.5,works by @nicoxiang 's

Same to me on Win11 Windows Terminal ( PowerShell ), fix it by set OptionUseANSICodes(true)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants