Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ IRB
source Loads a given file in the current session.
irb_info Show information about IRB.
show_cmds List all available commands and their description.
history Shows the input history. `-g [query]` or `-G [query]` allows you to filter the output.

Multi-irb (DEPRECATED)
irb Start a child IRB.
Expand Down
47 changes: 47 additions & 0 deletions lib/irb/cmd/history.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require "stringio"
require_relative "nop"
require_relative "../pager"

module IRB
# :stopdoc:

module ExtendCommand
class History < Nop
category "IRB"
description "Shows the input history. `-g [query]` or `-G [query]` allows you to filter the output."

def self.transform_args(args)
match = args&.match(/(-g|-G)\s+(?<grep>.+)\s*\n\z/)
return unless match

"grep: #{Regexp.new(match[:grep]).inspect}"
end

def execute(grep: nil)
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
end
end

# :startdoc:
end
6 changes: 6 additions & 0 deletions lib/irb/extend-command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ def irb_context
[
:irb_show_cmds, :ShowCmds, "cmd/show_cmds",
[:show_cmds, NO_OVERRIDE],
],

[
:irb_history, :History, "cmd/history",
[:history, NO_OVERRIDE],
[:hist, NO_OVERRIDE],
]
]

Expand Down
64 changes: 64 additions & 0 deletions test/irb/test_cmd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -971,4 +971,68 @@ def test_edit_with_editor_env_var
assert_match("command: ': code2'", out)
end
end

class HistoryCmdTest < CommandTestCase
def teardown
TestInputMethod.send(:remove_const, "HISTORY") if defined?(TestInputMethod::HISTORY)
super
end

def test_history
TestInputMethod.const_set("HISTORY", %w[foo bar baz])

out, err = without_rdoc do
execute_lines("history")
end

assert_include(out, <<~EOF)
2: baz
1: bar
0: foo
EOF
assert_empty err
end

def test_multiline_history_with_truncation
TestInputMethod.const_set("HISTORY", ["foo", "bar", <<~INPUT])
[].each do |x|
puts x
end
INPUT

out, err = without_rdoc do
execute_lines("hist")
end

assert_include(out, <<~EOF)
2: [].each do |x|
puts x
...
1: bar
0: foo
EOF
assert_empty err
end

def test_history_grep
TestInputMethod.const_set("HISTORY", ["foo", "bar", <<~INPUT])
[].each do |x|
puts x
end
INPUT

out, err = without_rdoc do
execute_lines("hist -g each\n")
end

assert_include(out, <<~EOF)
2: [].each do |x|
puts x
...
EOF
assert_empty err
end

end

end