Skip to content

Commit

Permalink
[passthru] Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Jan 22, 2024
1 parent 231f969 commit e31a3e4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
16 changes: 4 additions & 12 deletions passthru/passthru.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,13 @@ func (r *Reader) Read(p []byte) (int, error) {

n, err := r.r.Read(p)

if err != nil {
return n, err
}

atomic.AddInt64(&r.current, int64(n))

if r.Update != nil {
if r.Update != nil && n > 0 {
r.Update(n)
}

return n, nil
return n, err
}

// Current returns read amount of data
Expand Down Expand Up @@ -195,17 +191,13 @@ func (w *Writer) Write(p []byte) (int, error) {

n, err := w.w.Write(p)

if err != nil {
return n, err
}

atomic.AddInt64(&w.current, int64(n))

if w.Update != nil {
if w.Update != nil && n > 0 {
w.Update(n)
}

return n, nil
return n, err
}

// Current returns written amount of data
Expand Down
7 changes: 4 additions & 3 deletions passthru/passthru_spinner_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package passthru
package passthru_test

// ////////////////////////////////////////////////////////////////////////////////// //
// //
Expand All @@ -13,6 +13,7 @@ import (
"time"

"github.com/essentialkaos/ek/v12/fmtutil"
"github.com/essentialkaos/ek/v12/passthru"
"github.com/essentialkaos/ek/v12/path"
"github.com/essentialkaos/ek/v12/req"
"github.com/essentialkaos/ek/v12/spinner"
Expand All @@ -22,7 +23,7 @@ import (
type DLSpinner struct {
file string
lastUpdate time.Time
reader *Reader
reader *passthru.Reader
}

func Example() {
Expand All @@ -47,7 +48,7 @@ func Example() {
return
}

r := NewReader(resp.Body, resp.ContentLength)
r := passthru.NewReader(resp.Body, resp.ContentLength)
s := &DLSpinner{file: filename, reader: r}
s.reader.Update = s.Update

Expand Down

0 comments on commit e31a3e4

Please sign in to comment.