core: colorize VM output for each VM in a command
This commit is contained in:
parent
afbed7e816
commit
f3d102e069
|
@ -188,7 +188,14 @@ module Vagrant
|
||||||
machines.reverse! if options[:reverse]
|
machines.reverse! if options[:reverse]
|
||||||
|
|
||||||
# Go through each VM and yield it!
|
# Go through each VM and yield it!
|
||||||
|
color_order = [:green, :cyan, :magenta, :yellow, :blue]
|
||||||
|
color_index = 0
|
||||||
|
|
||||||
machines.each do |machine|
|
machines.each do |machine|
|
||||||
|
# Set the machine color
|
||||||
|
machine.ui.opts[:color] = color_order[color_index % color_order.length]
|
||||||
|
color_index += 1
|
||||||
|
|
||||||
@logger.info("With machine: #{machine.name} (#{machine.provider.inspect})")
|
@logger.info("With machine: #{machine.name} (#{machine.provider.inspect})")
|
||||||
yield machine
|
yield machine
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,8 +15,13 @@ module Vagrant
|
||||||
# * `error`
|
# * `error`
|
||||||
# * `success`
|
# * `success`
|
||||||
class Interface
|
class Interface
|
||||||
|
# Opts can be used to set some options. These options are implementation
|
||||||
|
# specific. See the implementation for more docs.
|
||||||
|
attr_accessor :opts
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@logger = Log4r::Logger.new("vagrant::ui::interface")
|
@logger = Log4r::Logger.new("vagrant::ui::interface")
|
||||||
|
@opts = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
[:ask, :detail, :warn, :error, :info, :output, :success].each do |method|
|
[:ask, :detail, :warn, :error, :info, :output, :success].each do |method|
|
||||||
|
@ -219,6 +224,13 @@ module Vagrant
|
||||||
@scope = scope
|
@scope = scope
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Return the parent's opts.
|
||||||
|
#
|
||||||
|
# @return [Hash]
|
||||||
|
def opts
|
||||||
|
@ui.opts
|
||||||
|
end
|
||||||
|
|
||||||
[:ask, :detail, :warn, :error, :info, :output, :success].each do |method|
|
[:ask, :detail, :warn, :error, :info, :output, :success].each do |method|
|
||||||
define_method(method) do |message, opts=nil|
|
define_method(method) do |message, opts=nil|
|
||||||
opts ||= {}
|
opts ||= {}
|
||||||
|
@ -248,34 +260,29 @@ module Vagrant
|
||||||
class Colored < Basic
|
class Colored < Basic
|
||||||
# Terminal colors
|
# Terminal colors
|
||||||
COLORS = {
|
COLORS = {
|
||||||
:clear => "\e[0m",
|
red: 31,
|
||||||
:red => "\e[31m",
|
green: 32,
|
||||||
:green => "\e[32m",
|
yellow: 33,
|
||||||
:yellow => "\e[33m"
|
blue: 34,
|
||||||
}
|
magenta: 35,
|
||||||
|
cyan: 36,
|
||||||
# Mapping between type of message and the color to output
|
|
||||||
COLOR_MAP = {
|
|
||||||
:warn => COLORS[:yellow],
|
|
||||||
:error => COLORS[:red],
|
|
||||||
:success => COLORS[:green]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# 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=nil)
|
def format_message(type, message, **opts)
|
||||||
# Get the format of the message before adding color.
|
# Get the format of the message before adding color.
|
||||||
message = super
|
message = super
|
||||||
|
|
||||||
# Colorize the message if there is a color for this type of message,
|
opts = @opts.merge(opts)
|
||||||
# either specified by the options or via the default color map.
|
return message if !opts.has_key?(:color)
|
||||||
if opts.has_key?(:color)
|
|
||||||
color = COLORS[opts[:color]]
|
|
||||||
message = "#{color}#{message}#{COLORS[:clear]}"
|
|
||||||
else
|
|
||||||
message = "#{COLOR_MAP[type]}#{message}#{COLORS[:clear]}" if COLOR_MAP[type]
|
|
||||||
end
|
|
||||||
|
|
||||||
message
|
# If it is a detail, it is not bold. Every other message type
|
||||||
|
# is bolded.
|
||||||
|
bold = type != :detail
|
||||||
|
color = COLORS[opts[:color]]
|
||||||
|
|
||||||
|
# Color the message and make sure to reset the color at the end
|
||||||
|
"\033[#{bold ? 1 : 0};#{color}m#{message}\033[0m"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -110,6 +110,7 @@ module Vagrant
|
||||||
if windows?
|
if windows?
|
||||||
return true if ENV.has_key?("ANSICON")
|
return true if ENV.has_key?("ANSICON")
|
||||||
return true if cygwin?
|
return true if cygwin?
|
||||||
|
return true if ENV["TERM"] == "cygwin"
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue