Skip to content

Commit

Permalink
Made some improvements to Kernel#ai
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
matrinox committed Apr 18, 2017
1 parent efbbad8 commit 686c188
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 22 deletions.
6 changes: 3 additions & 3 deletions lib/awesome_print/core_ext/kernel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<pre>#{awesome}</pre>"
awesome = "<pre>#{awesome}</pre>" unless options[:top_layer] == false
awesome = awesome.html_safe if defined? ActiveSupport
end
awesome
Expand Down
4 changes: 2 additions & 2 deletions lib/awesome_print/formatters/array_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/awesome_print/formatters/hash_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/awesome_print/formatters/object_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/awesome_print/formatters/struct_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/awesome_print/indentator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 13 additions & 11 deletions lib/awesome_print/inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 686c188

Please sign in to comment.