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

Add Pry::Helpers::Color.

This commit is contained in:
Rob Gleeson 2011-05-06 16:29:41 +01:00
parent dcc6ce4dbc
commit b54d907767
6 changed files with 56 additions and 66 deletions

View file

@ -25,5 +25,6 @@ class Pry
include Pry::Helpers::BaseHelpers
include Pry::Helpers::CommandHelpers
include Pry::Helpers::Color
end
end

View file

@ -27,7 +27,7 @@ class Pry
history.each_with_index do |element, index|
if element =~ pattern
output.puts "#{colorize index}: #{element}"
output.puts "#{blue index}: #{element}"
end
end
end

View file

@ -1,2 +1,3 @@
require "pry/helpers/base_helpers"
require "pry/helpers/command_helpers"
require "pry/helpers/color"

View file

@ -18,14 +18,7 @@ class Pry
text.split.drop(1).join(' ')
end
# turn off color for duration of block
def no_color(&block)
old_color_state = Pry.color
Pry.color = false
yield
ensure
Pry.color = old_color_state
end
def gem_installed?(gem_name)
require 'rubygems'
@ -67,53 +60,6 @@ class Pry
end
end
#
# Color helpers:
# gray, red, green, yellow, blue, purple, cyan, white,
# and bright_red, bright_green, etc...
#
# ANSI color codes:
# \033 => escape
# 30 => color base
# 1 => bright
# 0 => normal
#
COLORS = {
"black" => 0,
"red" => 1,
"green" => 2,
"yellow" => 3,
"blue" => 4,
"purple" => 5,
"magenta" => 5,
"cyan" => 6,
"white" => 7
}
COLORS.each do |color, i|
define_method color do |str|
Pry.color ? "\033[0;#{30+i}m#{str}\033[0m" : str
end
define_method "bright_#{color}" do |str|
Pry.color ? "\033[1;#{30+i}m#{str}\033[0m" : str
end
end
alias_method :grey, :bright_black
alias_method :gray, :bright_black
require 'set'
VALID_COLORS = Set.new(
COLORS.keys +
COLORS.keys.map{|k| "bright_#{k}" } +
["grey", "gray"]
)
def bold(text)
Pry.color ? "\e[1m#{text}\e[0m" : text
end
#
# Colorize a string that has "color tags".

51
lib/pry/helpers/color.rb Normal file
View file

@ -0,0 +1,51 @@
class Pry
module Helpers
module Color
extend self
COLORS =
{
"black" => 0,
"red" => 1,
"green" => 2,
"yellow" => 3,
"blue" => 4,
"purple" => 5,
"magenta" => 5,
"cyan" => 6,
"white" => 7
}
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
define_method "bright_#{color}" do |text|
Pry.color ? "\033[1;#{30+value}m#{text}\033[0m" : text.to_s
end
end
def strip_color text
text.gsub /\e\[.*?(\d)+m/, ''
end
def bold text
Pry.color ? "\e[1m#{text}\e[0m" : text.to_s
end
def no_color &block
boolean = Pry.color
Pry.color = false
yield
ensure
Pry.color = boolean
end
end
end
end

View file

@ -23,15 +23,11 @@ class Pry
end
end
def colorize text
Pry.color ? CodeRay.scan(text.to_s, :ruby).term : text
end
def add_line_numbers(lines, start_line)
line_array = lines.each_line.to_a
line_array.each_with_index.map do |line, idx|
adjusted_index = idx + start_line
"#{colorize adjusted_index}: #{line}"
"#{blue adjusted_index}: #{line}"
end.join
end
@ -231,11 +227,6 @@ class Pry
normalized_line_number(end_line, lines_array.size)]
end
# documentation related helpers
def strip_color_codes(str)
str.gsub(/\e\[.*?(\d)+m/, '')
end
def process_rdoc(comment, code_type)
comment = comment.dup
comment.gsub(/<code>(?:\s*\n)?(.*?)\s*<\/code>/m) { Pry.color ? CodeRay.scan($1, code_type).term : $1 }.