Merge pull request #5771 from mitchellh/sethvargo/color

Add UI::Interface#color?
This commit is contained in:
Seth Vargo 2015-06-02 17:07:08 -04:00
commit a7aea0953f
6 changed files with 29 additions and 7 deletions

View File

@ -63,6 +63,11 @@ module Vagrant
define_method(method) { |*args| } define_method(method) { |*args| }
end end
# @return [false]
def color?
return false
end
# For machine-readable output. # For machine-readable output.
# #
# @param [String] type The type of the data # @param [String] type The type of the data
@ -336,6 +341,11 @@ module Vagrant
white: 37, white: 37,
} }
# @return [true]
def color?
return true
end
# This is called by `say` to format the message for output. # This is called by `say` to format the message for output.
def format_message(type, message, **opts) def format_message(type, message, **opts)
# Get the format of the message before adding color. # Get the format of the message before adding color.

View File

@ -73,10 +73,10 @@ module VagrantPlugins
# When Ansible output is piped in Vagrant integration, its default colorization is # When Ansible output is piped in Vagrant integration, its default colorization is
# automatically disabled and the only way to re-enable colors is to use ANSIBLE_FORCE_COLOR. # automatically disabled and the only way to re-enable colors is to use ANSIBLE_FORCE_COLOR.
env["ANSIBLE_FORCE_COLOR"] = "true" if @machine.env.ui.is_a?(Vagrant::UI::Colored) env["ANSIBLE_FORCE_COLOR"] = "true" if @machine.env.ui.color?
# Setting ANSIBLE_NOCOLOR is "unnecessary" at the moment, but this could change in the future # Setting ANSIBLE_NOCOLOR is "unnecessary" at the moment, but this could change in the future
# (e.g. local provisioner [GH-2103], possible change in vagrant/ansible integration, etc.) # (e.g. local provisioner [GH-2103], possible change in vagrant/ansible integration, etc.)
env["ANSIBLE_NOCOLOR"] = "true" unless @machine.env.ui.is_a?(Vagrant::UI::Colored) env["ANSIBLE_NOCOLOR"] = "true" if !@machine.env.ui.color?
# ANSIBLE_SSH_ARGS is required for Multiple SSH keys, SSH forwarding and custom SSH settings # ANSIBLE_SSH_ARGS is required for Multiple SSH keys, SSH forwarding and custom SSH settings
env["ANSIBLE_SSH_ARGS"] = ansible_ssh_args unless ansible_ssh_args.empty? env["ANSIBLE_SSH_ARGS"] = ansible_ssh_args unless ansible_ssh_args.empty?
@ -211,7 +211,7 @@ module VagrantPlugins
" -p #{docker_host_ssh_info[:port]} -i #{docker_host_ssh_info[:private_key_path][0]}" " -p #{docker_host_ssh_info[:port]} -i #{docker_host_ssh_info[:private_key_path][0]}"
# Use same options than plugins/providers/docker/communicator.rb # Use same options than plugins/providers/docker/communicator.rb
# Note: this could be improved (DRY'ed) by sharing these settings. # Note: this could be improved (DRY'ed) by sharing these settings.
proxy_cmd += " -o Compression=yes -o ConnectTimeout=5 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" proxy_cmd += " -o Compression=yes -o ConnectTimeout=5 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
proxy_cmd += " -o ForwardAgent=yes" if @ssh_info[:forward_agent] proxy_cmd += " -o ForwardAgent=yes" if @ssh_info[:forward_agent]
@ -226,7 +226,7 @@ module VagrantPlugins
# Set IdentitiesOnly=yes to avoid authentication errors when the host has more than 5 ssh keys. # Set IdentitiesOnly=yes to avoid authentication errors when the host has more than 5 ssh keys.
# Notes: # Notes:
# - Solaris/OpenSolaris/Illumos uses SunSSH which doesn't support the IdentitiesOnly option. # - Solaris/OpenSolaris/Illumos uses SunSSH which doesn't support the IdentitiesOnly option.
# - this could be improved by sharing logic with lib/vagrant/util/ssh.rb # - this could be improved by sharing logic with lib/vagrant/util/ssh.rb
ssh_options << "-o IdentitiesOnly=yes" unless Vagrant::Util::Platform.solaris? ssh_options << "-o IdentitiesOnly=yes" unless Vagrant::Util::Platform.solaris?

View File

@ -54,7 +54,7 @@ module VagrantPlugins
# This returns the command to run Chef for the given client # This returns the command to run Chef for the given client
# type. # type.
def build_command(client) def build_command(client)
builder = CommandBuilder.new(@config, client, windows?, @machine.env.ui.is_a?(Vagrant::UI::Colored)) builder = CommandBuilder.new(@config, client, windows?, @machine.env.ui.color?)
return builder.build_command return builder.build_command
end end

View File

@ -182,7 +182,7 @@ module VagrantPlugins
options << "--hiera_config=#{@hiera_config_path}" options << "--hiera_config=#{@hiera_config_path}"
end end
if !@machine.env.ui.is_a?(Vagrant::UI::Colored) if !@machine.env.ui.color?
options << "--color=false" options << "--color=false"
end end

View File

@ -68,7 +68,7 @@ module VagrantPlugins
end end
# Disable colors if we must # Disable colors if we must
if !@machine.env.ui.is_a?(Vagrant::UI::Colored) if !@machine.env.ui.color?
options << "--color=false" options << "--color=false"
end end

View File

@ -73,6 +73,12 @@ describe Vagrant::UI::Basic do
end end
end end
context "#color?" do
it "returns false" do
expect(subject.color?).to be(false)
end
end
context "#detail" do context "#detail" do
it "outputs details" do it "outputs details" do
expect(subject).to receive(:safe_puts).with { |message, **opts| expect(subject).to receive(:safe_puts).with { |message, **opts|
@ -95,6 +101,12 @@ end
describe Vagrant::UI::Colored do describe Vagrant::UI::Colored do
include_context "unit" include_context "unit"
describe "#color?" do
it "returns true" do
expect(subject.color?).to be(true)
end
end
describe "#detail" do describe "#detail" do
it "colors output nothing by default" do it "colors output nothing by default" do
expect(subject).to receive(:safe_puts).with("\033[0mfoo\033[0m", anything) expect(subject).to receive(:safe_puts).with("\033[0mfoo\033[0m", anything)