Module: Amp::UI

Extended by:
Merges::MergeUI, UI
Included in:
UI
Defined in:
lib/amp/support/amp_ui.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Methods included from Merges::MergeUI

file_merge

Class Attribute Details

.configObject

Returns the value of attribute config.



66
67
68
# File 'lib/amp/support/amp_ui.rb', line 66

def config
  @config
end

Instance Method Details

#ask(question = '', type = String) ⇒ String

Ask question and return the answer, chomped

Parameters:

  • question (#to_s) (defaults to: '')

    question to ask

  • type (Class, #to_s, Symbol) (defaults to: String)

    the type to cast the answer to

Returns:

  • (String)

    their response without trailing newline



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/amp/support/amp_ui.rb', line 165

def ask(question='', type=String)
  type = type.to_s.downcase.to_sym
  
  print question.to_s
  result = gets.chomp unless type == :password
  
  # type conversion
  case type
  when :string
    result
  when :integer, :fixnum, :bignum, :numeric
    result.to_i
  when :array
    result.split(',').map {|e| e.strip }
  when :password
    get_pass
  else
    raise abort("Don't know how to convert to type #{type}")
  end
end

#choose {|menu| ... } ⇒ Object

Produces a menu for the user. Home-rolled to avoid rubygems dependencies!

Yields:

  • yields the menu object, which is configured inside the block, and then the menu is run

Yield Parameters:

  • menu

    the menu object. Should be configured inside the given block.



86
87
88
89
90
# File 'lib/amp/support/amp_ui.rb', line 86

def choose
  menu = UIMenu.new
  yield menu
  menu.run
end

#debug(message = '') ⇒ Object

Prints this message, only if the debug flag is set.

Parameters:

  • message (String, #to_s) (defaults to: '')

    The debug message to be printed



227
228
229
230
231
# File 'lib/amp/support/amp_ui.rb', line 227

def debug(message='')
  if @config && @config["debug","messages", Boolean, false]
    say message
  end
end

#edit(text = "", username = "") ⇒ String

Opens the editor for editing. Uses a temporary file to capture the user’s input. The user must have either HGEDITOR, AMPEDITOR, VISUAL, EDITOR, or the configuration “ui”->“editor” set. Or, it’ll just use vi.

Parameters:

  • text (String) (defaults to: "")

    the text with which to fill the file

  • username (String) (defaults to: "")

    the username to set the ENV var as

Returns:

  • (String)

    the file after being edited



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/amp/support/amp_ui.rb', line 242

def edit(text="", username="")
  tempfile = Tempfile.new("amp-editor-")
  path = tempfile.path
  tempfile.write text
  tempfile.close
  
  ENV["AMPUSER"] = username
  edit_file path
  
  text = File.open(path) {|tf| tf.read } || ''
  
  FileUtils.safe_unlink path
  
  text.gsub!(/^AMP:.*\n/,"")
  text
end

#edit_file(path) ⇒ Boolean

Opens the user’s editor for the file at the given path, with no extra processing afterward.

Parameters:

  • path (String, #inspect)

    The path to locate the file to open.

Returns:

  • (Boolean)

    whether or not the editor successfully launched



218
219
220
221
# File 'lib/amp/support/amp_ui.rb', line 218

def edit_file(path)
  editor = get_editor
  system "#{editor} #{path.inspect}"
end

#err(message = '') ⇒ NilClass

Prints message to standard error.

Parameters:

  • message (#to_s) (defaults to: '')

    the message to be errored.

Returns:

  • (NilClass)


155
156
157
# File 'lib/amp/support/amp_ui.rb', line 155

def err(message='')
  $stderr.puts message.to_s
end

#get_editorString

Gets the editor for the current system using environment variables or the configuration files.

Returns:

  • (String)

    the name of the editor command to execute



282
283
284
285
# File 'lib/amp/support/amp_ui.rb', line 282

def get_editor
  return ENV["AMPEDITOR"] || ENV["HGEDITOR"] || (@config && @config["ui","editor"]) ||
         ENV["VISUAL"] || ENV["EDITOR"] || "vi"
end

#get_passString

Gets the user’s password, while hiding it from prying eyes.

REQUIRES ‘stty`

Returns:

  • (String)

    the user’s password.



98
99
100
101
102
103
104
# File 'lib/amp/support/amp_ui.rb', line 98

def get_pass
  system "stty -echo"
  pass = gets.chomp
  tell "\n"
  system "stty echo"
  pass
end

#note(note = '') ⇒ NilClass

Notes the message - ignored unless in debug mode

Parameters:

  • note (#to_s) (defaults to: '')

    the note to print to standard output

Returns:

  • (NilClass)


125
126
127
128
129
130
# File 'lib/amp/support/amp_ui.rb', line 125

def note(note='')
  return unless $display
  if !@config || @config["ui", "amp-show-notes", Boolean, false]
    say "note: #{note}"
  end
end

#prompt(message = '', type = String, default = nil) ⇒ String

Deprecated.

Asks the user something.

Parameters:

  • message (#to_s) (defaults to: '')

    the message to send

  • type (Class, lambda) (defaults to: String)

    anything to force a type. If you supply a class, then the answer will be parsed into that class. If you supply a lambda, the string will be provided, and you do the conversion

  • []

    default Whatever the default answer is, if they fail to provide a valid answer.

Returns:

  • (String)

    their response with whitespace removed, or the default value



270
271
272
273
274
275
# File 'lib/amp/support/amp_ui.rb', line 270

def prompt(message='', type=String, default=nil)
  say message.to_s
  response = STDIN.gets.strip
  response = default if response == ""
  return response
end

#say(message = '') ⇒ NilClass

Prints message to standard out with a trailing newline

Parameters:

  • message (#to_s) (defaults to: '')

    the message to be printed.

Returns:

  • (NilClass)


137
138
139
# File 'lib/amp/support/amp_ui.rb', line 137

def say(message='')
  tell "#{message.to_s}\n"
end

#status(update = '') ⇒ NilClass

Prints a status update. Can be disabled using configuration files. These are casual updates to let the user know the progress in an operation.

Parameters:

  • update (#to_s) (defaults to: '')

    the message to print to standard output

Returns:

  • (NilClass)


113
114
115
116
117
118
# File 'lib/amp/support/amp_ui.rb', line 113

def status(update='')
  return unless $display
  if !@config || @config["ui", "amp-show-status", Boolean, true]
    say "status: #{update}"
  end
end

#tell(message = '') ⇒ NilClass

Prints message to standard out without a trailing newline.

Parameters:

  • message (#to_s) (defaults to: '')

    the message to be printed.

Returns:

  • (NilClass)


146
147
148
# File 'lib/amp/support/amp_ui.rb', line 146

def tell(message='')
  $stdout.print message.to_s
end

#warn(warning) ⇒ Object

Prints a warning. Can be disabled using configuration files. These are informational in nature but imply the user did something wrong, or that something is impossible.

Parameters:

  • warning (#to_s)

    the warning to print to standard output



74
75
76
77
78
# File 'lib/amp/support/amp_ui.rb', line 74

def warn(warning)
  if !@config || @config["ui","amp-show-warnings",Boolean,true]
    err "warning: #{warning}"
  end
end

#yes_or_no(question = '') ⇒ Boolean Also known as: agree

Ask a yes or no question (accepts a variety of inputs)

Parameters:

  • question (#to_s) (defaults to: '')

    question to ask

Returns:



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/amp/support/amp_ui.rb', line 191

def yes_or_no(question='')
  result = ask(question.to_s + ' [y/n/r/h] ')
  case result.downcase[0, 1]
  when 'r'
    yes_or_no question # recurse
  when 'h'
    tell <<-EOS
[y/n/r/h] means you can type anything starting with a 'y', 'n', 'r', or an 'h' to select those options.

'y'\tyes
'n'\tno
'r'\trepeat
'h'\thelp
EOS
    yes_or_no question
  else
    result.downcase.start_with? 'y'
  end
end