Sha256: ef3c723556487aa76bd4b9b3c58097e7a54dd5d1a44820aeebdd9b95f7c5ce87
Contents?: true
Size: 1.76 KB
Versions: 5
Compression:
Stored size: 1.76 KB
Contents
module Squealer class ProgressBar @@progress_bar = nil def self.new(*args) if @@progress_bar nil else @@progress_bar = super end end def initialize(total) @total = total @ticks = 0 @progress_bar_width = 50 @count_width = total.to_s.size end def start @start_time = Time.new @emitter = start_emitter if total > 0 self end def finish @end_time = Time.new @emitter.wakeup.join if @emitter @@progress_bar = nil end def tick @ticks += 1 end private def start_emitter Thread.new do emit sleep(1) and emit until done? end end def emit format = "\r[%-#{progress_bar_width}s] %#{count_width}i/%i (%i%%)" console.print format % [progress_markers, ticks, total, percentage] emit_final if done? end def emit_final console.puts console.puts "Start: #{start_time}" console.puts "End: #{end_time}" console.puts "Duration: #{duration}" end def done? ticks >= total || end_time end def start_time @start_time end def end_time @end_time end def ticks @ticks end def total @total end def percentage ((ticks.to_f / total) * 100).floor end def progress_markers "=" * ((ticks.to_f / total) * progress_bar_width).floor end def console $stderr end def progress_bar_width @progress_bar_width end def count_width @count_width end def total_time @end_time - @start_time end def duration duration = Time.at(total_time).utc duration.strftime("%H:%M:%S.#{duration.usec}") end end end
Version data entries
5 entries across 5 versions & 1 rubygems