aboutsummaryrefslogtreecommitdiffstats
path: root/lib/irb
diff options
context:
space:
mode:
authorGary Tou <gary@garytou.com>2023-12-01 20:32:00 -0800
committergit <svn-admin@ruby-lang.org>2023-12-02 04:32:04 +0000
commit4ee1f0fb5de08acd659ec18409fca433d1bf371a (patch)
tree59be9607e33d205a05c12d3c40659f5553e716fe /lib/irb
parent9f6c6f88c3b135a41efc3e76c4b3c012141ef8b1 (diff)
downloadruby-4ee1f0fb5de08acd659ec18409fca433d1bf371a.tar.gz
[ruby/irb] Implement `history` command
(https://github.com/ruby/irb/pull/761) * Implement `history` command Lists IRB input history with indices. Also aliased as `hist`. * Add tests for `history` command * Address feedback: `puts` with multiple arguments instead of `join`ing * Address feedback: Handle nil from splitting an empty input string * Refactor line truncation * Add `-g` grep option to `history` command * Add `history` command to README * Remove unused `*args` parameter * Allow spaces to be included in grep * Allow `/` to be included in grep regex * Handle `input` being an empty string * Exclude "#{index}: " from matching the grep regex * Add new line after joining https://github.com/ruby/irb/commit/3f9eacbfa9
Diffstat (limited to 'lib/irb')
-rw-r--r--lib/irb/cmd/history.rb47
-rw-r--r--lib/irb/extend-command.rb6
2 files changed, 53 insertions, 0 deletions
diff --git a/lib/irb/cmd/history.rb b/lib/irb/cmd/history.rb
new file mode 100644
index 0000000000..5b712fa44d
--- /dev/null
+++ b/lib/irb/cmd/history.rb
@@ -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
diff --git a/lib/irb/extend-command.rb b/lib/irb/extend-command.rb
index 514293a438..072069d4c4 100644
--- a/lib/irb/extend-command.rb
+++ b/lib/irb/extend-command.rb
@@ -191,6 +191,12 @@ module IRB # :nodoc:
[
:irb_show_cmds, :ShowCmds, "cmd/show_cmds",
[:show_cmds, NO_OVERRIDE],
+ ],
+
+ [
+ :irb_history, :History, "cmd/history",
+ [:history, NO_OVERRIDE],
+ [:hist, NO_OVERRIDE],
]
]