1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Start using Pry::Code#highlighted

Before this change the watch command was broken because Pry::Code#to_s
contained ansi codes which it passed into eval.
This commit is contained in:
Conrad Irwin 2014-04-29 18:40:56 -07:00
parent 5421dbfb34
commit 01785a8020
6 changed files with 18 additions and 12 deletions

View file

@ -253,15 +253,20 @@ class Pry
# @return [String] a formatted representation (based on the configuration of
# the object).
def to_s
print_to_output("")
print_to_output("", false)
end
# @return [String] a (possibly highlighted) copy of the source code.
def highlighted(color=false)
print_to_output("", color)
end
# Writes a formatted representation (based on the configuration of the
# object) to the given output, which must respond to `#<<`.
def print_to_output(output)
def print_to_output(output, color=false)
@lines.each do |loc|
loc = loc.dup
loc.colorize(@code_type) if Pry.config.color
loc.colorize(@code_type) if color
loc.add_line_number(max_lineno_width) if @with_line_numbers
loc.add_marker(@marker_lineno) if @with_marker
loc.indent(@indentation_num) if @with_indentation

View file

@ -79,7 +79,7 @@ class Pry
end
Pry::Pager.with_pager(output) do |pager|
@history.print_to_output(pager)
@history.print_to_output(pager, _pry_.config.color)
end
end

View file

@ -40,7 +40,7 @@ class Pry
# The source for code_object prepared for display.
def content_for(code_object)
Code.new(code_object.source, start_line_for(code_object)).
with_line_numbers(use_line_numbers?).to_s
with_line_numbers(use_line_numbers?).highlighted(_pry_.config.color)
end
end

View file

@ -87,8 +87,8 @@ class Pry
end
def add_expression(arguments)
expressions << Expression.new(target, arg_string)
output.puts "Watching #{Code.new(arg_string)}"
expressions << Expression.new(_pry_, target, arg_string)
output.puts "Watching #{Code.new(arg_string).highlighted(_pry_.config.color)}"
end
def add_hook

View file

@ -1,20 +1,21 @@
class Pry
class Command::WatchExpression
class Expression
attr_reader :target, :source, :value, :previous_value
attr_reader :target, :source, :value, :previous_value, :_pry_
def initialize(target, source)
def initialize(_pry_, target, source)
@_pry_ = _pry_
@target = target
@source = Code.new(source).strip
end
def eval!
@previous_value = @value
@value = Pry::ColorPrinter.pp(target_eval(target, source), "")
@value = Pry::ColorPrinter.pp(_pry_, target_eval(target, source), "")
end
def to_s
"#{source} => #{value}"
"#{Code.new(source).highlighted(_pry_.config.color).strip} => #{value}"
end
# Has the value of the expression changed?

View file

@ -89,7 +89,7 @@ class Pry
set_file_and_dir_locals(@file)
out = "\n#{text.bold('From:')} #{location}:\n\n" <<
code.with_line_numbers(use_line_numbers?).with_marker(marker).to_s << "\n"
code.with_line_numbers(use_line_numbers?).with_marker(marker).highlighted(_pry_.config.color) << "\n"
stagger_output(out)
end