From af56c274c0cda16dc44402d3a85b1b527b187c6e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 27 Nov 2011 16:29:07 -0700 Subject: [PATCH] Add the "Basic" UI for non-colored messages --- bin/vagrant | 2 +- lib/vagrant/ui.rb | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/bin/vagrant b/bin/vagrant index 5a3b90428..668b5fedc 100755 --- a/bin/vagrant +++ b/bin/vagrant @@ -11,7 +11,7 @@ begin # Disable color if the proper argument was passed ui = nil if !$stdout.tty? || ARGV.include?("--no-color") - ui = Vagrant::UI::Colored.new(env) + ui = Vagrant::UI::Basic.new(env) else ui = Vagrant::UI::Colored.new(env) end diff --git a/lib/vagrant/ui.rb b/lib/vagrant/ui.rb index 958f03d32..9adebdcb5 100644 --- a/lib/vagrant/ui.rb +++ b/lib/vagrant/ui.rb @@ -27,6 +27,63 @@ module Vagrant end end + # This is a UI implementation that outputs the text as is. It + # doesn't add any color. + class Basic < Interface + # 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 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] + + # Output! + channel.send(printer, 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.