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

custom io.Writer output #36

Closed
amlwwalker opened this issue Jun 25, 2019 · 2 comments
Closed

custom io.Writer output #36

amlwwalker opened this issue Jun 25, 2019 · 2 comments

Comments

@amlwwalker
Copy link

Hey,
this is a nice little library :)
I am trying to implement my own io.Writer to output to. Eventually I want to output the progress bar to a logging area I have put together in a terminal UI using this library however as a test case I have put together this:

import "github.com/schollz/progressbar"

type tmpStruct struct {
}

func (t *tmpStruct) Write(p []byte) (n int, err error) {
	fmt.Fprintf(os.Stdout, string(p[:]))
	return len(p), nil
}

func demoLoadingBarCount(maximumInt int) {
	var buf tmpStruct

	if bufWriter, ok := interface{}(buf).(io.Writer); ok {
		bar := progressbar.NewOptions(
			maximumInt,
			progressbar.OptionSetTheme(progressbar.Theme{Saucer: "#", SaucerPadding: "-", BarStart: ">", BarEnd: "<"}),
			progressbar.OptionSetWidth(10),
			progressbar.OptionSetWriter(bufWriter),
		)
		for i := 0; i < maximumInt; i++ {
			bar.Add(1)
			time.Sleep(10 * time.Millisecond)
		}
	}
}

The above seems to compile fine, but I don't see the progress bar on the terminal. Eventually I will change the os.Stdout to the writer of the terminal UI but for now I want to understand why the above isn't showing the terminal. Any help greatly appreciated.

Thanks

@amlwwalker
Copy link
Author

Oh, woops.
My type assertion to an io.Writer is failing.... Not sure why, but need to look into that!

@jerblack
Copy link

FYI to anyone running into this later on. I experienced something similar. If you set a custom writer using progressbar.OptionSetWriter and set progressbar.OptionFullWidth, the progress bar is not visible. It doesn't look like this package is able to get the terminal width it needs to determine max string length if it's not using os.Stdout. Manually setting the width with progressbar.OptionSetWidth makes the progress bar visible again.

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

2 participants