Module: Ripl::Commands::History

Included in:
Ripl::Commands
Defined in:
lib/ripl/commands/history.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.edit(last = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ripl/commands/history.rb', line 19

def edit(last=nil)
  body = if last.nil? && @last_edit
    @last_edit
  elsif last.is_a?(Fixnum)
    start = Array(Ripl.shell.history)[-2][/^history/] ? 2 : 1
    slice_history(last, start).join("\n")
  else
    ''
  end
  file = Tempfile.new('edit_string').path
  File.open(file, 'w') {|f| f.puts(body) }
  system(editor, file)
  Ripl.shell.loop_eval(@last_edit = File.read(file))
end

.editorObject



34
35
36
# File 'lib/ripl/commands/history.rb', line 34

def editor
  ENV['EDITOR'] ? ENV['EDITOR'][/\w+/] : 'vim'
end

.history(last = 10) ⇒ Object



13
14
15
16
17
# File 'lib/ripl/commands/history.rb', line 13

def history(last=10)
  slice_history(last).each_with_index do |e,i|
    puts "#{last - i}: #{e}"
  end
end

.slice_history(last, start = 1) ⇒ Object



38
39
40
# File 'lib/ripl/commands/history.rb', line 38

def slice_history(last, start=1)
  Array(Ripl.shell.history).reverse.slice(start, last).reverse
end

Instance Method Details

#editor(*args) ⇒ Object



8
9
10
# File 'lib/ripl/commands/history.rb', line 8

def editor(*args)
  Ripl::Commands::History.edit(*args)
end

#history(*args) ⇒ Object



4
5
6
# File 'lib/ripl/commands/history.rb', line 4

def history(*args)
  Ripl::Commands::History.history(*args)
end