2011-05-06 11:29:41 -04:00
|
|
|
class Pry
|
|
|
|
module Helpers
|
|
|
|
|
2011-12-31 06:50:04 -05:00
|
|
|
# The methods defined on {Text} are available to custom commands via {Pry::Command#text}.
|
2011-05-06 13:30:37 -04:00
|
|
|
module Text
|
2011-08-05 17:50:32 -04:00
|
|
|
|
|
|
|
COLORS =
|
2011-05-06 11:29:41 -04:00
|
|
|
{
|
|
|
|
"black" => 0,
|
|
|
|
"red" => 1,
|
|
|
|
"green" => 2,
|
|
|
|
"yellow" => 3,
|
|
|
|
"blue" => 4,
|
|
|
|
"purple" => 5,
|
|
|
|
"magenta" => 5,
|
|
|
|
"cyan" => 6,
|
|
|
|
"white" => 7
|
|
|
|
}
|
|
|
|
|
2011-05-07 10:20:03 -04:00
|
|
|
class << self
|
2011-08-05 17:50:32 -04:00
|
|
|
|
2011-05-07 10:20:03 -04:00
|
|
|
COLORS.each_pair do |color, value|
|
|
|
|
define_method color do |text|
|
|
|
|
Pry.color ? "\033[0;#{30+value}m#{text}\033[0m" : text.to_s
|
|
|
|
end
|
2011-05-06 11:29:41 -04:00
|
|
|
|
2011-05-07 10:20:03 -04:00
|
|
|
define_method "bright_#{color}" do |text|
|
|
|
|
Pry.color ? "\033[1;#{30+value}m#{text}\033[0m" : text.to_s
|
|
|
|
end
|
2011-05-06 11:29:41 -04:00
|
|
|
end
|
|
|
|
|
2011-05-07 10:20:03 -04:00
|
|
|
# Remove any color codes from _text_.
|
|
|
|
#
|
|
|
|
# @param [String, #to_s] text
|
|
|
|
# @return [String] _text_ stripped of any color codes.
|
2012-11-29 12:51:10 -05:00
|
|
|
def strip_color(text)
|
2011-05-19 11:53:44 -04:00
|
|
|
text.to_s.gsub(/\e\[.*?(\d)+m/ , '')
|
2011-05-07 10:20:03 -04:00
|
|
|
end
|
2011-05-06 11:29:41 -04:00
|
|
|
|
2011-08-05 17:50:32 -04:00
|
|
|
# Returns _text_ as bold text for use on a terminal.
|
2011-05-07 10:20:03 -04:00
|
|
|
# _Pry.color_ must be true for this method to perform any transformations.
|
|
|
|
#
|
|
|
|
# @param [String, #to_s] text
|
2011-08-05 17:50:32 -04:00
|
|
|
# @return [String] _text_
|
2012-11-29 12:51:10 -05:00
|
|
|
def bold(text)
|
2011-05-07 10:20:03 -04:00
|
|
|
Pry.color ? "\e[1m#{text}\e[0m" : text.to_s
|
|
|
|
end
|
2011-05-06 11:29:41 -04:00
|
|
|
|
2012-06-27 01:30:00 -04:00
|
|
|
# Returns `text` in the default foreground colour.
|
2011-10-10 00:41:33 -04:00
|
|
|
# Use this instead of "black" or "white" when you mean absence of colour.
|
|
|
|
#
|
2012-06-27 01:30:00 -04:00
|
|
|
# @param [String, #to_s] text
|
|
|
|
# @return [String]
|
2011-10-10 00:41:33 -04:00
|
|
|
def default(text)
|
|
|
|
text.to_s
|
|
|
|
end
|
|
|
|
alias_method :bright_default, :bold
|
|
|
|
|
2012-06-27 01:30:00 -04:00
|
|
|
# Executes the block with `Pry.color` set to false.
|
|
|
|
# @yield
|
2011-05-07 10:20:03 -04:00
|
|
|
# @return [void]
|
2012-11-29 12:51:10 -05:00
|
|
|
def no_color(&block)
|
2011-12-11 03:33:33 -05:00
|
|
|
boolean = Pry.config.color
|
|
|
|
Pry.config.color = false
|
2011-05-07 10:20:03 -04:00
|
|
|
yield
|
|
|
|
ensure
|
2011-12-11 03:33:33 -05:00
|
|
|
Pry.config.color = boolean
|
|
|
|
end
|
|
|
|
|
2012-06-27 01:30:00 -04:00
|
|
|
# Executes the block with `Pry.config.pager` set to false.
|
|
|
|
# @yield
|
2011-12-11 03:33:33 -05:00
|
|
|
# @return [void]
|
2012-11-29 12:51:10 -05:00
|
|
|
def no_pager(&block)
|
2011-12-11 03:33:33 -05:00
|
|
|
boolean = Pry.config.pager
|
|
|
|
Pry.config.pager = false
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Pry.config.pager = boolean
|
2011-05-07 10:20:03 -04:00
|
|
|
end
|
2011-05-06 11:29:41 -04:00
|
|
|
|
2011-05-07 10:20:03 -04:00
|
|
|
# Returns _text_ in a numbered list, beginning at _offset_.
|
2011-08-05 17:50:32 -04:00
|
|
|
#
|
2011-05-09 00:55:25 -04:00
|
|
|
# @param [#each_line] text
|
2011-05-07 10:20:03 -04:00
|
|
|
# @param [Fixnum] offset
|
|
|
|
# @return [String]
|
2011-08-05 17:50:32 -04:00
|
|
|
def with_line_numbers(text, offset, color=:blue)
|
2011-05-07 10:20:03 -04:00
|
|
|
lines = text.each_line.to_a
|
2011-09-04 03:29:36 -04:00
|
|
|
max_width = (offset + lines.count).to_s.length
|
2011-05-07 10:20:03 -04:00
|
|
|
lines.each_with_index.map do |line, index|
|
2011-09-04 03:29:36 -04:00
|
|
|
adjusted_index = (index + offset).to_s.rjust(max_width)
|
2011-08-05 17:50:32 -04:00
|
|
|
"#{self.send(color, adjusted_index)}: #{line}"
|
2011-05-07 10:20:03 -04:00
|
|
|
end.join
|
|
|
|
end
|
2011-11-06 02:59:49 -05:00
|
|
|
|
|
|
|
# Returns _text_ indented by _chars_ spaces.
|
|
|
|
#
|
|
|
|
# @param [String] text
|
|
|
|
# @param [Fixnum] chars
|
|
|
|
def indent(text, chars)
|
|
|
|
text.lines.map { |l| "#{' ' * chars}#{l}" }.join
|
|
|
|
end
|
2011-05-06 13:50:40 -04:00
|
|
|
end
|
2011-05-07 10:20:03 -04:00
|
|
|
|
2011-05-06 11:29:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|