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| }
end
# @return [false]
def color?
return false
end
# For machine-readable output.
#
# @param [String] type The type of the data
@ -336,6 +341,11 @@ module Vagrant
white: 37,
}
# @return [true]
def color?
return true
end
# This is called by `say` to format the message for output.
def format_message(type, message, **opts)
# 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
# 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
# (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
env["ANSIBLE_SSH_ARGS"] = ansible_ssh_args unless ansible_ssh_args.empty?

View File

@ -54,7 +54,7 @@ module VagrantPlugins
# This returns the command to run Chef for the given client
# type.
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
end

View File

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

View File

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

View File

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