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:
parent
3f248971fd
commit
188becfadf
4 changed files with 70 additions and 17 deletions
|
@ -41,6 +41,7 @@ Default options:
|
|||
: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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -14,6 +14,7 @@ module AwesomePrint
|
|||
: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,
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue