Method: Amp::UI#edit

Defined in:
lib/amp/support/amp_ui.rb

#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



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

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