Sha256: 4c11a301c269f1196dbc0452afa03bc04967306a62d377ab45b38e0e439e8fd3

Contents?: true

Size: 1.62 KB

Versions: 39

Compression:

Stored size: 1.62 KB

Contents

class Pry
  class Command::Cat
    class FileFormatter < AbstractFormatter
      attr_reader :file_with_embedded_line
      attr_reader :opts
      attr_reader :_pry_

      def initialize(file_with_embedded_line, _pry_, opts)
        raise CommandError, "Must provide a filename, --in, or --ex." if !file_with_embedded_line

        @file_with_embedded_line = file_with_embedded_line
        @opts = opts
        @_pry_ = _pry_
        @code_from_file = Pry::Code.from_file(file_name)
      end

      def format
        set_file_and_dir_locals(file_name, _pry_, _pry_.current_context)
        decorate(@code_from_file)
      end

      def file_and_line
        file_name, line_num = file_with_embedded_line.split(/:(?!\/|\\)/)

        [file_name, line_num ? line_num.to_i : nil]
      end

      private

      def file_name
        file_and_line.first
      end

      def line_number
        file_and_line.last
      end

      def code_window_size
        _pry_.config.default_window_size || 7
      end

      def decorate(content)
        if line_number
          super(content.around(line_number, code_window_size))
        else
          super
        end
      end

      def code_type
        opts[:type] || detect_code_type_from_file(file_name)
      end

      def detect_code_type_from_file(file_name)
        code_type = @code_from_file.code_type

        if code_type == :unknown
          name = File.basename(file_name).split('.', 2).first
          case name
          when "Rakefile", "Gemfile"
            :ruby
          else
            :text
          end
        else
          code_type
        end
      end
    end
  end
end

Version data entries

39 entries across 39 versions & 14 rubygems

Version Path
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/pry-0.12.2/lib/pry/commands/cat/file_formatter.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/pry-0.12.2/lib/pry/commands/cat/file_formatter.rb
honeybadger-4.5.3 vendor/bundle/ruby/2.6.0/gems/pry-0.12.2/lib/pry/commands/cat/file_formatter.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/pry-0.12.2/lib/pry/commands/cat/file_formatter.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/pry-0.12.2/lib/pry/commands/cat/file_formatter.rb
dadapush_client-1.0.1 vendor/bundle/ruby/2.3.0/gems/pry-0.11.3/lib/pry/commands/cat/file_formatter.rb
chess_engine-0.0.2 vendor/bundle/gems/pry-0.12.2/lib/pry/commands/cat/file_formatter.rb
chess_engine-0.0.1 vendor/bundle/gems/pry-0.12.2/lib/pry/commands/cat/file_formatter.rb
xaiml-0.1.3 vendor/bundle/ruby/2.5.0/gems/pry-0.11.3/lib/pry/commands/cat/file_formatter.rb
alimentos-alu0100945645-0.1.0 vendor/bundle/ruby/2.3.0/gems/pry-0.12.2/lib/pry/commands/cat/file_formatter.rb
alimentos-alu0100945645-1.0.0 vendor/bundle/ruby/2.3.0/gems/pry-0.12.2/lib/pry/commands/cat/file_formatter.rb
xaiml-0.1.2 vendor/bundle/ruby/2.5.0/gems/pry-0.11.3/lib/pry/commands/cat/file_formatter.rb
pry-0.12.2-java lib/pry/commands/cat/file_formatter.rb
pry-0.12.2 lib/pry/commands/cat/file_formatter.rb
pry-0.12.1 lib/pry/commands/cat/file_formatter.rb
pry-0.12.1-java lib/pry/commands/cat/file_formatter.rb
pry-0.12.0 lib/pry/commands/cat/file_formatter.rb
pry-0.12.0-java lib/pry/commands/cat/file_formatter.rb
xaiml-0.1.1 vendor/bundle/ruby/2.5.0/gems/pry-0.11.3/lib/pry/commands/cat/file_formatter.rb
xaiml-0.1.0 vendor/bundle/ruby/2.5.0/gems/pry-0.11.3/lib/pry/commands/cat/file_formatter.rb