Skip to content

Commit

Permalink
Small rework to read wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Dec 31, 2021
1 parent 6cc27e7 commit b00d16f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
5 changes: 2 additions & 3 deletions internal/transport/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (c *Client) NewReceive(code string, pathname chan string, progress *util.Pr
}

progress.Max = float64(msg.TransferBytes64)
contents := io.TeeReader(msg, progress)
contents := util.TeeReader(msg, progress)

if msg.Type == wormhole.TransferText {
pathname <- "text"
Expand Down Expand Up @@ -119,8 +119,7 @@ func (c *Client) NewReceive(code string, pathname chan string, progress *util.Pr
return err
}

// TODO: Progress sometimes stops at 99%. Is it the offset that isn't accounted for?
progress.Done()
progress.Done() // Workaround for progress sometimes stopping at 99%.

return
}
36 changes: 22 additions & 14 deletions internal/util/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,32 @@ import (
"io"
)

type teeReaderAt struct {
r io.ReaderAt
w io.Writer
type teeReader struct {
readat io.ReaderAt
read io.Reader
prog *ProgressBar
}

// ReadAt wraps the reader and calls write on it.
func (t *teeReaderAt) ReadAt(p []byte, off int64) (int, error) {
n, err := t.r.ReadAt(p, off)
if n > 0 {
if n, err := t.w.Write(p[:n]); err != nil {
return n, err
}
}
// ReadAt wraps the ReaderAt and updates the progress bar.
func (t *teeReader) ReadAt(p []byte, off int64) (int, error) {
n, err := t.readat.ReadAt(p, off)
t.prog.SetValue(t.prog.Value + float64(n))
return n, err
}

// Read wraps the Reader and updates the progress bar.
func (t *teeReader) Read(p []byte) (int, error) {
n, err := t.read.Read(p)
t.prog.SetValue(t.prog.Value + float64(n))
return n, err
}

// TeeReaderAt returns a wrapped ReaderAt that writes what is being read.
func TeeReaderAt(r io.ReaderAt, w io.Writer) io.ReaderAt {
return &teeReaderAt{r, w}
// TeeReaderAt returns a wrapped ReaderAt that updates the progress bar.
func TeeReaderAt(r io.ReaderAt, p *ProgressBar) io.ReaderAt {
return &teeReader{readat: r, prog: p}
}

// TeeReader returns a wrapped Reader that updates the progress bar.
func TeeReader(r io.Reader, p *ProgressBar) io.Reader {
return &teeReader{read: r, prog: p}
}

0 comments on commit b00d16f

Please sign in to comment.