updated with_line_numbers method to accept color parameter

This commit is contained in:
John Mair 2011-08-06 08:50:32 +11:00
parent f25542a8af
commit 01b248e296
1 changed files with 10 additions and 10 deletions

View File

@ -1,10 +1,10 @@
class Pry class Pry
module Helpers module Helpers
# The methods defined on {Text} are available to custom commands via {Pry::CommandContext#text}. # The methods defined on {Text} are available to custom commands via {Pry::CommandContext#text}.
module Text module Text
COLORS = COLORS =
{ {
"black" => 0, "black" => 0,
"red" => 1, "red" => 1,
@ -18,7 +18,7 @@ class Pry
} }
class << self class << self
COLORS.each_pair do |color, value| COLORS.each_pair do |color, value|
define_method color do |text| define_method color do |text|
Pry.color ? "\033[0;#{30+value}m#{text}\033[0m" : text.to_s Pry.color ? "\033[0;#{30+value}m#{text}\033[0m" : text.to_s
@ -31,7 +31,7 @@ class Pry
alias_method :grey, :bright_black alias_method :grey, :bright_black
alias_method :gray, :bright_black alias_method :gray, :bright_black
# Remove any color codes from _text_. # Remove any color codes from _text_.
# #
@ -41,11 +41,11 @@ class Pry
text.to_s.gsub(/\e\[.*?(\d)+m/ , '') text.to_s.gsub(/\e\[.*?(\d)+m/ , '')
end end
# Returns _text_ as bold text for use on a terminal. # Returns _text_ as bold text for use on a terminal.
# _Pry.color_ must be true for this method to perform any transformations. # _Pry.color_ must be true for this method to perform any transformations.
# #
# @param [String, #to_s] text # @param [String, #to_s] text
# @return [String] _text_ # @return [String] _text_
def bold text def bold text
Pry.color ? "\e[1m#{text}\e[0m" : text.to_s Pry.color ? "\e[1m#{text}\e[0m" : text.to_s
end end
@ -63,15 +63,15 @@ class Pry
end end
# Returns _text_ in a numbered list, beginning at _offset_. # Returns _text_ in a numbered list, beginning at _offset_.
# #
# @param [#each_line] text # @param [#each_line] text
# @param [Fixnum] offset # @param [Fixnum] offset
# @return [String] # @return [String]
def with_line_numbers text, offset def with_line_numbers(text, offset, color=:blue)
lines = text.each_line.to_a lines = text.each_line.to_a
lines.each_with_index.map do |line, index| lines.each_with_index.map do |line, index|
adjusted_index = index + offset adjusted_index = index + offset
"#{self.blue adjusted_index}: #{line}" "#{self.send(color, adjusted_index)}: #{line}"
end.join end.join
end end
end end