ruby/lib/irb/command/history.rb
tomoya ishida 6a505d1b59 [ruby/irb] Command implementation not by method
(https://github.com/ruby/irb/pull/824)

* Command is not a method

* Fix command test

* Implement non-method command name completion

* Add test for ExtendCommandBundle.def_extend_command

* Add helper method install test

* Remove spaces in command input parse

* Remove command arg unquote in help command

* Simplify Statement and handle execution in IRB::Irb

* Tweak require, const name

* Always install CommandBundle module to main object

* Remove considering local variable in command or expression check

* Remove unused method, tweak

* Remove outdated comment for help command arg

Co-authored-by: Stan Lo <stan001212@gmail.com>

---------

8fb776e379

Co-authored-by: Stan Lo <stan001212@gmail.com>
2024-04-10 16:52:53 +00:00

45 lines
1.1 KiB
Ruby

# frozen_string_literal: true
require "stringio"
require_relative "../pager"
module IRB
# :stopdoc:
module Command
class History < Base
category "IRB"
description "Shows the input history. `-g [query]` or `-G [query]` allows you to filter the output."
def execute(arg)
if (match = arg&.match(/(-g|-G)\s+(?<grep>.+)\s*\n\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
end
end
# :startdoc:
end