ruby/lib/irb/command/history.rb
Stan Lo f5801e2bf4 [ruby/irb] Standardize command related names
(https://github.com/ruby/irb/pull/873)

* Replace ExtendCommand with Command and standardize command related names

1. Rename lib/irb/extend-command.rb to lib/irb/command.rb
2. Rename lib/irb/cmd/*.rb to lib/irb/command/*.rb
3. Rename test/irb/test_cmd.rb to test/irb/test_command.rb
4. Rename ExtendCommand to Command

* Alias ExtendCommand to Command and deprecate it

* Rename Command::Nop to Command::Base

* Not deprecate old constants just yet

* Add lib/irb/cmd/nop.rb back

462c1284af
2024-02-16 16:47:36 +00:00

47 lines
1.2 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 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