diff options
author | 0xAB <0xAB@protonmail.com> | 2017-09-01 14:08:32 +0100 |
---|---|---|
committer | 0xAB <0xAB@protonmail.com> | 2017-09-01 14:08:32 +0100 |
commit | a7df6bc73467523c0328d18baaf62184f40a74b9 (patch) | |
tree | 4ff2ca433eb80eb2950e6fddb07a11929ee21c22 | |
parent | 0615de6a06a04abcdae14745eaff9fc959471847 (diff) | |
parent | 28ef682a0acd8a6eeec6d21b1fc90acaabc42372 (diff) | |
download | pry-a7df6bc73467523c0328d18baaf62184f40a74b9.tar.gz |
Merge branch 'master' into issue-1633
-rw-r--r-- | lib/pry/commands/gem_list.rb | 2 | ||||
-rw-r--r-- | lib/pry/commands/ls.rb | 22 | ||||
-rw-r--r-- | lib/pry/commands/ls/formatter.rb | 3 | ||||
-rw-r--r-- | lib/pry/helpers/colors.rb | 83 | ||||
-rw-r--r-- | lib/pry/helpers/text.rb | 74 |
5 files changed, 105 insertions, 79 deletions
diff --git a/lib/pry/commands/gem_list.rb b/lib/pry/commands/gem_list.rb index e4a5d00e..1c15f403 100644 --- a/lib/pry/commands/gem_list.rb +++ b/lib/pry/commands/gem_list.rb @@ -24,7 +24,7 @@ class Pry index == 0 ? text.bright_green(spec.version.to_s) : text.green(spec.version.to_s) end - output.puts "#{text.default gem} (#{versions.join ', '})" + output.puts "#{text.strip_color gem} (#{versions.join ', '})" end end end diff --git a/lib/pry/commands/ls.rb b/lib/pry/commands/ls.rb index 42b83d9f..a5d3d5a3 100644 --- a/lib/pry/commands/ls.rb +++ b/lib/pry/commands/ls.rb @@ -3,21 +3,21 @@ class Pry class Command::Ls < Pry::ClassCommand DEFAULT_OPTIONS = { :heading_color => :bright_blue, - :public_method_color => :default, + :public_method_color => :strip_color, :private_method_color => :blue, :protected_method_color => :blue, :method_missing_color => :bright_red, :local_var_color => :yellow, - :pry_var_color => :default, # e.g. _, _pry_, _file_ - :instance_var_color => :blue, # e.g. @foo - :class_var_color => :bright_blue, # e.g. @@foo - :global_var_color => :default, # e.g. $CODERAY_DEBUG, $eventmachine_library - :builtin_global_color => :cyan, # e.g. $stdin, $-w, $PID - :pseudo_global_color => :cyan, # e.g. $~, $1..$9, $LAST_MATCH_INFO - :constant_color => :default, # e.g. VERSION, ARGF - :class_constant_color => :blue, # e.g. Object, Kernel - :exception_constant_color => :magenta, # e.g. Exception, RuntimeError - :unloaded_constant_color => :yellow, # Any constant that is still in .autoload? state + :pry_var_color => :strip_color, # e.g. _, _pry_, _file_ + :instance_var_color => :blue, # e.g. @foo + :class_var_color => :bright_blue, # e.g. @@foo + :global_var_color => :strip_color, # e.g. $CODERAY_DEBUG, $eventmachine_library + :builtin_global_color => :cyan, # e.g. $stdin, $-w, $PID + :pseudo_global_color => :cyan, # e.g. $~, $1..$9, $LAST_MATCH_INFO + :constant_color => :strip_color, # e.g. VERSION, ARGF + :class_constant_color => :blue, # e.g. Object, Kernel + :exception_constant_color => :magenta, # e.g. Exception, RuntimeError + :unloaded_constant_color => :yellow, # Any constant that is still in .autoload? state :separator => " ", :ceiling => [Object, Module, Class] } diff --git a/lib/pry/commands/ls/formatter.rb b/lib/pry/commands/ls/formatter.rb index 3730b4cc..6421d1ce 100644 --- a/lib/pry/commands/ls/formatter.rb +++ b/lib/pry/commands/ls/formatter.rb @@ -18,8 +18,7 @@ class Pry private def color(type, str) - # Pass 'Pry' because global state programmers... - Pry::Helpers::Text.public_send _pry_.config.ls["#{type}_color"], str, Pry + Pry::Helpers::Text.paint str, _pry_.config.ls["#{type}_color"] end # Add a new section to the output. diff --git a/lib/pry/helpers/colors.rb b/lib/pry/helpers/colors.rb new file mode 100644 index 00000000..51105b3b --- /dev/null +++ b/lib/pry/helpers/colors.rb @@ -0,0 +1,83 @@ +module Pry::Helpers::Colors + extend self + COLORS = + { + "black" => 0, + "red" => 1, + "green" => 2, + "yellow" => 3, + "blue" => 4, + "purple" => 5, + "magenta" => 5, + "cyan" => 6, + "white" => 7 + } + + # + # @example + # + # paint "foo", :green + # paint "bar", :red + # paint "baz", :bold + # + # @param [String] str + # String to paint. + # + # @param [Symbol] + # The effect to apply to _str_. + # + # @return [String] + # Returns a string with _effect_ applied, or _str_ if the effect is unknown. + # + def paint(str, effect) + (Pry::Helpers::Colors.instance_methods(false) - [__method__]).include?(effect) ? + public_send(effect, str) : str + end + + COLORS.each_pair do |color, value| + define_method color do |text| + "\033[0;#{30+value}m#{text}\033[0m" + end + + define_method "bright_#{color}" do |text| + "\033[1;#{30+value}m#{text}\033[0m" + end + + COLORS.each_pair do |bg_color, bg_value| + define_method "#{color}_on_#{bg_color}" do |text| + "\033[0;#{30 + value};#{40 + bg_value}m#{text}\033[0m" + end + + define_method "bright_#{color}_on_#{bg_color}" do |text| + "\033[1;#{30 + value};#{40 + bg_value}m#{text}\033[0m" + end + end + end + + # Returns _text_ as bold text for use on a terminal. + # + # @param [String, #to_s] text + # @return [String] _text_ + def bold(text) + "\e[1m#{text}\e[0m" + end + + # Remove any color codes from _text_. + # + # @param [String, #to_s] text + # @return [String] _text_ stripped of any color codes. + def strip_color(text) + text.to_s.gsub(/(\001)?\e\[.*?(\d)+m(\002)?/ , '') + end + + # Executes the block with `Pry.config.color` set to false. + # @yield + # @return [void] + def no_color(&block) + boolean = Pry.config.color + Pry.config.color = false + yield + ensure + Pry.config.color = boolean + end +end diff --git a/lib/pry/helpers/text.rb b/lib/pry/helpers/text.rb index 66aeed03..ee63ec8d 100644 --- a/lib/pry/helpers/text.rb +++ b/lib/pry/helpers/text.rb @@ -1,82 +1,26 @@ class Pry module Helpers + require_relative "colors" # The methods defined on {Text} are available to custom commands via {Pry::Command#text}. module Text + include Pry::Helpers::Colors extend self - COLORS = - { - "black" => 0, - "red" => 1, - "green" => 2, - "yellow" => 3, - "blue" => 4, - "purple" => 5, - "magenta" => 5, - "cyan" => 6, - "white" => 7 - } - - color_enabled = lambda do |pry| - (pry and pry.color) or (defined?(_pry_) and _pry_.color) or Pry.color - end - - COLORS.each_pair do |color, value| - define_method(color) do |text, pry=nil| - instance_exec(pry, &color_enabled) ? "\033[0;#{30+value}m#{text}\033[0m" : text - end - - define_method("bright_#{color}") do |text, pry=nil| - instance_exec(pry, &color_enabled) ? "\033[1;#{30+value}m#{text}\033[0m" : text - end - - COLORS.each_pair do |bg_color, bg_value| - define_method "#{color}_on_#{bg_color}" do |text, pry=nil| - instance_exec(pry, &color_enabled) ? "\033[0;#{30 + value};#{40 + bg_value}m#{text}\033[0m" : text - end - - define_method "bright_#{color}_on_#{bg_color}" do |text, pry=nil| - instance_exec(pry, &color_enabled) ? "\033[1;#{30 + value};#{40 + bg_value}m#{text}\033[0m" : text - end - end - end - - # Remove any color codes from _text_. - # - # @param [String, #to_s] text - # @return [String] _text_ stripped of any color codes. - def strip_color(text) - text.to_s.gsub(/(\001)?\e\[.*?(\d)+m(\002)?/ , '') - end - - # Returns _text_ as bold text for use on a terminal. - # - # @param [String, #to_s] text - # @return [String] _text_ - def bold text, pry=(defined?(_pry_) && _pry_) || Pry - (pry and pry.color) ? "\e[1m#{text}\e[0m" : text - end # Returns `text` in the default foreground colour. # Use this instead of "black" or "white" when you mean absence of colour. # + # @deprecated + # Please use {#strip_color} instead. + # # @param [String, #to_s] text # @return [String] - def default(text, pry=nil) - text.to_s + def default text, pry=(defined?(_pry_) && _pry_) || Pry + pry.output.puts "DEPRECATED: Pry::Helpers::Text#default is deprecated, " \ + "please use Pry::Helpers::Text#strip_color instead" + strip_color text.to_s end alias_method :bright_default, :bold - # Executes the block with `Pry.config.color` set to false. - # @yield - # @return [void] - def no_color pry=(defined?(_pry_) && _pry_) || Pry, &block - boolean = pry.color - pry.config.color = false - yield - ensure - pry.config.color = boolean - end - # Executes the block with `Pry.config.pager` set to false. # @yield # @return [void] |