Skip to content

Commit

Permalink
Simplify Pry::Code#to_s
Browse files Browse the repository at this point in the history
Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
  • Loading branch information
kyrylo committed Jan 3, 2013
1 parent a1f0a00 commit 79dcf24
Showing 1 changed file with 32 additions and 36 deletions.
68 changes: 32 additions & 36 deletions lib/pry/code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -306,44 +306,16 @@ def inspect
Object.instance_method(:to_s).bind(self).call
end

# Based on the configuration of the object, return a formatted String
# representation.
#
# @return [String]
# @return [String] a formatted representation (based on the configuration of
# the object).
def to_s
lines = @lines.map(&:dup)

if Pry.color
lines.each do |l|
l[0] = CodeRay.scan(l[0], @code_type).term
end
end

if @with_line_numbers
max_width = lines.last.last.to_s.length if lines.length > 0
lines.each do |l|
padded_line_num = l[1].to_s.rjust(max_width)
l[0] = "#{Pry::Helpers::BaseHelpers.colorize_code(padded_line_num.to_s)}: #{l[0]}"
end
end

if @with_marker
lines.each do |l|
if l[1] == @marker_line_num
l[0] = " => #{l[0]}"
else
l[0] = " #{l[0]}"
end
end
end

if @with_indentation
lines.each do |l|
l[0] = "#{' ' * @indentation_num}#{l[0]}"
end
lines = @lines.map(&:dup).each do |line|
add_color(line) if Pry.color
add_line_numbers(line) if @with_line_numbers
add_marker(line) if @with_marker
add_indentation(line) if @with_indentation
end

lines.map { |l| "#{l.first}\n" }.join
lines.map { |line| "#{ line[0] }\n" }.join
end

# Get the comment that describes the expression on the given line number.
Expand Down Expand Up @@ -414,5 +386,29 @@ def method_missing(name, *args, &blk)
def alter(&blk)
dup.tap { |o| o.instance_eval(&blk) }
end

def add_color(line_tuple)
line_tuple[0] = CodeRay.scan(line_tuple[0], @code_type).term
end

def add_line_numbers(line_tuple)
max_width = @lines.last.last.to_s.length if @lines.length > 0
padded_line_num = line_tuple[1].to_s.rjust(max_width)
line_tuple[0] =
"#{ Pry::Helpers::BaseHelpers.colorize_code(padded_line_num.to_s) }: " \
"#{ line_tuple[0] }"
end

def add_marker(line_tuple)
line_tuple[0] = if line_tuple[1] == @marker_line_num
" => #{ line_tuple[0] }"
else
" #{ line_tuple[0] }"
end
end

def add_indentation(line_tuple)
line_tuple[0] = "#{ ' ' * @indentation_num }#{ line_tuple[0] }"
end
end
end

0 comments on commit 79dcf24

Please sign in to comment.