From 81bff1827c6d71a37d6d800ca23d7f8593ba5f87 Mon Sep 17 00:00:00 2001 From: Dario Lombardo Date: Mon, 22 Apr 2024 16:34:35 +0200 Subject: [PATCH] print: add elapsed + eta. --- src/progress.cr | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/progress.cr b/src/progress.cr index 8a74751..b980aac 100644 --- a/src/progress.cr +++ b/src/progress.cr @@ -7,6 +7,7 @@ class ProgressBar def initialize(@total = 100, @step = 1, @width = 100, @complete = "\u2593", @incomplete = "\u2591", use_stdout = false) @current = 0.0 @output_stream = use_stdout ? STDOUT : STDERR + @start = Time.monotonic end def inc @@ -54,7 +55,7 @@ class ProgressBar private def print(percent) @output_stream.flush - @output_stream.print "[#{@complete * position}#{@incomplete * (@width - position)}] #{percent} % \r" + @output_stream.print "[#{@complete * position}#{@incomplete * remaining}] #{percent} % Elapsed: #{round(elapsed)} ETA: #{eta}\r" @output_stream.flush @output_stream.print "\n" if done? end @@ -62,4 +63,24 @@ class ProgressBar private def position ((@current.to_f * @width.to_f) / @total).to_i end + + private def remaining + @width - position + end + + private def elapsed + Time.monotonic - @start + end + + private def round(t) + Time::Span.new(seconds: t.total_seconds.to_i, nanoseconds: 0) + end + + private def eta + if position > 0 + round(elapsed * (remaining.to_f / position)) + else + "--" + end + end end