From 686c18869f507019d63c5165f0b689d1de1c5e77 Mon Sep 17 00:00:00 2001 From: matrinox Date: Mon, 17 Apr 2017 17:40:14 -0700 Subject: [PATCH] Made some improvements to Kernel#ai Only the top layer will wrap code in pre tags (fixed) The current indentation amount is passed in to the inspector so that new ones will maintain the same indentation ----------------------------------------------------------- On branch master - Mon 17 Apr 2017 17:40:14 PDT by matrinox --- lib/awesome_print/core_ext/kernel.rb | 6 ++--- .../formatters/array_formatter.rb | 4 ++-- .../formatters/hash_formatter.rb | 4 ++-- .../formatters/object_formatter.rb | 2 +- .../formatters/struct_formatter.rb | 2 +- lib/awesome_print/indentator.rb | 4 ++-- lib/awesome_print/inspector.rb | 24 ++++++++++--------- 7 files changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/awesome_print/core_ext/kernel.rb b/lib/awesome_print/core_ext/kernel.rb index e8d38edd..753ca804 100644 --- a/lib/awesome_print/core_ext/kernel.rb +++ b/lib/awesome_print/core_ext/kernel.rb @@ -6,10 +6,10 @@ module Kernel def ai(options = {}) - ap = AwesomePrint::Inspector.new(options) - awesome = ap.awesome self + inspector = AwesomePrint::Inspector.new(options.merge(top_layer: false)) + awesome = inspector.awesome self if options[:html] - awesome = "
#{awesome}
" + awesome = "
#{awesome}
" unless options[:top_layer] == false awesome = awesome.html_safe if defined? ActiveSupport end awesome diff --git a/lib/awesome_print/formatters/array_formatter.rb b/lib/awesome_print/formatters/array_formatter.rb index a2ae0e90..e8584ff0 100644 --- a/lib/awesome_print/formatters/array_formatter.rb +++ b/lib/awesome_print/formatters/array_formatter.rb @@ -31,7 +31,7 @@ def simple_array if options[:multiline] multiline_array else - '[ ' << array.map { |item| item.ai(@options) }.join(', ') << ' ]' + '[ ' << array.map { |item| item.ai(@options.merge(current_indentation: inspector.current_indentation)) }.join(', ') << ' ]' end end @@ -48,7 +48,7 @@ def multiline_array def generate_printable_array array.map.with_index do |item, index| array_prefix(index, width(array)).tap do |temp| - indented { temp << item.ai(@options) } + indented { temp << item.ai(@options.merge(current_indentation: inspector.current_indentation)) } end end end diff --git a/lib/awesome_print/formatters/hash_formatter.rb b/lib/awesome_print/formatters/hash_formatter.rb index 8ef95414..15f5da6f 100644 --- a/lib/awesome_print/formatters/hash_formatter.rb +++ b/lib/awesome_print/formatters/hash_formatter.rb @@ -84,11 +84,11 @@ def symbol?(key) def ruby19_syntax(key, value, width) key[0] = '' - align(key, width - 1) << colorize(': ', :hash) << value.ai(@options) + align(key, width - 1) << colorize(': ', :hash) << value.ai(@options.merge(current_indentation: inspector.current_indentation)) end def pre_ruby19_syntax(key, value, width) - align(key, width) << colorize(' => ', :hash) << value.ai(@options) + align(key, width) << colorize(' => ', :hash) << value.ai(@options.merge(current_indentation: inspector.current_indentation)) end def plain_single_line diff --git a/lib/awesome_print/formatters/object_formatter.rb b/lib/awesome_print/formatters/object_formatter.rb index 71531f90..ba779ed5 100644 --- a/lib/awesome_print/formatters/object_formatter.rb +++ b/lib/awesome_print/formatters/object_formatter.rb @@ -42,7 +42,7 @@ def format end indented do - key << colorize(' = ', :hash) + object.instance_variable_get(var).ai(@options) + key << colorize(' = ', :hash) + object.instance_variable_get(var).ai(@options.merge(current_indentation: inspector.current_indentation)) end end diff --git a/lib/awesome_print/formatters/struct_formatter.rb b/lib/awesome_print/formatters/struct_formatter.rb index 680f8f37..caf16c31 100644 --- a/lib/awesome_print/formatters/struct_formatter.rb +++ b/lib/awesome_print/formatters/struct_formatter.rb @@ -42,7 +42,7 @@ def format end indented do - key << colorize(' = ', :hash) + struct.send(var).ai(@options) + key << colorize(' = ', :hash) + struct.send(var).ai(@options.merge(current_indentation: inspector.current_indentation)) end end diff --git a/lib/awesome_print/indentator.rb b/lib/awesome_print/indentator.rb index 73e876bc..93cc9210 100644 --- a/lib/awesome_print/indentator.rb +++ b/lib/awesome_print/indentator.rb @@ -3,8 +3,8 @@ class Indentator attr_reader :shift_width, :indentation - def initialize(indentation) - @indentation = indentation + def initialize(indentation, current = nil) + @indentation = current ? current : indentation @shift_width = indentation.freeze end diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index 994a0d11..0f9489c4 100644 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -13,16 +13,17 @@ class Inspector def initialize(options = {}) @options = { - indent: 4, # Number of spaces for indenting. - index: true, # Display array indices. - html: false, # Use ANSI color codes rather than HTML. - multiline: true, # Display in multiple lines. - plain: false, # Use colors. - raw: false, # Do not recursively format instance variables. - sort_keys: false, # Do not sort hash keys. - sort_vars: true, # Sort instance variables. - limit: false, # Limit arrays & hashes. Accepts bool or int. - ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output. + indent: 4, # Number of spaces for indenting. + current_indentation: nil, # The current amount of indentation. + index: true, # Display array indices. + html: false, # Use ANSI color codes rather than HTML. + multiline: true, # Display in multiple lines. + plain: false, # Use colors. + raw: false, # Do not recursively format instance variables. + sort_keys: false, # Do not sort hash keys. + sort_vars: true, # Sort instance variables. + limit: false, # Limit arrays & hashes. Accepts bool or int. + ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output. color: { args: :pale, array: :white, @@ -51,7 +52,8 @@ def initialize(options = {}) merge_options!(options) @formatter = AwesomePrint::Formatter.new(self) - @indentator = AwesomePrint::Indentator.new(@options[:indent].abs) + + @indentator = AwesomePrint::Indentator.new(@options[:indent].abs, @options[:current_indentation]) Thread.current[AP] ||= [] end