Sha256: 9bafc18851c69bb9d498e112aee64eed8f917cd3dc054a76012ea5f4ea638db1

Contents?: true

Size: 1.85 KB

Versions: 6908

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

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

        def initialize(file_with_embedded_line, pry_instance, opts)
          unless file_with_embedded_line
            raise CommandError, "Must provide a filename, --in, or --ex."
          end

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

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

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

          [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_instance.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
end

Version data entries

6,908 entries across 6,903 versions & 33 rubygems

Version Path
ory-client-0.0.1.alpha40 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/cat/file_formatter.rb
ory-client-0.0.1.alpha39 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/cat/file_formatter.rb
ory-client-0.0.1.alpha38 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/cat/file_formatter.rb
ory-client-0.0.1.alpha37 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/cat/file_formatter.rb
ory-client-0.0.1.alpha36 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/cat/file_formatter.rb
ory-kratos-client-0.8.2.alpha1 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/cat/file_formatter.rb
ory-client-0.0.1.alpha31 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/cat/file_formatter.rb
primary_connect_proto-0.4.0 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/commands/cat/file_formatter.rb
primary_connect_proto-0.3.0 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/commands/cat/file_formatter.rb
primary_connect_proto-0.2.0 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/commands/cat/file_formatter.rb
primary_connect_proto-0.1.0 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/commands/cat/file_formatter.rb
ory-client-0.0.1.alpha30 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/cat/file_formatter.rb
ory-client-0.0.1.alpha29 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/cat/file_formatter.rb
primary_connect_proto-0.0.6 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/commands/cat/file_formatter.rb
ory-client-0.0.1.alpha28 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/cat/file_formatter.rb
primary_connect_proto-0.0.5 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/commands/cat/file_formatter.rb
ory-client-0.0.1.alpha27 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/cat/file_formatter.rb
ory-client-0.0.1.alpha24 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/cat/file_formatter.rb
primary_connect_proto-0.0.4 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/commands/cat/file_formatter.rb
primary_connect_proto-0.0.3 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/commands/cat/file_formatter.rb