summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author0xAB <0xAB@protonmail.com>2017-09-01 14:33:12 +0100
committer0xAB <0xAB@protonmail.com>2017-09-01 14:33:12 +0100
commit0f92ffc57cb699f90ae3667932a6976229c5e45b (patch)
treeff239ea874100e42be90c791d2c0aafde74e2b44
parent97e1d70301a2bc6ff97c6a3a7cc0bbe35e33c61e (diff)
parent075a5c4a45469ec5b0e34e21674e197d89829cee (diff)
downloadpry-0f92ffc57cb699f90ae3667932a6976229c5e45b.tar.gz
Merge remote-tracking branch 'pry/master' into pry-helpers
-rw-r--r--lib/pry/commands/ls/formatter.rb3
-rw-r--r--lib/pry/helpers/colors.rb56
-rw-r--r--lib/pry/helpers/text.rb10
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_.