From 5d7c887129f8ce1fccd50f112995fc8284044432 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Thu, 1 May 2014 01:10:10 -0700 Subject: [PATCH] Use Pry::Output to strip color when necessary All credit to johnny5- for this idea: https://github.com/johnny5-/pry/commit/952568b933f915fa78e193962ca4dca723de8fed Pros: No longer need to check _pry_.config.color everywhere. All color is stripped (not just that added by pry) Cons: Inefficient to add colors and then remove them again. --- lib/pry.rb | 1 + lib/pry/helpers/text.rb | 7 +++---- lib/pry/output.rb | 31 +++++++++++++++++++++++++++++++ lib/pry/pager.rb | 8 ++++---- lib/pry/pry_instance.rb | 7 +++++++ 5 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 lib/pry/output.rb diff --git a/lib/pry.rb b/lib/pry.rb index 391cdf46..479dc2b8 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -170,3 +170,4 @@ require "pry/last_exception" require "pry/prompt" require "pry/inspector" require 'pry/object_path' +require 'pry/output' diff --git a/lib/pry/helpers/text.rb b/lib/pry/helpers/text.rb index 826a6402..13867cf1 100644 --- a/lib/pry/helpers/text.rb +++ b/lib/pry/helpers/text.rb @@ -21,11 +21,11 @@ class Pry COLORS.each_pair do |color, value| define_method color do |text| - Pry.config.color ? "\033[0;#{30+value}m#{text}\033[0m" : text.to_s + "\033[0;#{30+value}m#{text}\033[0m" end define_method "bright_#{color}" do |text| - Pry.config.color ? "\033[1;#{30+value}m#{text}\033[0m" : text.to_s + "\033[1;#{30+value}m#{text}\033[0m" end end @@ -38,12 +38,11 @@ class Pry end # Returns _text_ as bold text for use on a terminal. - # _Pry.config.color_ must be true for this method to perform any transformations. # # @param [String, #to_s] text # @return [String] _text_ def bold(text) - Pry.config.color ? "\e[1m#{text}\e[0m" : text.to_s + "\e[1m#{text}\e[0m" end # Returns `text` in the default foreground colour. diff --git a/lib/pry/output.rb b/lib/pry/output.rb new file mode 100644 index 00000000..7661a379 --- /dev/null +++ b/lib/pry/output.rb @@ -0,0 +1,31 @@ + +class Pry + class Output + attr_reader :_pry_ + def initialize(_pry_) + @_pry_ = _pry_ + end + + def puts(str) + print "#{str.chomp}\n" + end + + def print str + if _pry_.config.color + _pry_.config.output.print str + else + _pry_.config.output.print Helpers::Text.strip_color str + end + end + alias << print + alias write print + + def method_missing(name, *args, &block) + _pry_.config.output.send(name, *args, &block) + end + + def respond_to_missing?(*a) + _pry_.config.respond_to?(*a) + end + end +end diff --git a/lib/pry/pager.rb b/lib/pry/pager.rb index d245941e..aedb9b20 100644 --- a/lib/pry/pager.rb +++ b/lib/pry/pager.rb @@ -50,11 +50,11 @@ class Pry::Pager # @param [#<<] output ($stdout) An object to send output to. def best_available if !_pry_.config.pager - NullPager.new(_pry_.config.output) + NullPager.new(_pry_.output) elsif !SystemPager.available? || Pry::Helpers::BaseHelpers.jruby? - SimplePager.new(_pry_.config.output) + SimplePager.new(_pry_.output) else - SystemPager.new(_pry_.config.output) + SystemPager.new(_pry_.output) end end @@ -107,7 +107,7 @@ class Pry::Pager if @tracker.page? @out.print "\n" - @out.print "\e[0m" if Pry.config.color + @out.print "\e[0m" @out.print " --- Press enter to continue " \ "( q to break ) --- \n" raise StopPaging if Readline.readline("").chomp == "q" diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index fbbdccdd..7122b591 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -603,6 +603,13 @@ class Pry Pry::Pager.new(self) end + # Returns an output device + # @example + # _pry_.output.puts "ohai!" + def output + Pry::Output.new(self) + end + # Raise an exception out of Pry. # # See Kernel#raise for documentation of parameters.