From 96f4a2195db12fe1ee61698cf6001cd1a8a5a65f Mon Sep 17 00:00:00 2001 From: Nick Gauthier Date: Thu, 25 Mar 2010 14:35:21 -0400 Subject: [PATCH] added progressbar listener and made it the new default --- hydra.gemspec | 1 + lib/hydra.rb | 2 +- lib/hydra/listener/progress_bar.rb | 48 ++++++++++++++++++++++++++++++ lib/hydra/tasks.rb | 2 +- 4 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 lib/hydra/listener/progress_bar.rb diff --git a/hydra.gemspec b/hydra.gemspec index 98c9f5f..d2ed6a6 100644 --- a/hydra.gemspec +++ b/hydra.gemspec @@ -34,6 +34,7 @@ Gem::Specification.new do |s| "lib/hydra/listener/abstract.rb", "lib/hydra/listener/minimal_output.rb", "lib/hydra/listener/notifier.rb", + "lib/hydra/listener/progress_bar.rb", "lib/hydra/listener/report_generator.rb", "lib/hydra/master.rb", "lib/hydra/message.rb", diff --git a/lib/hydra.rb b/lib/hydra.rb index 1d81077..2b61522 100644 --- a/lib/hydra.rb +++ b/lib/hydra.rb @@ -11,5 +11,5 @@ require 'hydra/listener/abstract' require 'hydra/listener/minimal_output' require 'hydra/listener/report_generator' require 'hydra/listener/notifier' - +require 'hydra/listener/progress_bar' diff --git a/lib/hydra/listener/progress_bar.rb b/lib/hydra/listener/progress_bar.rb new file mode 100644 index 0000000..53c90ec --- /dev/null +++ b/lib/hydra/listener/progress_bar.rb @@ -0,0 +1,48 @@ +module Hydra #:nodoc: + module Listener #:nodoc: + # Output a progress bar as files are completed + class ProgressBar < Hydra::Listener::Abstract + # Store the total number of files + def testing_begin(files) + @total_files = files.size + @files_completed = 0 + @test_output = "" + @errors = false + render_progress_bar + end + + # Increment completed files count and update bar + def file_end(file, output) + unless output == '.' + @output.write "\r#{' '*60}\r#{output}\n" + @errors = true + end + @files_completed += 1 + render_progress_bar + end + + # Break the line + def testing_end + render_progress_bar + @output.write "\n" + end + + private + + def render_progress_bar + width = 30 + complete = ((@files_completed.to_f / @total_files.to_f) * width).to_i + @output.write "\r" # move to beginning + @output.write 'Progress [' + @output.write @errors ? "\033[1;31m" : "\033[1;32m" + complete.times{@output.write '#'} + @output.write '>' + (width-complete).times{@output.write ' '} + @output.write "\033[0m" + @output.write "] #{@files_completed}/#{@total_files}" + @output.flush + end + end + end +end + diff --git a/lib/hydra/tasks.rb b/lib/hydra/tasks.rb index 666c421..295749b 100644 --- a/lib/hydra/tasks.rb +++ b/lib/hydra/tasks.rb @@ -68,7 +68,7 @@ module Hydra #:nodoc: @files = [] @verbose = false @autosort = true - @listeners = [Hydra::Listener::MinimalOutput.new] + @listeners = [Hydra::Listener::ProgressBar.new] yield self if block_given?