From b71cde6b999611e9b346f113b39bf075eefd2e45 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 23 Jan 2014 22:16:20 -0800 Subject: [PATCH] core: make box add UI much icer --- lib/vagrant/action/builtin/box_add.rb | 2 ++ lib/vagrant/ui.rb | 19 ++++++++++++------- lib/vagrant/util/downloader.rb | 4 ++-- plugins/commands/box/command/add.rb | 1 + templates/locales/en.yml | 2 ++ test/unit/vagrant/ui_test.rb | 18 +++++++++++++++--- 6 files changed, 34 insertions(+), 12 deletions(-) diff --git a/lib/vagrant/action/builtin/box_add.rb b/lib/vagrant/action/builtin/box_add.rb index 0553a3dd0..ab9946c02 100644 --- a/lib/vagrant/action/builtin/box_add.rb +++ b/lib/vagrant/action/builtin/box_add.rb @@ -59,6 +59,8 @@ module Vagrant url = env[:box_url] version = env[:box_version] + env[:ui].output(I18n.t( + "vagrant.box_loading_metadata", name: url)) metadata = nil begin metadata_path = download(url, env, ui: false) diff --git a/lib/vagrant/ui.rb b/lib/vagrant/ui.rb index 9563af974..3654c83f8 100644 --- a/lib/vagrant/ui.rb +++ b/lib/vagrant/ui.rb @@ -209,7 +209,10 @@ module Vagrant class_eval <<-CODE def #{method}(message, *args, **opts) super(message) - opts[:bold] = #{method.inspect} != :detail if !opts.has_key?(:bold) + if !opts.has_key?(:bold) + opts[:bold] = #{method.inspect} != :detail && \ + #{method.inspect} != :ask + end @ui.#{method}(format_message(#{method.inspect}, message, **opts), *args, **opts) end CODE @@ -241,7 +244,8 @@ module Vagrant prefix = "" if !opts.has_key?(:prefix) || opts[:prefix] prefix = OUTPUT_PREFIX - prefix = " " * OUTPUT_PREFIX.length if type == :detail + prefix = " " * OUTPUT_PREFIX.length if \ + type == :detail || type == :ask end # Fast-path if there is no prefix @@ -281,16 +285,17 @@ module Vagrant opts[:color] = :green if type == :success opts[:color] = :yellow if type == :warn - # If there is no color specified, exit early - return message if !opts.has_key?(:color) - # If it is a detail, it is not bold. Every other message type # is bolded. bold = !!opts[:bold] - color = COLORS[opts[:color]] + colorseq = "#{bold ? 1 : 0 }" + if opts[:color] + color = COLORS[opts[:color]] + colorseq += ";#{color}" + end # Color the message and make sure to reset the color at the end - "\033[#{bold ? 1 : 0};#{color}m#{message}\033[0m" + "\033[#{colorseq}m#{message}\033[0m" end end end diff --git a/lib/vagrant/util/downloader.rb b/lib/vagrant/util/downloader.rb index 3858936db..69f1e8700 100644 --- a/lib/vagrant/util/downloader.rb +++ b/lib/vagrant/util/downloader.rb @@ -92,7 +92,7 @@ module Vagrant output = "Progress: #{columns[0]}% (Rate: #{columns[11]}/s, Estimated time remaining: #{columns[10]})" @ui.clear_line - @ui.info(output, :new_line => false) + @ui.detail(output, :new_line => false) end end end @@ -111,7 +111,7 @@ module Vagrant # Windows doesn't clear properly for some reason, so we just # output one more newline. - @ui.info("") if Platform.windows? + @ui.detail("") if Platform.windows? end end diff --git a/plugins/commands/box/command/add.rb b/plugins/commands/box/command/add.rb index c94e10013..976700ce9 100644 --- a/plugins/commands/box/command/add.rb +++ b/plugins/commands/box/command/add.rb @@ -84,6 +84,7 @@ module VagrantPlugins box_download_ca_cert: options[:ca_cert], box_download_client_cert: options[:client_cert], box_download_insecure: options[:insecure], + ui: Vagrant::UI::Prefixed.new(@env.ui, "box"), }) # Success, exit status 0 diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 611367c29..1ef3bd7b1 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -18,6 +18,8 @@ en: Adding box '%{name}' (v%{version}) for '%{provider}' provider... box_added: |- Successfully added box '%{name}' for '%{provider}'! + box_loading_metadata: |- + Loading metadata for box '%{name}' cfengine_bootstrapping: |- Bootstrapping CFEngine with policy server: %{policy_server}... cfengine_bootstrapping_policy_hub: |- diff --git a/test/unit/vagrant/ui_test.rb b/test/unit/vagrant/ui_test.rb index 735d41da8..b8cc122da 100644 --- a/test/unit/vagrant/ui_test.rb +++ b/test/unit/vagrant/ui_test.rb @@ -65,7 +65,7 @@ describe Vagrant::UI::Colored do describe "#detail" do it "colors output nothing by default" do - subject.should_receive(:safe_puts).with("foo", anything) + subject.should_receive(:safe_puts).with("\033[0mfoo\033[0m", anything) subject.detail("foo") end @@ -91,11 +91,16 @@ describe Vagrant::UI::Colored do end describe "#output" do - it "colors output nothing by default" do - subject.should_receive(:safe_puts).with("foo", anything) + it "colors output nothing by default, no bold" do + subject.should_receive(:safe_puts).with("\033[0mfoo\033[0m", anything) subject.output("foo") end + it "bolds output without color if specified" do + subject.should_receive(:safe_puts).with("\033[1mfoo\033[0m", anything) + subject.output("foo", bold: true) + end + it "colors output to color specified in global opts" do subject.opts[:color] = :red @@ -232,6 +237,13 @@ describe Vagrant::UI::Prefixed do subject { described_class.new(ui, prefix) } + describe "#ask" do + it "does not request bolding" do + ui.should_receive(:ask).with(" #{prefix}: foo", bold: false) + subject.ask("foo") + end + end + describe "#detail" do it "prefixes with spaces and the message" do ui.should_receive(:safe_puts).with(" #{prefix}: foo", anything)