Class: Terjira::Editor

Inherits:
Object
  • Object
show all
Defined in:
lib/terjira/option_support/editor.rb

Class Method Summary collapse

Class Method Details

.editor_text(content = '') ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/terjira/option_support/editor.rb', line 5

def self.editor_text(content = '')
  editor = ENV['EDITOR']
  if editor.nil? || editor.empty?
    raise 'EDITOR environment variable not found. Please set a default editor.'
  end

  tmp_file = Tempfile.new('content')
  tmp_file.write(content)
  tmp_file.close
  success = system "#{editor} #{tmp_file.path}"
  content = File.read(tmp_file.path) if success

  tmp_file.unlink

  raise 'Editor returned a non-zero exit code. Something must have gone wrong' unless success

  content
end