diff --git a/.gitignore b/.gitignore index 16b94dc..9a3813e 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,9 @@ pkg .ruby-version gemfiles/*.gemfile.lock Gemfile.lock +/tmp +/.bundle +/gemfiles/.bundle ## PROJECT::RVM .rvmrc diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index 1193757..9caa512 100644 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -3,16 +3,15 @@ # Awesome Print is freely distributable under the terms of MIT license. # See LICENSE file or http://www.opensource.org/licenses/mit-license.php #------------------------------------------------------------------------------ -require_relative "indentator" +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 @@ -22,12 +21,12 @@ module AwesomePrint end def rails_console? - console? && !!(defined?(Rails::Console) || ENV["RAILS_ENV"]) + console? && !!(defined?(Rails::Console) || ENV['RAILS_ENV']) end def irb! return unless defined?(IRB) - unless IRB.version.include?("DietRB") + unless IRB.version.include?('DietRB') IRB::Irb.class_eval do def output_value ap @context.last_value @@ -45,9 +44,7 @@ module AwesomePrint end def pry! - if defined?(Pry) - Pry.print = proc { |output, value| output.puts value.ai } - end + Pry.print = proc { |output, value| output.puts value.ai } if defined?(Pry) end end @@ -108,7 +105,7 @@ module AwesomePrint end # Dispatcher that detects data nesting and invokes object-aware formatter. - #------------------------------------------------------------------------------ + #--------------------------------------------------------------------------- def awesome(object) if Thread.current[AP].include?(object.object_id) nested(object) @@ -123,7 +120,7 @@ module AwesomePrint end # Return true if we are to colorize the output. - #------------------------------------------------------------------------------ + #--------------------------------------------------------------------------- def colorize? AwesomePrint.force_colors ||= false AwesomePrint.force_colors || (STDOUT.tty? && ((ENV['TERM'] && ENV['TERM'] != 'dumb') || ENV['ANSICON'])) @@ -136,24 +133,24 @@ module AwesomePrint # => [1,2, [...]] # hash = { :a => 1 }; hash[:b] = hash # => { :a => 1, :b => {...} } - #------------------------------------------------------------------------------ + #--------------------------------------------------------------------------- def nested(object) case printable(object) - when :array then @formatter.colorize("[...]", :array) - when :hash then @formatter.colorize("{...}", :hash) - when :struct then @formatter.colorize("{...}", :struct) - else @formatter.colorize("...#{object.class}...", :class) + when :array then @formatter.colorize('[...]', :array) + when :hash then @formatter.colorize('{...}', :hash) + when :struct then @formatter.colorize('{...}', :struct) + else @formatter.colorize("...#{object.class}...", :class) end end - #------------------------------------------------------------------------------ + #--------------------------------------------------------------------------- def unnested(object) @formatter.format(object, printable(object)) end # Turn class name into symbol, ex: Hello::World => :hello_world. Classes that # inherit from Array, Hash, File, Dir, and Struct are treated as the base class. - #------------------------------------------------------------------------------ + #--------------------------------------------------------------------------- def printable(object) case object when Array then :array @@ -161,19 +158,19 @@ module AwesomePrint when File then :file when Dir then :dir when Struct then :struct - else object.class.to_s.gsub(/:+/, "_").downcase.to_sym + else object.class.to_s.gsub(/:+/, '_').downcase.to_sym end end # Update @options by first merging the :color hash and then the remaining keys. - #------------------------------------------------------------------------------ + #--------------------------------------------------------------------------- def merge_options!(options = {}) @options[:color].merge!(options.delete(:color) || {}) @options.merge!(options) end # Load ~/.aprc file with custom defaults that override default options. - #------------------------------------------------------------------------------ + #--------------------------------------------------------------------------- def merge_custom_defaults! dotfile = File.join(ENV['HOME'], '.aprc') load dotfile if File.readable?(dotfile) diff --git a/spec/misc_spec.rb b/spec/misc_spec.rb index 8539470..60f51c7 100644 --- a/spec/misc_spec.rb +++ b/spec/misc_spec.rb @@ -96,24 +96,24 @@ RSpec.describe "AwesomePrint" do it "wraps multiline ap output with
 tag with colorized " do
       markup = [ 1, :two, "three" ]
-      expect(markup.ai(:html => true)).to eq <<-EOS.strip
-
[
-    [0] 1,
-    [1] :two,
-    [2] "three"
-]
-EOS + expect(markup.ai(:html => true)).to eq <<-EOS.strip_heredoc.strip +
[
+            [0] 1,
+            [1] :two,
+            [2] "three"
+        ]
+ EOS end it "wraps hash ap output with only an outer
 tag" do
       markup = [ { "hello" => "world" } ]
-      expect(markup.ai(:html => true)).to eq <<-EOS.strip
-
[
-    [0] {
-        "hello" => "world"
-    }
-]
- EOS + expect(markup.ai(:html => true)).to eq <<-EOS.strip_heredoc.strip +
[
+            [0] {
+                "hello" => "world"
+            }
+        ]
+ EOS end it "encodes HTML entities (plain)" do @@ -142,13 +142,13 @@ EOS AwesomePrint.defaults = { :indent => -2, :sort_keys => true } hash = { [0, 0, 255] => :yellow, :red => "rgb(255, 0, 0)", "magenta" => "rgb(255, 0, 255)" } out = hash.ai(:plain => true) - expect(out).to eq <<-EOS.strip -{ - [ 0, 0, 255 ] => :yellow, - "magenta" => "rgb(255, 0, 255)", - :red => "rgb(255, 0, 0)" -} -EOS + expect(out).to eq <<-EOS.strip_heredoc.strip + { + [ 0, 0, 255 ] => :yellow, + "magenta" => "rgb(255, 0, 255)", + :red => "rgb(255, 0, 0)" + } + EOS end end