Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
40 changes: 40 additions & 0 deletions lib/irb/cmd/history.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

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

module IRB
# :stopdoc:

module ExtendCommand
class History < Nop
category "IRB"
description "Show the input history."

def execute(*)
output = StringIO.new
irb_context.io.class::HISTORY.each_with_index.reverse_each do |input, index|
header = "#{index}: "
header_length = header.size

lines = input.split("\n")
first_line = header + lines[0]

truncated_input = lines[1..1]
truncated_input << "..." if lines.size > 2

truncated_input = truncated_input.map do |line|
" " * header_length + line
end

output.puts [first_line, *truncated_input].join("\n")
end

Pager.page_content(output.string)
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
44 changes: 44 additions & 0 deletions test/irb/test_cmd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -971,4 +971,48 @@ 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
end

end