diff --git a/durationformat.go b/durationformat.go new file mode 100644 index 0000000..ac3ccd7 --- /dev/null +++ b/durationformat.go @@ -0,0 +1,46 @@ +package gwp + +import ( + "fmt" + "time" +) + +func fmtDuration(d time.Duration) string { + d = d.Round(time.Second) + + days := d / time.Hour / 24 + d -= days * 24 * time.Hour + + hours := d / time.Hour + d -= hours * time.Hour + + minites := d / time.Minute + d -= minites * time.Minute + + seconds := d / time.Second + d -= seconds * time.Second + + var resultStr string + + if days > 0 { + resultStr += fmt.Sprintf("%3dd", days) + } else { + resultStr += " " + } + + if hours > 0 { + resultStr += fmt.Sprintf(" %2dh", hours) + } else { + resultStr += " " + } + + if minites > 0 { + resultStr += fmt.Sprintf(" %2dm", minites) + } else { + resultStr += " " + } + + resultStr += fmt.Sprintf(" %2ds", seconds) + + return resultStr +} diff --git a/durationformat_test.go b/durationformat_test.go new file mode 100644 index 0000000..ef16c1e --- /dev/null +++ b/durationformat_test.go @@ -0,0 +1,16 @@ +package gwp + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestFmtDuration(t *testing.T) { + assert.Equal(t, " 1s", fmtDuration(time.Second)) + assert.Equal(t, " 1m 0s", fmtDuration(time.Minute)) + assert.Equal(t, " 1h 0s", fmtDuration(time.Hour)) + assert.Equal(t, " 1d 0s", fmtDuration(time.Hour*24)) + assert.Equal(t, "365d 0s", fmtDuration(time.Hour*24*365)) +} diff --git a/gwp.go b/gwp.go index 617c98b..a9aae4b 100644 --- a/gwp.go +++ b/gwp.go @@ -101,7 +101,7 @@ func (workerPool *WorkerPool) printProgress() { } if workerPool.EstimateCount > 0 && workerPool.currentSpeed > 0 { fmt.Fprintf(os.Stderr, " ETA: %s at %.2f rps", - time.Second*time.Duration(float64(workerPool.EstimateCount-workerPool.processedCount)/workerPool.currentSpeed), workerPool.currentSpeed) + fmtDuration(time.Second*time.Duration(float64(workerPool.EstimateCount-workerPool.processedCount)/workerPool.currentSpeed)), workerPool.currentSpeed) } fmt.Fprint(os.Stderr, endLine) } @@ -121,7 +121,7 @@ func (workerPool *WorkerPool) CloseAndWait() { workerPool.printProgress() } -// ErrorCount returns total error count +// ErrorCount returns total error count. func (workerPool *WorkerPool) ErrorCount() int { return workerPool.errorCount }