diff --git a/lib/awesome_print/custom_defaults.rb b/lib/awesome_print/custom_defaults.rb new file mode 100644 index 0000000..d4ef42e --- /dev/null +++ b/lib/awesome_print/custom_defaults.rb @@ -0,0 +1,47 @@ +module AwesomePrint + + class << self + attr_accessor :defaults, :force_colors + + # Class accessor to force colorized output (ex. forked subprocess where TERM + # might be dumb). + #------------------------------------------------------------------------------ + def force_colors!(value = true) + @force_colors = value + end + + def console? + !!(defined?(IRB) || defined?(Pry)) + end + + def rails_console? + console? && !!(defined?(Rails::Console) || ENV["RAILS_ENV"]) + end + + def diet_rb + IRB.formatter = Class.new(IRB::Formatter) do + def inspect_object(object) + object.ai + end + end.new + end + + def usual_rb + IRB::Irb.class_eval do + def output_value + ap @context.last_value + end + end + end + + def irb! + return unless defined?(IRB) + + IRB.version.include?("DietRB") ? diet_rb : usual_rb + end + + def pry! + Pry.print = proc { |output, value| output.puts value.ai } if defined?(Pry) + end + end +end diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index 9caa512..9da697d 100644 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -6,48 +6,6 @@ require_relative 'indentator' module AwesomePrint - class << self # Class accessors for custom defaults. - attr_accessor :defaults, :force_colors - - # Class accessor to force colorized output (ex. forked subprocess where TERM - # might be dumb). - #--------------------------------------------------------------------------- - def force_colors!(value = true) - @force_colors = value - end - - def console? - !!(defined?(IRB) || defined?(Pry)) - end - - def rails_console? - console? && !!(defined?(Rails::Console) || ENV['RAILS_ENV']) - end - - def irb! - return unless defined?(IRB) - unless IRB.version.include?('DietRB') - IRB::Irb.class_eval do - def output_value - ap @context.last_value - rescue NoMethodError - puts "(Object doesn't support #ai)" - end - end - else # MacRuby - IRB.formatter = Class.new(IRB::Formatter) do - def inspect_object(object) - object.ai - end - end.new - end - end - - def pry! - Pry.print = proc { |output, value| output.puts value.ai } if defined?(Pry) - end - end - class Inspector attr_accessor :options, :indentator