Sha256: 7bbe2369122f0f56885f4e6d32ebd2ba9cc0dfb7a29e5880d11fe21c78ac2b16

Contents?: true

Size: 1.88 KB

Versions: 36

Compression:

Stored size: 1.88 KB

Contents

require 'mario'

module Vagrant
  # Vagrant UIs handle communication with the outside world (typically
  # through a shell). They must respond to the typically logger methods
  # of `warn`, `error`, `info`, and `confirm`.
  class UI
    attr_accessor :env

    def initialize(env)
      @env = env
    end

    [:warn, :error, :info, :confirm, :say_with_vm, :report_progress, :ask, :no?, :yes?].each do |method|
      # By default these methods don't do anything. A silent UI.
      define_method(method) { |*args| }
    end

    # A shell UI, which uses a `Thor::Shell` object to talk with
    # a terminal.
    class Shell < UI
      def initialize(env, shell)
        super(env)

        @shell = shell
      end

      [[:warn, :yellow], [:error, :red], [:info, nil], [:confirm, :green]].each do |method, color|
        class_eval <<-CODE
          def #{method}(message, opts=nil)
            @shell.say("\#{line_reset}\#{format_message(message, opts)}", #{color.inspect})
          end
        CODE
      end

      [:ask, :no?, :yes?].each do |method|
        class_eval <<-CODE
          def #{method}(message, opts=nil)
            opts ||= {}
            @shell.send(#{method.inspect}, format_message(message, opts), opts[:color])
          end
        CODE
      end

      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
        line = "#{line_reset}#{line}"

        @shell.say(line, nil, false)
      end

      protected

      def format_message(message, opts=nil)
        opts = { :prefix => true }.merge(opts || {})
        message = "[#{env.resource}] #{message}" if opts[:prefix]
        message
      end

      def line_reset
        reset = "\r"
        reset += "\e[0K" unless Mario::Platform.windows?
        reset
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 4 rubygems

Version Path
vagrantup-0.7.8 lib/vagrant/ui.rb
vagrantup-0.7.7 lib/vagrant/ui.rb
vagrantup-0.7.6 lib/vagrant/ui.rb
vagrantup-0.7.5 lib/vagrant/ui.rb
vagrantup-0.7.4 lib/vagrant/ui.rb
vagrantup-0.7.3 lib/vagrant/ui.rb
vagrantup-0.7.2 lib/vagrant/ui.rb
vagrantup-0.7.1 lib/vagrant/ui.rb
vagrantup-0.7.0 lib/vagrant/ui.rb
vagrantup-0.6.9 lib/vagrant/ui.rb
vagrantup-0.6.8 lib/vagrant/ui.rb
vagrantup-0.6.7 lib/vagrant/ui.rb
vagrantup-0.6.6 lib/vagrant/ui.rb
vagrantup-0.6.5 lib/vagrant/ui.rb
vagrantup-0.6.4 lib/vagrant/ui.rb
vagrantup-0.6.3 lib/vagrant/ui.rb
vagrant-0.7.8 lib/vagrant/ui.rb
vagrant-0.7.7 lib/vagrant/ui.rb
vagrant-0.7.6 lib/vagrant/ui.rb
vagrant-0.7.5 lib/vagrant/ui.rb