From 00cf00018909367b79aea42cac16fc9ba6b1fd2a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 8 Apr 2010 12:44:25 -0700 Subject: [PATCH] Added a progress meter module/mixin since that'll be used elsewhere. --- lib/vagrant.rb | 2 +- lib/vagrant/downloaders/http.rb | 18 +++--------------- lib/vagrant/util/progress_meter.rb | 29 +++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 16 deletions(-) create mode 100644 lib/vagrant/util/progress_meter.rb diff --git a/lib/vagrant.rb b/lib/vagrant.rb index 7b55c9cff..414aaa8e7 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -8,7 +8,7 @@ PROJECT_ROOT = File.join(libdir, '..') unless defined?(PROJECT_ROOT) end # The vagrant specific files which must be loaded prior to the rest -%w{vagrant/util vagrant/util/stacked_proc_runner vagrant/actions/base vagrant/downloaders/base vagrant/actions/collection +%w{vagrant/util vagrant/util/stacked_proc_runner vagrant/util/progress_meter vagrant/actions/base vagrant/downloaders/base vagrant/actions/collection vagrant/actions/runner vagrant/config vagrant/provisioners/base vagrant/provisioners/chef}.each do |f| require File.expand_path(f, libdir) end diff --git a/lib/vagrant/downloaders/http.rb b/lib/vagrant/downloaders/http.rb index 48ecb87ba..b999c0c22 100644 --- a/lib/vagrant/downloaders/http.rb +++ b/lib/vagrant/downloaders/http.rb @@ -3,8 +3,7 @@ module Vagrant # Downloads a file from an HTTP URL to a temporary file. This # downloader reports its progress to stdout while downloading. class HTTP < Base - # ANSI escape code to clear lines from cursor to end of line - CL_RESET = "\r\e[0K" + include Util::ProgressMeter def self.match?(uri) # URI.parse barfs on ':\\files \on\ windows' @@ -12,7 +11,7 @@ module Vagrant extracted = URI.extract(uri).first extracted && extracted.include?(uri) end - + def download!(source_url, destination_file) Net::HTTP.get_response(URI.parse(source_url)) do |response| total = response.content_length @@ -27,7 +26,7 @@ module Vagrant # Progress reporting is limited to every 25 segments just so # we're not constantly updating if segment_count % 25 == 0 - report_progress(progress, total) + update_progress(progress, total) segment_count = 0 end @@ -38,17 +37,6 @@ module Vagrant complete_progress end - - def report_progress(progress, total) - percent = (progress.to_f / total.to_f) * 100 - print "#{CL_RESET}Download Progress: #{percent.to_i}% (#{progress} / #{total})" - $stdout.flush - end - - def complete_progress - # Just clear the line back out - print "#{CL_RESET}" - end end end end diff --git a/lib/vagrant/util/progress_meter.rb b/lib/vagrant/util/progress_meter.rb new file mode 100644 index 000000000..5810db641 --- /dev/null +++ b/lib/vagrant/util/progress_meter.rb @@ -0,0 +1,29 @@ +module Vagrant + module Util + # A mixin which allows any class to be able to show a "progress meter" + # to standard out. The progress meter shows the progress of an operation + # with console-animated text in stdout. + module ProgressMeter + # ANSI escape code to clear lines from cursor to end of line + CL_RESET = "\r\e[0K" + + # Updates the progress meter with the given progress amount and total. + # This method will do the math to figure out a percentage and show it + # within stdout. + # + # @param [Float] progress Progress + # @param [Float] total Total + def update_progress(progress, total) + percent = (progress.to_f / total.to_f) * 100 + print "#{CL_RESET}Progress: #{percent.to_i}% (#{progress} / #{total})" + $stdout.flush + end + + # Completes the progress meter by resetting it off of the screen. + def complete_progress + # Just clear the line back out + print "#{CL_RESET}" + end + end + end +end \ No newline at end of file