DRY up the new UI class a bit
This commit is contained in:
parent
d84ec88fae
commit
6ea3335de6
|
@ -76,18 +76,22 @@ module Vagrant
|
|||
# to based on the type of the message
|
||||
channel = type == :error ? $stderr : $stdout
|
||||
|
||||
# Format the message
|
||||
message = "[#{env.resource}] #{message}" if opts[:prefix]
|
||||
|
||||
# Output!
|
||||
channel.send(printer, message)
|
||||
channel.send(printer, format_message(type, message, opts))
|
||||
end
|
||||
|
||||
# This is called by `say` to format the message for output.
|
||||
def format_message(type, message, opts=nil)
|
||||
opts ||= {}
|
||||
message = "[#{env.resource}] #{message}" if opts[:prefix]
|
||||
message
|
||||
end
|
||||
end
|
||||
|
||||
# This is a UI implementation that outputs color for various types
|
||||
# of messages. This should only be used with a TTY that supports color,
|
||||
# but is up to the user of the class to verify this is the case.
|
||||
class Colored < Interface
|
||||
class Colored < Basic
|
||||
# Terminal colors
|
||||
CLEAR = "\e[0m"
|
||||
YELLOW = "\e[33m"
|
||||
|
@ -101,61 +105,14 @@ module Vagrant
|
|||
:success => GREEN
|
||||
}
|
||||
|
||||
# Use some light meta-programming to create the various methods to
|
||||
# output text to the UI. These all delegate the real functionality
|
||||
# to `say`.
|
||||
[:info, :warn, :error, :success].each do |method|
|
||||
class_eval <<-CODE
|
||||
def #{method}(message, *args)
|
||||
super(message)
|
||||
say(#{method.inspect}, message, *args)
|
||||
end
|
||||
CODE
|
||||
end
|
||||
# This is called by `say` to format the message for output.
|
||||
def format_message(type, message, opts=nil)
|
||||
# Get the format of the message before adding color.
|
||||
message = super
|
||||
|
||||
# This is used to output progress reports to the UI.
|
||||
# Send this method progress/total and it will output it
|
||||
# to the UI. Send `clear_line` to clear the line to show
|
||||
# a continuous progress meter.
|
||||
def report_progress(progress, total, show_parts=true)
|
||||
percent = (progress.to_f / total.to_f) * 100
|
||||
line = "Progress: #{percent.to_i}%"
|
||||
line << " (#{progress} / #{total})" if show_parts
|
||||
|
||||
info(line, :new_line => false)
|
||||
end
|
||||
|
||||
def clear_line
|
||||
reset = "\r"
|
||||
reset += "\e[0K" unless Util::Platform.windows?
|
||||
reset
|
||||
|
||||
info(reset, :new_line => false)
|
||||
end
|
||||
|
||||
# This method handles actually outputting a message of a given type
|
||||
# to the console.
|
||||
def say(type, message, opts=nil)
|
||||
defaults = { :new_line => true, :prefix => true }
|
||||
opts = defaults.merge(opts || {})
|
||||
|
||||
# Determine whether we're expecting to output our
|
||||
# own new line or not.
|
||||
printer = opts[:new_line] ? :puts : :print
|
||||
|
||||
# Determine the proper IO channel to send this message
|
||||
# to based on the type of the message
|
||||
channel = type == :error ? $stderr : $stdout
|
||||
|
||||
# Format the message
|
||||
message = "[#{env.resource}] #{message}" if opts[:prefix]
|
||||
|
||||
# Colorize the message
|
||||
# Colorize the message if there is a color for this type of message
|
||||
message = "#{COLOR_MAP[type]}#{message}#{CLEAR}" if COLOR_MAP[type]
|
||||
|
||||
# Output!
|
||||
# TODO: Color
|
||||
channel.send(printer, message)
|
||||
message
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue