diff options
author | Robert <r-obert@users.noreply.github.com> | 2017-09-01 14:28:04 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-01 14:28:04 +0100 |
commit | 075a5c4a45469ec5b0e34e21674e197d89829cee (patch) | |
tree | 7aa985533314a2abc89e72c2b597b8318ff348c1 | |
parent | 28ef682a0acd8a6eeec6d21b1fc90acaabc42372 (diff) | |
parent | ba0b42da63a1949dd3a564bb9e6672472b8c6b26 (diff) | |
download | pry-075a5c4a45469ec5b0e34e21674e197d89829cee.tar.gz |
Merge pull request #1637 from pry/issue-1633
fix #1633
-rw-r--r-- | lib/pry/commands/ls/formatter.rb | 3 | ||||
-rw-r--r-- | lib/pry/helpers/colors.rb | 56 | ||||
-rw-r--r-- | lib/pry/helpers/text.rb | 10 |
3 files changed, 37 insertions, 32 deletions
diff --git a/lib/pry/commands/ls/formatter.rb b/lib/pry/commands/ls/formatter.rb index 623d7e54..6421d1ce 100644 --- a/lib/pry/commands/ls/formatter.rb +++ b/lib/pry/commands/ls/formatter.rb @@ -25,7 +25,8 @@ class Pry # Outputs nothing if the section would be empty. def output_section(heading, body) return '' if body.compact.empty? - fancy_heading = Pry::Helpers::Text.bold(color(:heading, heading)) + # Pass 'Pry' because global state programmers... + fancy_heading = Pry::Helpers::Text.bold(color(:heading, heading), Pry) Pry::Helpers.tablify_or_one_line(fancy_heading, body) end diff --git a/lib/pry/helpers/colors.rb b/lib/pry/helpers/colors.rb index 51105b3b..de5c2faf 100644 --- a/lib/pry/helpers/colors.rb +++ b/lib/pry/helpers/colors.rb @@ -13,6 +13,30 @@ module Pry::Helpers::Colors "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 + # # @example # @@ -34,32 +58,12 @@ module Pry::Helpers::Colors 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" + def bold text, pry=(defined?(_pry_) && _pry_) || Pry + (pry and pry.color) ? "\e[1m#{text}\e[0m" : text end # Remove any color codes from _text_. @@ -73,11 +77,11 @@ module Pry::Helpers::Colors # 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 + def no_color pry=(defined?(_pry_) && _pry_) || Pry, &block + boolean = pry.config.color + pry.config.color = false yield ensure - Pry.config.color = boolean + pry.config.color = boolean end end diff --git a/lib/pry/helpers/text.rb b/lib/pry/helpers/text.rb index 2ec1904c..3d2dd0f9 100644 --- a/lib/pry/helpers/text.rb +++ b/lib/pry/helpers/text.rb @@ -10,7 +10,7 @@ class Pry # Use this instead of "black" or "white" when you mean absence of colour. # # @deprecated - # Please use {#strip_color} instead. + # Please use {Colors#strip_color} instead. # # @param [String, #to_s] text # @return [String] @@ -24,12 +24,12 @@ class Pry # Executes the block with `Pry.config.pager` set to false. # @yield # @return [void] - def no_pager(&block) - boolean = Pry.config.pager - Pry.config.pager = false + def no_pager pry=(defined?(_pry_) && _pry_) || Pry, &block + boolean = pry.config.pager + pry.config.pager = false yield ensure - Pry.config.pager = boolean + pry.config.pager = boolean end # Returns _text_ in a numbered list, beginning at _offset_. |