Skip to content

Cannot get ProxyWriter to show bytes written #154

@AlexThurston

Description

@AlexThurston

When using the ProxyWriter, it doesn't seem as though CurrentKibiByte is being displayed or updated correctly.

Using the following example:

package main

import (
	"fmt"
	"io"
	"net/http"

	"github.com/vbauerster/mpb/v8"
	"github.com/vbauerster/mpb/v8/decor"
)

func main() {
	url := "https://httpbin.org/drip?duration=10&numbytes=20&delay=0&code=200"
	resp, err := http.Get(url)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		panic(fmt.Sprintf("bad status code: %d", resp.StatusCode))
	}

	p := mpb.New(mpb.WithWidth(4))
	bar := p.New(-1,
		mpb.NopStyle(),
		mpb.PrependDecorators(decor.CurrentKibiByte("% .2f")),
		mpb.AppendDecorators(decor.Elapsed(decor.ET_STYLE_GO)),
	)

	pw := bar.ProxyWriter(io.Discard)
	_, err = io.Copy(pw, resp.Body)
	if err != nil {
		panic(err)
	}
}

The output is as follows:

0.00 b  10s

Notice that it continues to show 0.00b

Interestingly (and without totally understanding the code base), I looked at proxywriter.go in function newProxyWriter and noticed lines 62-64:

pw := proxyWriter{wc, b}
if _, ok := w.(io.ReaderFrom); ok {
    return proxyReaderFrom{pw}
}
return pw

If I remove that check so that the pw is returned, the bytes are displayed correctly.

For example:

19.00 b  10s

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions