1
0
Fork 0
mirror of https://github.com/awesome-print/awesome_print synced 2023-03-27 23:22:34 -04:00

Add new hash syntax option to hash_formatter

This commit is contained in:
Mauro George 2016-06-07 23:10:48 -03:00 committed by Waldyr de Souza
parent 7ddc36bde6
commit 239c990a13
2 changed files with 27 additions and 9 deletions

View file

@ -28,7 +28,11 @@ module AwesomePrint
data = data.map do |key, value|
indented do
align(key, width) << colorize(" => ", :hash) << inspector.awesome(value)
if options[:new_hash_syntax] && symbol?(key)
new_hash_syntax(key, value, width)
else
old_hash_syntax(key, value, width)
end
end
end
@ -42,6 +46,19 @@ module AwesomePrint
private
def symbol?(k)
k.first == ':'
end
def new_hash_syntax(k, v, width)
k[0] = ''
align(k, width - 1) << colorize(": ", :hash) << inspector.awesome(v)
end
def old_hash_syntax(k, v, width)
align(k, width) << colorize(" => ", :hash) << inspector.awesome(v)
end
def plain_single_line
plain, multiline = options[:plain], options[:multiline]
options[:plain], options[:multiline] = true, false

View file

@ -56,14 +56,15 @@ module AwesomePrint
def initialize(options = {})
@options = {
:indent => 4, # Indent using 4 spaces.
: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 object instance variables.
:sort_keys => false, # Do not sort hash keys.
:limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer.
:indent => 4, # Indent using 4 spaces.
: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 object instance variables.
:sort_keys => false, # Do not sort hash keys.
:limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer.
:new_hash_syntax => false, # Use the JSON like syntax { foo: 'bar' }, when the key is a symbol
:color => {
:args => :pale,
:array => :white,