class Pry::Command::Cat::FileFormatter

Attributes

_pry_[RW]
file_with_embedded_line[RW]
opts[RW]

Public Class Methods

new(file_with_embedded_line, _pry_, opts) click to toggle source
# File lib/pry/commands/cat/file_formatter.rb, line 8
def initialize(file_with_embedded_line, _pry_, opts)
  @file_with_embedded_line = file_with_embedded_line
  @opts = opts
  @_pry_ = _pry_
end

Public Instance Methods

format() click to toggle source
# File lib/pry/commands/cat/file_formatter.rb, line 14
def format
  raise CommandError, "Must provide a filename, --in, or --ex." if !file_with_embedded_line

  set_file_and_dir_locals(file_name, _pry_, _pry_.current_context)
  decorate(Pry::Code.from_file(file_name))
end

Private Instance Methods

code_type() click to toggle source
# File lib/pry/commands/cat/file_formatter.rb, line 45
def code_type
  opts[:type] || detect_code_type_from_file(file_name)
end
code_window_size() click to toggle source
# File lib/pry/commands/cat/file_formatter.rb, line 37
def code_window_size
  Pry.config.default_window_size || 7
end
decorate(content) click to toggle source
# File lib/pry/commands/cat/file_formatter.rb, line 41
def decorate(content)
  line_number ? super.around(line_number, code_window_size) : super
end
detect_code_type_from_file(file_name) click to toggle source
# File lib/pry/commands/cat/file_formatter.rb, line 49
def detect_code_type_from_file(file_name)
  name, ext = File.basename(file_name).split('.', 2)

  if ext
    case ext
    when "py"
      :python
    when "rb", "gemspec", "rakefile", "ru", "pryrc", "irbrc"
      :ruby
    when "js"
      return :javascript
    when "yml", "prytheme"
      :yaml
    when "groovy"
      :groovy
    when "c"
      :c
    when "cpp"
      :cpp
    when "java"
      :java
    else
      :text
    end
  else
    case name
    when "Rakefile", "Gemfile"
      :ruby
    else
      :text
    end
  end
end
file_and_line() click to toggle source
# File lib/pry/commands/cat/file_formatter.rb, line 23
def file_and_line
  file_name, line_num = file_with_embedded_line.split(':')

  [File.expand_path(file_name), line_num ? line_num.to_i : nil]
end
file_name() click to toggle source
# File lib/pry/commands/cat/file_formatter.rb, line 29
def file_name
  file_and_line.first
end
line_number() click to toggle source
# File lib/pry/commands/cat/file_formatter.rb, line 33
def line_number
  file_and_line.last
end