I have the following code that uses a package to draw a progress bar
type tmpStruct struct {
}
func (t *tmpStruct) Write(p []byte) (n int, err error) {
fmt.Fprintf(os.Stdout, "%s", string(p))
return len(p), nil
}
func demoLoadingBarCount(maximumInt int) {
buf := tmpStruct{}
if nBuf, ok := interface{}(&buf).(io.Writer); ok {
bar := progressbar.NewOptions(
maximumInt,
progressbar.OptionSetTheme(progressbar.Theme{Saucer: "█", SaucerPadding: "-", BarStart: ">", BarEnd: "<"}),
progressbar.OptionSetWidth(100),
progressbar.OptionSetWriter(nBuf),
)
for i := 0; i < maximumInt; i++ {
bar.Add(1)
time.Sleep(10 * time.Millisecond)
}
}
}
All works, except there is no new line at the end as you can see here
I can't add a new line character in the Write function as that will cause it to new line after every byte pushed to the writer. Is there a neat way can I do this?
EDIT: I want the new line after the progress bar and before the next line prints out
The simple answer to the question you've asked is simply to print an additional newline after the progress bar is complete:
func demoLoadingBarCount(maximumInt int) {
buf := &tmpStruct{}
bar := progressbar.NewOptions(
maximumInt,
progressbar.OptionSetTheme(progressbar.Theme{Saucer: "█", SaucerPadding: "-", BarStart: ">", BarEnd: "<"}),
progressbar.OptionSetWidth(100),
progressbar.OptionSetWriter(buf),
)
for i := 0; i < maximumInt; i++ {
bar.Add(1)
time.Sleep(10 * time.Millisecond)
}
fmt.Fprintf(buf, "\n") // <---- Add this
}
Although your comments indicate that this is problematic, but you haven't explained how. If you update your question to explain why this is a problem, perhaps a better solution can follow.