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

add color more confidently

This commit is contained in:
Conrad Irwin 2014-05-01 01:14:33 -07:00
parent 2285a83330
commit 2ad1b3a1c2
11 changed files with 21 additions and 33 deletions

View file

@ -257,8 +257,8 @@ class Pry
end
# @return [String] a (possibly highlighted) copy of the source code.
def highlighted(color=false)
print_to_output("", color)
def highlighted
print_to_output("", true)
end
# Writes a formatted representation (based on the configuration of the
@ -267,7 +267,7 @@ class Pry
@lines.each do |loc|
loc = loc.dup
loc.colorize(@code_type) if color
loc.add_line_number(max_lineno_width) if @with_line_numbers
loc.add_line_number(max_lineno_width, color) if @with_line_numbers
loc.add_marker(@marker_lineno) if @with_marker
loc.indent(@indentation_num) if @with_indentation
output << loc.line

View file

@ -59,9 +59,9 @@ class Pry
#
# @param [Integer] max_width
# @return [void]
def add_line_number(max_width = 0)
def add_line_number(max_width = 0, color = false)
padded = lineno.to_s.rjust(max_width)
colorized_lineno = Pry::Helpers::BaseHelpers.colorize_code(padded)
colorized_lineno = color ? Pry::Helpers::BaseHelpers.colorize_code(padded) : padded
tuple[0] = "#{ colorized_lineno }: #{ line }"
end

View file

@ -18,16 +18,14 @@ class Pry
end
def text(str, width = str.length)
super(*if !Pry.config.color
[str, width]
# Don't recolorize output with color [Issue #751]
elsif str.include?("\e[")
["#{str}\e[0m", width]
if str.include?("\e[")
super "#{str}\e[0m", width
elsif str.start_with?('#<') || str == '=' || str == '>'
[highlight_object_literal(str), width]
super highlight_object_literal(str), width
else
[CodeRay.scan(str, :ruby).term, width]
end)
super CodeRay.scan(str, :ruby).term, width
end
end
def pp(obj)
@ -43,7 +41,7 @@ class Pry
obj_id = obj.__id__.to_s(16) rescue 0
str = "#<#{klass}:0x#{obj_id}>"
text(Pry.config.color ? highlight_object_literal(str) : str)
text highlight_object_literal(str)
end
private

View file

@ -79,7 +79,7 @@ class Pry
end
_pry_.pager.open do |pager|
@history.print_to_output(pager, _pry_.config.color)
@history.print_to_output(pager, true)
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?).highlighted(_pry_.config.color)
with_line_numbers(use_line_numbers?).highlighted
end
end

View file

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

View file

@ -11,11 +11,11 @@ class Pry
def eval!
@previous_value = @value
@value = Pry::ColorPrinter.pp(_pry_, target_eval(target, source), "")
@value = Pry::ColorPrinter.pp(target_eval(target, source), "")
end
def to_s
"#{Code.new(source).highlighted(_pry_.config.color).strip} => #{value}"
"#{Code.new(source).highlighted.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).highlighted(_pry_.config.color) << "\n"
code.with_line_numbers(use_line_numbers?).with_marker(marker).highlighted << "\n"
_pry_.pager.page out
end

View file

@ -47,11 +47,7 @@ class Pry
end
def colorize_code(code)
if Pry.config.color
CodeRay.scan(code, :ruby).term
else
code
end
CodeRay.scan(code, :ruby).term
end
def highlight(string, regexp, highlight_color=:bright_yellow)
@ -61,7 +57,7 @@ class Pry
# formatting
def heading(text)
text = "#{text}\n--"
Pry.config.color ? "\e[1m#{text}\e[0m": text
"\e[1m#{text}\e[0m"
end
# have fun on the Windows platform.
@ -110,7 +106,7 @@ class Pry
# enabled). Infers where to send the output if used as a mixin.
# DEPRECATED.
def stagger_output(text, out = nil)
Pry::Pager.new(Pry.config.pager, out || Pry.config.output).page text
Pry.new.pager.page text
end
end
end

View file

@ -8,7 +8,6 @@ class Pry
module_function
def process_rdoc(comment)
return comment unless Pry.config.color
comment = comment.dup
comment.gsub(/<code>(?:\s*\n)?(.*?)\s*<\/code>/m) { CodeRay.scan($1, :ruby).term }.
gsub(/<em>(?:\s*\n)?(.*?)\s*<\/em>/m) { "\e[1m#{$1}\e[0m" }.
@ -37,7 +36,7 @@ class Pry
yard_tags = ["param", "return", "option", "yield", "attr", "attr_reader", "attr_writer",
"deprecate", "example", "raise"]
(yard_tags - ["example"]).inject(comment) { |a, v| process_yardoc_tag(a, v) }.
gsub(/^@(#{yard_tags.join("|")})/) { Pry.config.color ? "\e[33m#{$1}\e[0m": $1 }
gsub(/^@(#{yard_tags.join("|")})/) { "\e[33m#{$1}\e[0m" }
end
def process_comment_markup(comment)

View file

@ -63,11 +63,6 @@ describe Pry::Helpers::DocumentationHelpers do
it "should not remove ++" do
@helper.process_rdoc("--\n comment in a bubble\n++").should =~ /\+\+/
end
it "should do nothing if Pry.config.color is false" do
Pry.config.color = false
@helper.process_rdoc(" 4 + 4\n").should == " 4 + 4\n"
end
end
end