Skip to content

Commit 6235c30

Browse files
committed
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 <[email protected]>
1 parent 01f363f commit 6235c30

File tree

7 files changed

+24
-22
lines changed

7 files changed

+24
-22
lines changed

lib/awesome_print/core_ext/kernel.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
module Kernel
77

88
def ai(options = {})
9-
ap = AwesomePrint::Inspector.new(options)
10-
awesome = ap.awesome self
9+
inspector = AwesomePrint::Inspector.new(options.merge(top_layer: false))
10+
awesome = inspector.awesome self
1111
if options[:html]
12-
awesome = "<pre>#{awesome}</pre>"
12+
awesome = "<pre>#{awesome}</pre>" unless options[:top_layer] == false
1313
awesome = awesome.html_safe if defined? ActiveSupport
1414
end
1515
awesome

lib/awesome_print/formatters/array_formatter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def simple_array
3131
if options[:multiline]
3232
multiline_array
3333
else
34-
'[ ' << array.map { |item| item.ai(@options) }.join(', ') << ' ]'
34+
'[ ' << array.map { |item| item.ai(@options.merge(current_indentation: inspector.current_indentation)) }.join(', ') << ' ]'
3535
end
3636
end
3737

@@ -48,7 +48,7 @@ def multiline_array
4848
def generate_printable_array
4949
array.map.with_index do |item, index|
5050
array_prefix(index, width(array)).tap do |temp|
51-
indented { temp << item.ai(@options) }
51+
indented { temp << item.ai(@options.merge(current_indentation: inspector.current_indentation)) }
5252
end
5353
end
5454
end

lib/awesome_print/formatters/hash_formatter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ def symbol?(key)
8484

8585
def ruby19_syntax(key, value, width)
8686
key[0] = ''
87-
align(key, width - 1) << colorize(': ', :hash) << value.ai(@options)
87+
align(key, width - 1) << colorize(': ', :hash) << value.ai(@options.merge(current_indentation: inspector.current_indentation))
8888
end
8989

9090
def pre_ruby19_syntax(key, value, width)
91-
align(key, width) << colorize(' => ', :hash) << value.ai(@options)
91+
align(key, width) << colorize(' => ', :hash) << value.ai(@options.merge(current_indentation: inspector.current_indentation))
9292
end
9393

9494
def plain_single_line

lib/awesome_print/formatters/object_formatter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def format
4242
end
4343

4444
indented do
45-
key << colorize(' = ', :hash) + object.instance_variable_get(var).ai(@options)
45+
key << colorize(' = ', :hash) + object.instance_variable_get(var).ai(@options.merge(current_indentation: inspector.current_indentation))
4646
end
4747
end
4848

lib/awesome_print/formatters/struct_formatter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def format
4242
end
4343

4444
indented do
45-
key << colorize(' = ', :hash) + struct.send(var).ai(@options)
45+
key << colorize(' = ', :hash) + struct.send(var).ai(@options.merge(current_indentation: inspector.current_indentation))
4646
end
4747
end
4848

lib/awesome_print/indentator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ class Indentator
33

44
attr_reader :shift_width, :indentation
55

6-
def initialize(indentation)
7-
@indentation = indentation
6+
def initialize(indentation, current = nil)
7+
@indentation = current ? current : indentation
88
@shift_width = indentation.freeze
99
end
1010

lib/awesome_print/inspector.rb

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ class Inspector
1313

1414
def initialize(options = {})
1515
@options = {
16-
indent: 4, # Number of spaces for indenting.
17-
index: true, # Display array indices.
18-
html: false, # Use ANSI color codes rather than HTML.
19-
multiline: true, # Display in multiple lines.
20-
plain: false, # Use colors.
21-
raw: false, # Do not recursively format instance variables.
22-
sort_keys: false, # Do not sort hash keys.
23-
sort_vars: true, # Sort instance variables.
24-
limit: false, # Limit arrays & hashes. Accepts bool or int.
25-
ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output.
16+
indent: 4, # Number of spaces for indenting.
17+
current_indentation: nil, # The current amount of indentation.
18+
index: true, # Display array indices.
19+
html: false, # Use ANSI color codes rather than HTML.
20+
multiline: true, # Display in multiple lines.
21+
plain: false, # Use colors.
22+
raw: false, # Do not recursively format instance variables.
23+
sort_keys: false, # Do not sort hash keys.
24+
sort_vars: true, # Sort instance variables.
25+
limit: false, # Limit arrays & hashes. Accepts bool or int.
26+
ruby19_syntax: false, # Use Ruby 1.9 hash syntax in output.
2627
color: {
2728
args: :pale,
2829
array: :white,
@@ -51,7 +52,8 @@ def initialize(options = {})
5152
merge_options!(options)
5253

5354
@formatter = AwesomePrint::Formatter.new(self)
54-
@indentator = AwesomePrint::Indentator.new(@options[:indent].abs)
55+
56+
@indentator = AwesomePrint::Indentator.new(@options[:indent].abs, @options[:current_indentation])
5557
Thread.current[AP] ||= []
5658
end
5759

0 commit comments

Comments
 (0)