Sha256: 1a33935fbcd6fbc6354df7b6c2b22365abf5a88586e76d46c10461386ec02e5d

Contents?: true

Size: 1.09 KB

Versions: 9

Compression:

Stored size: 1.09 KB

Contents

# 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

Version data entries

9 entries across 9 versions & 3 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/irb-1.14.0/lib/irb/command/history.rb
irb-1.14.3 lib/irb/command/history.rb
irb-1.14.2 lib/irb/command/history.rb
irb-1.14.1 lib/irb/command/history.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/irb-1.13.2/lib/irb/command/history.rb
irb-1.14.0 lib/irb/command/history.rb
irb-1.13.2 lib/irb/command/history.rb
irb-1.13.1 lib/irb/command/history.rb
irb-1.13.0 lib/irb/command/history.rb