Class: Pry::Command::Cat::FileFormatter

Inherits:
AbstractFormatter show all
Defined in:
lib/pry/commands/cat/file_formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractFormatter

#between_lines, #use_line_numbers?

Methods included from Helpers::BaseHelpers

#colorize_code, #command_dependencies_met?, #find_command, #heading, #highlight, #not_a_real_file?, #safe_send, #silence_warnings, #stagger_output, #use_ansi_codes?

Methods included from Platform

#jruby?, #jruby_19?, #known_engines, #linux?, #mac_osx?, #mri?, #mri_19?, #mri_2?, #rbx?, #windows?, #windows_ansi?

Methods included from Helpers::CommandHelpers

#absolute_index_number, absolute_index_number, #absolute_index_range, absolute_index_range, #command_error, command_error, #get_method_or_raise, get_method_or_raise, #internal_binding?, internal_binding?, #one_index_number, one_index_number, #one_index_range, one_index_range, #one_index_range_or_number, one_index_range_or_number, #restrict_to_lines, restrict_to_lines, #set_file_and_dir_locals, set_file_and_dir_locals, #temp_file, temp_file, #unindent, unindent

Methods included from Helpers::OptionsHelpers

#method_object, method_object, #method_options, method_options

Constructor Details

#initialize(file_with_embedded_line, _pry_, opts) ⇒ FileFormatter

Returns a new instance of FileFormatter

Raises:



8
9
10
11
12
13
14
15
# File 'lib/pry/commands/cat/file_formatter.rb', line 8

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

Instance Attribute Details

#_pry_Object (readonly)

Returns the value of attribute pry



6
7
8
# File 'lib/pry/commands/cat/file_formatter.rb', line 6

def _pry_
  @_pry_
end

#file_with_embedded_lineObject (readonly)

Returns the value of attribute file_with_embedded_line



4
5
6
# File 'lib/pry/commands/cat/file_formatter.rb', line 4

def file_with_embedded_line
  @file_with_embedded_line
end

#optsObject (readonly)

Returns the value of attribute opts



5
6
7
# File 'lib/pry/commands/cat/file_formatter.rb', line 5

def opts
  @opts
end

Instance Method Details

#code_typeObject (private)



50
51
52
# File 'lib/pry/commands/cat/file_formatter.rb', line 50

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

#code_window_sizeObject (private)



38
39
40
# File 'lib/pry/commands/cat/file_formatter.rb', line 38

def code_window_size
  _pry_.config.default_window_size || 7
end

#decorate(content) ⇒ Object (private)



42
43
44
45
46
47
48
# File 'lib/pry/commands/cat/file_formatter.rb', line 42

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

#detect_code_type_from_file(file_name) ⇒ Object (private)



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pry/commands/cat/file_formatter.rb', line 54

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

#file_and_lineObject



22
23
24
25
26
# File 'lib/pry/commands/cat/file_formatter.rb', line 22

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

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

#file_nameObject (private)



30
31
32
# File 'lib/pry/commands/cat/file_formatter.rb', line 30

def file_name
  file_and_line.first
end

#formatObject



17
18
19
20
# File 'lib/pry/commands/cat/file_formatter.rb', line 17

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

#line_numberObject (private)



34
35
36
# File 'lib/pry/commands/cat/file_formatter.rb', line 34

def line_number
  file_and_line.last
end