From 3fdcd50ea0a81b0a59646b271454df0ccf76fa72 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 25 Aug 2010 22:19:41 -0700 Subject: [PATCH] Add a report progress feature to the UI --- lib/vagrant/action/vm/export.rb | 4 +--- lib/vagrant/action/vm/import.rb | 16 ++++++---------- lib/vagrant/ui.rb | 21 +++++++++++++++++++-- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/lib/vagrant/action/vm/export.rb b/lib/vagrant/action/vm/export.rb index 5993b1876..741b31621 100644 --- a/lib/vagrant/action/vm/export.rb +++ b/lib/vagrant/action/vm/export.rb @@ -39,10 +39,8 @@ module Vagrant def export @env.ui.info "Exporting VM to #{ovf_path}..." @env["vm"].vm.export(ovf_path) do |progress| - @env.logger.report_progress(progress.percent, 100, false) + @env.ui.report_progress(progress.percent, 100, false) end - ensure - @env.logger.clear_progress end def ovf_path diff --git a/lib/vagrant/action/vm/import.rb b/lib/vagrant/action/vm/import.rb index 11868890e..b205f86ad 100644 --- a/lib/vagrant/action/vm/import.rb +++ b/lib/vagrant/action/vm/import.rb @@ -9,18 +9,14 @@ module Vagrant def call(env) env.ui.info "Importing base VM (#{env.env.box.ovf_file})" - begin - # Import the virtual machine - env.env.vm.vm = VirtualBox::VM.import(env.env.box.ovf_file) do |progress| - env.logger.report_progress(progress.percent, 100, false) - end - - # Flag as erroneous and return if import failed - return env.error!(:virtualbox_import_failure) if !env['vm'].vm - ensure - env.logger.clear_progress + # Import the virtual machine + env.env.vm.vm = VirtualBox::VM.import(env.env.box.ovf_file) do |progress| + env.ui.report_progress(progress.percent, 100, false) end + # Flag as erroneous and return if import failed + return env.error!(:virtualbox_import_failure) if !env['vm'].vm + # Import completed successfully. Continue the chain @app.call(env) end diff --git a/lib/vagrant/ui.rb b/lib/vagrant/ui.rb index 77aa5ec60..c6f78f32f 100644 --- a/lib/vagrant/ui.rb +++ b/lib/vagrant/ui.rb @@ -1,3 +1,5 @@ +require 'mario' + module Vagrant # Vagrant UIs handle communication with the outside world (typically # through a shell). They must respond to the typically logger methods @@ -9,7 +11,7 @@ module Vagrant @env = env end - [:warn, :error, :info, :confirm, :say_with_vm].each do |method| + [:warn, :error, :info, :confirm, :say_with_vm, :report_progress].each do |method| # By default these methods don't do anything. A silent UI. define_method(method) { |*args| } end @@ -26,16 +28,31 @@ module Vagrant [[:warn, :yellow], [:error, :red], [:info, nil], [:confirm, :green]].each do |method, color| define_method(method) do |message, prepend_vm_name=true| message = format_message(message) if prepend_vm_name - @shell.say(message, color) + @shell.say("#{line_reset}#{message}", color) end end + def report_progress(progress, total, show_parts=true) + percent = (progress.to_f / total.to_f) * 100 + line = "Progress: #{percent.to_i}%" + line << " (#{data[:progress]} / #{data[:total]})" if data[:show_parts] + line = "#{line_reset}#{line}" + + @shell.say(line, nil, false) + end + protected def format_message(message) name = env.vm_name || "vagrant" "[#{name}] #{message}" end + + def line_reset + reset = "\r" + reset += "\e[0K" unless Mario::Platform.windows? + reset + end end end end