diff --git a/lib/pry/command_context.rb b/lib/pry/command_context.rb index 4f7d465f..459d6e21 100644 --- a/lib/pry/command_context.rb +++ b/lib/pry/command_context.rb @@ -25,5 +25,6 @@ class Pry include Pry::Helpers::BaseHelpers include Pry::Helpers::CommandHelpers + include Pry::Helpers::Color end end diff --git a/lib/pry/default_commands/input.rb b/lib/pry/default_commands/input.rb index c891ecaf..793afe4d 100644 --- a/lib/pry/default_commands/input.rb +++ b/lib/pry/default_commands/input.rb @@ -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 diff --git a/lib/pry/helpers.rb b/lib/pry/helpers.rb index 30681922..bfd8b131 100644 --- a/lib/pry/helpers.rb +++ b/lib/pry/helpers.rb @@ -1,2 +1,3 @@ require "pry/helpers/base_helpers" require "pry/helpers/command_helpers" +require "pry/helpers/color" diff --git a/lib/pry/helpers/base_helpers.rb b/lib/pry/helpers/base_helpers.rb index 5ce9b0c7..f2627355 100644 --- a/lib/pry/helpers/base_helpers.rb +++ b/lib/pry/helpers/base_helpers.rb @@ -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". diff --git a/lib/pry/helpers/color.rb b/lib/pry/helpers/color.rb new file mode 100644 index 00000000..bd50a259 --- /dev/null +++ b/lib/pry/helpers/color.rb @@ -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 + diff --git a/lib/pry/helpers/command_helpers.rb b/lib/pry/helpers/command_helpers.rb index d44610bb..28650458 100644 --- a/lib/pry/helpers/command_helpers.rb +++ b/lib/pry/helpers/command_helpers.rb @@ -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(/(?:\s*\n)?(.*?)\s*<\/code>/m) { Pry.color ? CodeRay.scan($1, code_type).term : $1 }.