From c64dc43c844d4b09222e42217fc1f328e8f80e88 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 15 Apr 2010 17:53:54 -0700 Subject: [PATCH] Fix progress meter output for Windows --- lib/vagrant/util/progress_meter.rb | 13 ++++++---- test/vagrant/util/progress_meter_test.rb | 33 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 test/vagrant/util/progress_meter_test.rb diff --git a/lib/vagrant/util/progress_meter.rb b/lib/vagrant/util/progress_meter.rb index c15706090..32d9d3745 100644 --- a/lib/vagrant/util/progress_meter.rb +++ b/lib/vagrant/util/progress_meter.rb @@ -4,9 +4,6 @@ module Vagrant # 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. @@ -15,7 +12,7 @@ module Vagrant # @param [Float] total Total def update_progress(progress, total, show_parts=true) percent = (progress.to_f / total.to_f) * 100 - print "#{CL_RESET}Progress: #{percent.to_i}%" + print "#{cl_reset}Progress: #{percent.to_i}%" print " (#{progress} / #{total})" if show_parts $stdout.flush end @@ -23,7 +20,13 @@ module Vagrant # Completes the progress meter by resetting it off of the screen. def complete_progress # Just clear the line back out - print "#{CL_RESET}" + print "#{cl_reset}" + end + + def cl_reset + reset = "\r" + reset += "\e[0K" unless Mario::Platform.windows? + reset end end end diff --git a/test/vagrant/util/progress_meter_test.rb b/test/vagrant/util/progress_meter_test.rb new file mode 100644 index 000000000..b74b69a58 --- /dev/null +++ b/test/vagrant/util/progress_meter_test.rb @@ -0,0 +1,33 @@ +require File.join(File.dirname(__FILE__), '..', '..', 'test_helper') + +class ProgressMeterUtilTest < Test::Unit::TestCase + class TestProgressMeter + include Vagrant::Util::ProgressMeter + end + + setup do + @instance = TestProgressMeter.new + + Mario::Platform.logger(nil) + end + + context "on windows" do + setup do + Mario::Platform.forced = Mario::Platform::Windows7 + end + + should "just return \\r for the clear screen" do + assert_equal "\r", @instance.cl_reset + end + end + + context "on other platforms" do + setup do + Mario::Platform.forced = Mario::Platform::Linux + end + + should "return the full clear screen" do + assert_equal "\r\e[0K", @instance.cl_reset + end + end +end