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

Add the new_hash_syntax syntax option

This commit is contained in:
Mauro George 2015-03-18 19:48:00 -03:00
parent 3f248971fd
commit 188becfadf
4 changed files with 70 additions and 17 deletions

View file

@ -33,14 +33,15 @@ ap object, options = {}
Default options:
```ruby
: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 foo: 'bar' syntax, when the key is a symbol
:color => {
:args => :pale,
:array => :white,

View file

@ -43,7 +43,11 @@ module AwesomePrint
@data = @data.map do |key, value|
formatter.indented do
formatter.align(key, width) << formatter.colorize(" => ", :hash) << inspector.awesome(value)
if options[:new_hash_syntax] && is_a_symbol?(key)
new_hash_syntax_format(key, value)
else
old_hash_syntax_format(key, value)
end
end
end
@ -65,6 +69,19 @@ module AwesomePrint
ensure
options[:plain], options[:multiline] = plain, multiline
end
def old_hash_syntax_format(key, value)
formatter.align(key, width) << formatter.colorize(" => ", :hash) << inspector.awesome(value)
end
def new_hash_syntax_format(key, value)
key[0] = ''
formatter.align(key, width - 1) << formatter.colorize(": ", :hash) << inspector.awesome(value)
end
def is_a_symbol?(key)
key[0] == ':'
end
end
end
end

View file

@ -6,14 +6,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 foo: 'bar' syntax, when the key is a symbol
:color => {
:args => :pale,
:array => :white,

View file

@ -275,6 +275,24 @@ EOS
EOS
end
it "new hash syntax" do
expect(@hash.ai(:plain => true, :new_hash_syntax => true)).to eq <<-EOS.strip
{
1 => {
sym: {
"str" => {
[ 1, 2, 3 ] => {
{ k: :v } => Hash < Object
}
}
}
}
}
EOS
end
it "plain multiline indented" do
expect(@hash.ai(:plain => true, :indent => 1)).to eq <<-EOS.strip
{
@ -311,6 +329,22 @@ EOS
EOS
end
it "colored with new hash syntax" do
expect(@hash.ai(:new_hash_syntax => true)).to eq <<-EOS.strip
{
1\e[0;37m => \e[0m{
sym\e[0;37m: \e[0m{
\"str\"\e[0;37m => \e[0m{
[ 1, 2, 3 ]\e[0;37m => \e[0m{
{ k: :v }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m
}
}
}
}
}
EOS
end
it "colored multiline indented" do
expect(@hash.ai(:indent => 2)).to eq <<-EOS.strip
{