diff --git a/lib/pry/default_commands/input.rb b/lib/pry/default_commands/input.rb index 6f557efa..b0c95131 100644 --- a/lib/pry/default_commands/input.rb +++ b/lib/pry/default_commands/input.rb @@ -12,20 +12,22 @@ class Pry history = Readline::HISTORY.to_a Slop.parse(args) do |opt| - opt.banner "Usage: hist [--replay START..END]\n" \ - "View and replay history\n" \ - "e.g hist --replay 2..8" + opt.banner "Usage: hist [--replay START..END] [--clear] [--grep PATTERN] [--help]\n" opt.on :g, :grep, 'A pattern to match against the history.', true do |pattern| + pattern = Regexp.new pattern history.pop - matches = history.grep Regexp.new(pattern) - text = add_line_numbers matches.join("\n"), 0 - stagger_output text + + history.each_with_index do |element, index| + if element =~ pattern + output.puts "#{colorize index}: #{element}" + end + end end opt.on :r, :replay, 'The line (or range of lines) to replay.', true, :as => Range do |range| unless opt.grep? - actions = history[range].join("\n") + "\n" + actions = Array(history[range]).join("\n") + "\n" Pry.active_instance.input = StringIO.new(actions) end end @@ -39,7 +41,7 @@ class Pry opt.on :h, :help, 'Show this message.', :tail => true do unless opt.grep? - output.puts opt.help + output.puts opt.help end end diff --git a/lib/pry/helpers/command_helpers.rb b/lib/pry/helpers/command_helpers.rb index 0884d8a5..d44610bb 100644 --- a/lib/pry/helpers/command_helpers.rb +++ b/lib/pry/helpers/command_helpers.rb @@ -23,16 +23,15 @@ class Pry end end + def colorize text + Pry.color ? CodeRay.scan(text.to_s, :ruby).term : text + end + def add_line_numbers(lines, start_line) line_array = lines.each_line.to_a line_array.each_with_index.map do |line, idx| adjusted_index = idx + start_line - if Pry.color - cindex = CodeRay.scan("#{adjusted_index}", :ruby).term - "#{cindex}: #{line}" - else - "#{idx}: #{line}" - end + "#{colorize adjusted_index}: #{line}" end.join end