Sha256: 7455ec16537e01e9f244abc9a694f13b2c340cc24a83ad5b71e3e701adae79da

Contents?: true

Size: 1.19 KB

Versions: 5

Compression:

Stored size: 1.19 KB

Contents

# 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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
irb-1.11.2 lib/irb/cmd/history.rb
irb-1.11.1 lib/irb/cmd/history.rb
irb-1.11.0 lib/irb/cmd/history.rb
irb-1.10.1 lib/irb/cmd/history.rb
irb-1.10.0 lib/irb/cmd/history.rb