Class: IRB::Command::History

Inherits:
Base show all
Defined in:
lib/irb/command/history.rb

Instance Attribute Summary

Attributes inherited from Base

#irb_context

Instance Method Summary collapse

Methods inherited from Base

category, description, execute, help_message, #initialize

Constructor Details

This class inherits a constructor from IRB::Command::Base

Instance Method Details

#execute(arg) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/irb/command/history.rb', line 15

def execute(arg)

  if (match = arg&.match(/(-g|-G)\s+(?<grep>.+)\s*\z/))
    grep = Regexp.new(match[:grep])
  end

  formatted_inputs = irb_context.io.class::HISTORY.each_with_index.reverse_each.filter_map do |input, index|
    next if grep && !input.match?(grep)

    header = "#{index}: "

    first_line, *other_lines = input.split("\n")
    first_line = "#{header}#{first_line}"

    truncated_lines = other_lines.slice!(1..) # Show 1 additional line (2 total)
    other_lines << "..." if truncated_lines&.any?

    other_lines.map! do |line|
      " " * header.length + line
    end

    [first_line, *other_lines].join("\n") + "\n"
  end

  Pager.page_content(formatted_inputs.join)
end