Class: Vagrant::UI::Basic

Inherits:
Interface show all
Includes:
Vagrant::Util::SafePuts
Defined in:
lib/vagrant/ui.rb

Overview

This is a UI implementation that outputs the text as is. It doesn’t add any color.

Direct Known Subclasses

Colored

Instance Method Summary collapse

Methods included from Vagrant::Util::SafePuts

#safe_puts

Constructor Details

#initializeBasic



59
60
61
62
63
# File 'lib/vagrant/ui.rb', line 59

def initialize
  super

  @lock = Mutex.new
end

Instance Method Details

#ask(message, opts = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/vagrant/ui.rb', line 77

def ask(message, opts=nil)
  super(message)

  # We can't ask questions when the output isn't a TTY.
  raise Errors::UIExpectsTTY if !$stdin.tty? && !Vagrant::Util::Platform.cygwin?

  # Setup the options so that the new line is suppressed
  opts ||= {}
  opts[:new_line] = false if !opts.has_key?(:new_line)
  opts[:prefix]   = false if !opts.has_key?(:prefix)

  # Output the data
  say(:info, message, opts)

  # Get the results and chomp off the newline. We do a logical OR
  # here because `gets` can return a nil, for example in the case
  # that ctrl-D is pressed on the input.
  input = $stdin.gets || ""
  input.chomp
end

#clear_lineObject



114
115
116
117
118
119
# File 'lib/vagrant/ui.rb', line 114

def clear_line
  reset = "\r"
  reset += "\e[0K" if Util::Platform.windows? && !Util::Platform.cygwin?

  info(reset, :new_line => false)
end

#format_message(type, message, opts = nil) ⇒ Object

This is called by ‘say` to format the message for output.



148
149
150
151
152
# File 'lib/vagrant/ui.rb', line 148

def format_message(type, message, opts=nil)
  opts ||= {}
  message = "[#{opts[:scope]}] #{message}" if opts[:scope] && opts[:prefix]
  message
end

#report_progress(progress, total, show_parts = true) ⇒ Object

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.



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/vagrant/ui.rb', line 102

def report_progress(progress, total, show_parts=true)
  if total && total > 0
    percent = (progress.to_f / total.to_f) * 100
    line    = "Progress: #{percent.to_i}%"
    line   << " (#{progress} / #{total})" if show_parts
  else
    line    = "Progress: #{progress}"
  end

  info(line, :new_line => false)
end

#say(type, message, opts = nil) ⇒ Object

This method handles actually outputting a message of a given type to the console.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/vagrant/ui.rb', line 123

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 || opts[:channel] == :error ? $stderr : $stdout

  # Output! We wrap this in a lock so that it safely outputs only
  # one line at a time.
  @lock.synchronize do
    safe_puts(format_message(type, message, opts),
              :io => channel, :printer => printer)
  end
end

#scope(scope_name) ⇒ Object



143
144
145
# File 'lib/vagrant/ui.rb', line 143

def scope(scope_name)
  BasicScope.new(self, scope_name)
end