Class: InteractiveEditor

Inherits:
Object show all
Defined in:
lib/utility_belt/interactive_editor.rb

Constant Summary collapse

DEBIAN_SENSIBLE_EDITOR =
"/usr/bin/sensible-editor"
MACOSX_OPEN_CMD =
"open"
XDG_OPEN =
"/usr/bin/xdg-open"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(editor = :vim) ⇒ InteractiveEditor

Returns a new instance of InteractiveEditor.



24
25
26
27
28
29
30
31
# File 'lib/utility_belt/interactive_editor.rb', line 24

def initialize(editor = :vim)
  @editor = editor.to_s
  if @editor == "mate"
    @editor = "mate -w"
  elsif @editor == "vim"
    @editor = 'vim -c ":set ft=ruby"'
  end
end

Instance Attribute Details

#editorObject

Returns the value of attribute editor.



23
24
25
# File 'lib/utility_belt/interactive_editor.rb', line 23

def editor
  @editor
end

Class Method Details

.sensible_editorObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/utility_belt/interactive_editor.rb', line 8

def self.sensible_editor
  return ENV["VISUAL"] if ENV["VISUAL"]
  return ENV["EDITOR"] if ENV["EDITOR"]
  return MACOSX_OPEN_CMD if Platform::IMPL == :macosx
  if Platform::IMPL == :linux
    if File.executable?(XDG_OPEN)
      return XDG_OPEN
    end
    if File.executable?(DEBIAN_SENSIBLE_EDITOR)
      return DEBIAN_SENSIBLE_EDITOR
    end
  end
  raise "Could not determine what editor to use.  Please specify."
end

Instance Method Details

#edit_interactivelyObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/utility_belt/interactive_editor.rb', line 32

def edit_interactively
  unless @file
    @file = Tempfile.new("irb_tempfile")
  end
  system("#{@editor} #{@file.path}")
  lines = File.read(@file.path).gsub("\r", "\n")
  lines.split("\n").each { |l| Readline::HISTORY << l } # update history
  Object.class_eval(lines)
  rescue Exception => error
    puts @file.path
    puts error
end