From d1fe8d0de89fd7b8f70c08b5c436d35d18b0f623 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Tue, 7 Jun 2016 23:09:50 -0300 Subject: [PATCH 1/3] Add hash syntax option to README --- README.md | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f2da91e..076762b 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 JSON like syntax { foo: 'bar' }, when the key is a symbol :color => { :args => :pale, :array => :white, From 7ddc36bde6a1895d5228e3083afbcb156e48580b Mon Sep 17 00:00:00 2001 From: Mauro George Date: Tue, 7 Jun 2016 23:10:24 -0300 Subject: [PATCH 2/3] Add spec for hash syntax option --- spec/formats_spec.rb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/spec/formats_spec.rb b/spec/formats_spec.rb index f806bf3..e6d5421 100644 --- a/spec/formats_spec.rb +++ b/spec/formats_spec.rb @@ -275,6 +275,22 @@ 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 +327,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 { From 239c990a1325f403f93112ff872e31c21f6c8784 Mon Sep 17 00:00:00 2001 From: Mauro George Date: Tue, 7 Jun 2016 23:10:48 -0300 Subject: [PATCH 3/3] Add new hash syntax option to hash_formatter --- .../formatters/hash_formatter.rb | 19 ++++++++++++++++++- lib/awesome_print/inspector.rb | 17 +++++++++-------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/lib/awesome_print/formatters/hash_formatter.rb b/lib/awesome_print/formatters/hash_formatter.rb index 1435e43..92d9ca2 100644 --- a/lib/awesome_print/formatters/hash_formatter.rb +++ b/lib/awesome_print/formatters/hash_formatter.rb @@ -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 diff --git a/lib/awesome_print/inspector.rb b/lib/awesome_print/inspector.rb index c519322..fcd0e98 100644 --- a/lib/awesome_print/inspector.rb +++ b/lib/awesome_print/inspector.rb @@ -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,