Skip to content

Commit d6d19c1

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

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/progress.cr

Lines changed: 24 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
@@ -53,13 +54,35 @@ class ProgressBar
5354
end
5455

5556
private def print(percent)
57+
# f = Time::Format.new("%a")
58+
# pp elapsed.to_s("%Y")
5659
@output_stream.flush
57-
@output_stream.print "[#{@complete * position}#{@incomplete * (@width - position)}] #{percent} % \r"
60+
@output_stream.print "[#{@complete * position}#{@incomplete * remaining}] #{percent} % Elapsed: #{round(elapsed)} ETA: #{eta}\r"
5861
@output_stream.flush
5962
@output_stream.print "\n" if done?
6063
end
6164

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

0 commit comments

Comments
 (0)