UI objects use "scopes" now

This commit is contained in:
Mitchell Hashimoto 2013-03-21 18:25:27 -07:00
parent 8cd74fee16
commit 7b0745abcc
2 changed files with 39 additions and 6 deletions

View File

@ -132,7 +132,7 @@ module Vagrant
:action_name => "machine_action_#{name}".to_sym,
:machine => self,
:machine_action => name,
:ui => @env.ui_class.new(@name)
:ui => @env.ui.scope(@name)
}.merge(extra_env || {})
@env.action_runner.run(callable, env)
end

View File

@ -12,11 +12,8 @@ module Vagrant
# * `error`
# * `success`
class Interface
attr_accessor :resource
def initialize(resource=nil)
def initialize
@logger = Log4r::Logger.new("vagrant::ui::interface")
@resource = resource
end
[:ask, :warn, :error, :info, :success].each do |method|
@ -30,6 +27,15 @@ module Vagrant
# By default do nothing, these aren't logged
define_method(method) { |*args| }
end
# Returns a new UI class that is scoped to the given resource name.
# Subclasses can then use this scope name to do whatever they please.
#
# @param [String] scope_name
# @return [Interface]
def scope(scope_name)
self
end
end
# This is a UI implementation that does nothing.
@ -122,14 +128,41 @@ module Vagrant
:io => channel, :printer => printer)
end
def scope(scope_name)
BasicScope.new(self, scope_name)
end
# This is called by `say` to format the message for output.
def format_message(type, message, opts=nil)
opts ||= {}
message = "[#{@resource}] #{message}" if @resource && opts[:prefix]
message = "[#{opts[:scope]}] #{message}" if opts[:scope] && opts[:prefix]
message
end
end
# This implements a scope for the {Basic} UI.
class BasicScope < Interface
def initialize(ui, scope)
super()
@ui = ui
@scope = scope
end
[:ask, :warn, :error, :info, :success].each do |method|
define_method(method) do |message, opts=nil|
opts ||= {}
opts[:scope] = @scope
@ui.send(method, message, opts)
end
end
[:clear_line, :report_progress].each do |method|
# By default do nothing, these aren't logged
define_method(method) { |*args| @ui.send(method, *args) }
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.