Skip to content

Commit

Permalink
[progress] Migrate to 'passthru' package
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Jan 22, 2024
1 parent 0073e60 commit 231f969
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 160 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### 12.97.0

* `[passthru]` Added package with pass-thru reader and writer
* `[progress]` Migrate to `passthru` package
* `[fmtutil/table]` Improved borders and separators rendering
* `[usage]` Improved environment info output
* `[spinner]` Improved message rendering
Expand Down
73 changes: 0 additions & 73 deletions progress/pass_thru_calc.go

This file was deleted.

81 changes: 18 additions & 63 deletions progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/essentialkaos/ek/v12/fmtc"
"github.com/essentialkaos/ek/v12/fmtutil"
"github.com/essentialkaos/ek/v12/mathutil"
"github.com/essentialkaos/ek/v12/passthru"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -54,11 +55,11 @@ type Bar struct {
buffer string

ticker *time.Ticker
passThruCalc *PassThruCalc
passThruCalc *passthru.Calculator
phCounter int

reader *passThruReader
writer *passThruWriter
reader *passthru.Reader
writer *passthru.Writer

mu *sync.RWMutex
}
Expand Down Expand Up @@ -91,18 +92,6 @@ type Settings struct {

// ////////////////////////////////////////////////////////////////////////////////// //

type passThruReader struct {
io.Reader
bar *Bar
}

type passThruWriter struct {
io.Writer
bar *Bar
}

// ////////////////////////////////////////////////////////////////////////////////// //

// DefaultSettings is default progress bar settings
var DefaultSettings = Settings{
RefreshRate: 100 * time.Millisecond,
Expand Down Expand Up @@ -161,7 +150,7 @@ func (b *Bar) Start() error {
b.ticker = time.NewTicker(b.settings.RefreshRate)

if b.total > 0 {
b.passThruCalc = NewPassThruCalc(
b.passThruCalc = passthru.NewCalculator(
b.total, math.Max(1.0, float64(b.settings.WindowSizeSec)),
)
}
Expand Down Expand Up @@ -243,7 +232,7 @@ func (b *Bar) SetTotal(v int64) {
b.mu.Lock()

if b.passThruCalc == nil {
b.passThruCalc = NewPassThruCalc(
b.passThruCalc = passthru.NewCalculator(
v, math.Max(1.0, float64(b.settings.WindowSizeSec)),
)
} else {
Expand Down Expand Up @@ -321,40 +310,30 @@ func (b *Bar) IsStarted() bool {
return b.started
}

// Reader creates and returns pass thru proxy reader
func (b *Bar) Reader(r io.Reader) io.Reader {
// Reader creates and returns pass thru-proxy reader
func (b *Bar) Reader(r io.ReadCloser) io.ReadCloser {
if b == nil {
return nil
}

if b.reader != nil {
b.reader.Reader = r
} else {
b.reader = &passThruReader{
Reader: r,
bar: b,
}
}
pr := passthru.NewReader(r, b.total)
pr.Update = b.Add
b.reader = pr

return b.reader
return pr
}

// Writer creates and returns pass thru proxy reader
func (b *Bar) Writer(w io.Writer) io.Writer {
// Writer creates and returns pass-thru proxy reader
func (b *Bar) Writer(w io.WriteCloser) io.WriteCloser {
if b == nil {
return nil
}

if b.writer != nil {
b.writer.Writer = w
} else {
b.writer = &passThruWriter{
Writer: w,
bar: b,
}
}
pw := passthru.NewWriter(w, b.total)
pw.Update = b.Add
b.writer = pw

return b.writer
return pw
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -679,30 +658,6 @@ func (s Settings) Validate() error {

// ////////////////////////////////////////////////////////////////////////////////// //

// Read reads data and updates progress bar
func (r *passThruReader) Read(p []byte) (int, error) {
n, err := r.Reader.Read(p)

if n > 0 {
r.bar.Add(n)
}

return n, err
}

// Write writes data and updates progress bar
func (w *passThruWriter) Write(p []byte) (int, error) {
n, err := w.Writer.Write(p)

if n > 0 {
w.bar.Add(n)
}

return n, err
}

// ////////////////////////////////////////////////////////////////////////////////// //

// getPrettyCTSize returns formatted current/total size text
func getPrettyCTSize(current, total int64) (string, string, string) {
var mod float64
Expand Down
32 changes: 8 additions & 24 deletions progress/progress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,30 +322,6 @@ func (s *ProgressSuite) TestRemainingRender(c *C) {
c.Assert(sz, Equals, 5)
}

func (s *ProgressSuite) TestPassThruCalc(c *C) {
ptc := NewPassThruCalc(1000, 0.5)

c.Assert(ptc, NotNil)

ptc.SetTotal(20000)

c.Assert(ptc.total, Equals, float64(20000))

sp, dr := ptc.Calculate(1)

time.Sleep(time.Second)

sp, dr = ptc.Calculate(2)

c.Assert(sp, Not(Equals), 0.0)
c.Assert(dr, Not(Equals), time.Duration(0))

sp2, dr2 := ptc.Calculate(2)

c.Assert(sp2, Equals, sp)
c.Assert(dr2, Equals, dr)
}

func (s *ProgressSuite) TestAux(c *C) {
ct, tt, lt := getPrettyCTSize(1, 15)

Expand Down Expand Up @@ -428,6 +404,14 @@ func (r *DummyReader) Read(p []byte) (int, error) {
return 100, nil
}

func (r *DummyReader) Close() error {
return nil
}

func (w *DummyWriter) Write(p []byte) (int, error) {
return 100, nil
}

func (r *DummyWriter) Close() error {
return nil
}

0 comments on commit 231f969

Please sign in to comment.