From 188becfadfbff3b5fd952e0366bf36c8c6f59b59 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Wed, 18 Mar 2015 19:48:00 -0300 Subject: [PATCH] Add the new_hash_syntax syntax option --- README.md | 17 +++++++------- lib/awesome_print/formatters/hash.rb | 19 +++++++++++++++- lib/awesome_print/inspector.rb | 17 +++++++------- spec/formats_spec.rb | 34 ++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index e7cf4fa..f5e6f06 100644 --- a/README.md +++ b/README.md @@ -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, diff --git a/lib/awesome_print/formatters/hash.rb b/lib/awesome_print/formatters/hash.rb index ab34ccb..0d069c7 100644 --- a/lib/awesome_print/formatters/hash.rb +++ b/lib/awesome_print/formatters/hash.rb @@ -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 diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index dc3d257..94634a7 100644 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -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, diff --git a/spec/formats_spec.rb b/spec/formats_spec.rb index 91c8b0b..cf76cb6 100644 --- a/spec/formats_spec.rb +++ b/spec/formats_spec.rb @@ -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 {