lib/progressbar.rb in progressbar_zobar-0.9.2.1 vs lib/progressbar.rb in progressbar_zobar-0.21.0.1
- old
+ new
@@ -8,11 +8,11 @@
# You can redistribute it and/or modify it under the terms
# of Ruby's license.
#
class ProgressBar
- VERSION = "0.9"
+ VERSION = "0.21.0.1"
def initialize (title, total, out = STDERR)
@title = title
@prev_total = @total = total
@out = out
@@ -26,10 +26,14 @@
@title_width = 14
@format = "%-#{@title_width}s %3d%% %s %s"
@format_arguments = [:title, :percentage, :bar, :stat]
clear
show
+ if block_given?
+ yield(self)
+ finish
+ end
end
attr_reader :title
attr_reader :current
attr_reader :total
@@ -51,10 +55,14 @@
def fmt_stat
if @finished_p then elapsed else eta end
end
+ def fmt_stat_for_long_run
+ if @finished_p then elapsed else eta_running_average end
+ end
+
def fmt_stat_for_file_transfer
if @finished_p then
sprintf("%s %s %s", bytes, transfer_rate, elapsed)
else
sprintf("%s %s %s", bytes, transfer_rate, eta)
@@ -89,24 +97,52 @@
def format_time (t)
t = t.to_i
sec = t % 60
min = (t / 60) % 60
hour = t / 3600
- sprintf("%02d:%02d:%02d", hour, min, sec);
+ sprintf("% 3d:%02d:%02d", hour, min, sec);
end
# ETA stands for Estimated Time of Arrival.
def eta
if @current == 0
"ETA: --:--:--"
else
elapsed = Time.now - @start_time
eta = elapsed * @total / @current - elapsed;
- sprintf("ETA: %s", format_time(eta))
+ sprintf("ETA: %s", format_time(eta))
end
end
+ # Compute ETA with running average (better suited to long running tasks)
+ def eta_running_average
+ now = Time.now
+
+ # update throughput running average
+ if @total > 0 && @eta_previous && @eta_previous_time
+ current_elapsed = @current - @eta_previous
+ alpha = 0.9 ** current_elapsed
+ current_progress = 1.0 * current_elapsed
+ current_throughput = current_progress / (now - @eta_previous_time)
+ if @eta_throughput
+ @eta_throughput = @eta_throughput * alpha + current_throughput * (1-alpha)
+ else
+ @eta_throughput = current_throughput
+ end
+ end
+
+ @eta_previous = @current
+ @eta_previous_time = now
+
+ if @eta_throughput && @eta_throughput > 0
+ eta = (@total - @current) / @eta_throughput;
+ sprintf("ETA: %s", format_time(eta))
+ else
+ "ETA: --:--:--"
+ end
+ end
+
def elapsed
elapsed = Time.now - @start_time
sprintf("Time: %s", format_time(elapsed))
end
@@ -122,19 +158,25 @@
end
end
DEFAULT_WIDTH = 80
def get_term_width
- if ENV['COLUMNS'] =~ /^\d+$/
+ term_width = if ENV['COLUMNS'] =~ /^\d+$/
ENV['COLUMNS'].to_i
elsif (RUBY_PLATFORM =~ /java/ || (!STDIN.tty? && ENV['TERM'])) && shell_command_exists?('tput')
`tput cols`.to_i
elsif STDIN.tty? && shell_command_exists?('stty')
`stty size`.scan(/\d+/).map { |s| s.to_i }[1]
else
DEFAULT_WIDTH
end
+
+ if term_width > 0
+ term_width
+ else
+ DEFAULT_WIDTH
+ end
rescue
DEFAULT_WIDTH
end
def shell_command_exists?(command)
@@ -199,9 +241,13 @@
@finished_p
end
def file_transfer_mode
@format_arguments = [:title, :percentage, :bar, :stat_for_file_transfer]
+ end
+
+ def long_running
+ @format_arguments = [:title, :percentage, :bar, :stat_for_long_run]
end
def format= (format)
@format = format
end