Skip to content

Commit 81bff18

Browse files
committed
print: add elapsed + eta.
1 parent 5eb8c59 commit 81bff18

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/progress.cr

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class ProgressBar
77
def initialize(@total = 100, @step = 1, @width = 100, @complete = "\u2593", @incomplete = "\u2591", use_stdout = false)
88
@current = 0.0
99
@output_stream = use_stdout ? STDOUT : STDERR
10+
@start = Time.monotonic
1011
end
1112

1213
def inc
@@ -54,12 +55,32 @@ class ProgressBar
5455

5556
private def print(percent)
5657
@output_stream.flush
57-
@output_stream.print "[#{@complete * position}#{@incomplete * (@width - position)}] #{percent} % \r"
58+
@output_stream.print "[#{@complete * position}#{@incomplete * remaining}] #{percent} % Elapsed: #{round(elapsed)} ETA: #{eta}\r"
5859
@output_stream.flush
5960
@output_stream.print "\n" if done?
6061
end
6162

6263
private def position
6364
((@current.to_f * @width.to_f) / @total).to_i
6465
end
66+
67+
private def remaining
68+
@width - position
69+
end
70+
71+
private def elapsed
72+
Time.monotonic - @start
73+
end
74+
75+
private def round(t)
76+
Time::Span.new(seconds: t.total_seconds.to_i, nanoseconds: 0)
77+
end
78+
79+
private def eta
80+
if position > 0
81+
round(elapsed * (remaining.to_f / position))
82+
else
83+
"--"
84+
end
85+
end
6586
end

0 commit comments

Comments
 (0)