2013-01-06 20:44:33 +00:00
|
|
|
class Pry
|
|
|
|
class Command::Cat
|
|
|
|
class FileFormatter < AbstractFormatter
|
2014-02-07 00:55:48 +00:00
|
|
|
attr_reader :file_with_embedded_line
|
|
|
|
attr_reader :opts
|
|
|
|
attr_reader :_pry_
|
2013-01-06 20:44:33 +00:00
|
|
|
|
|
|
|
def initialize(file_with_embedded_line, _pry_, opts)
|
|
|
|
@file_with_embedded_line = file_with_embedded_line
|
|
|
|
@opts = opts
|
|
|
|
@_pry_ = _pry_
|
2013-11-14 06:47:56 +00:00
|
|
|
@code_from_file = Pry::Code.from_file(file_name)
|
2013-01-06 20:44:33 +00:00
|
|
|
end
|
|
|
|
|
2013-01-07 22:10:16 +00:00
|
|
|
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)
|
2013-11-14 06:47:56 +00:00
|
|
|
decorate(@code_from_file)
|
2013-01-07 22:10:16 +00:00
|
|
|
end
|
|
|
|
|
2013-01-06 20:44:33 +00:00
|
|
|
def file_and_line
|
2013-12-14 11:29:42 +00:00
|
|
|
file_name, line_num = file_with_embedded_line.split(/:(?!\/|\\)/)
|
2013-01-06 20:44:33 +00:00
|
|
|
|
2013-11-14 04:25:11 +00:00
|
|
|
[file_name, line_num ? line_num.to_i : nil]
|
2013-01-06 20:44:33 +00:00
|
|
|
end
|
|
|
|
|
2013-12-14 12:04:45 +00:00
|
|
|
private
|
|
|
|
|
2013-01-06 20:44:33 +00:00
|
|
|
def file_name
|
|
|
|
file_and_line.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def line_number
|
|
|
|
file_and_line.last
|
|
|
|
end
|
|
|
|
|
|
|
|
def code_window_size
|
2014-02-07 00:55:48 +00:00
|
|
|
_pry_.config.default_window_size || 7
|
2013-01-06 20:44:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def decorate(content)
|
|
|
|
line_number ? super.around(line_number, code_window_size) : super
|
|
|
|
end
|
|
|
|
|
|
|
|
def code_type
|
|
|
|
opts[:type] || detect_code_type_from_file(file_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def detect_code_type_from_file(file_name)
|
2013-11-14 06:47:56 +00:00
|
|
|
code_type = @code_from_file.code_type
|
2014-02-07 00:55:48 +00:00
|
|
|
|
2013-11-14 06:47:56 +00:00
|
|
|
if code_type == :unknown
|
|
|
|
name, ext = File.basename(file_name).split('.', 2)
|
2013-01-06 20:44:33 +00:00
|
|
|
case name
|
|
|
|
when "Rakefile", "Gemfile"
|
|
|
|
:ruby
|
|
|
|
else
|
|
|
|
:text
|
|
|
|
end
|
2013-11-14 06:47:56 +00:00
|
|
|
else
|
|
|
|
code_type
|
2013-01-06 20:44:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|