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

Merge pull request #245 from waldyr/feature/new-hash-syntax

New hash syntax option
This commit is contained in:
Waldyr de Souza 2016-06-10 10:51:34 -03:00 committed by GitHub
commit 3c90d58575
4 changed files with 68 additions and 17 deletions

View file

@ -33,14 +33,15 @@ ap object, options = {}
Default options: Default options:
```ruby ```ruby
:indent => 4, # Indent using 4 spaces. :indent => 4, # Indent using 4 spaces.
:index => true, # Display array indices. :index => true, # Display array indices.
:html => false, # Use ANSI color codes rather than HTML. :html => false, # Use ANSI color codes rather than HTML.
:multiline => true, # Display in multiple lines. :multiline => true, # Display in multiple lines.
:plain => false, # Use colors. :plain => false, # Use colors.
:raw => false, # Do not recursively format object instance variables. :raw => false, # Do not recursively format object instance variables.
:sort_keys => false, # Do not sort hash keys. :sort_keys => false, # Do not sort hash keys.
:limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer. :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 => { :color => {
:args => :pale, :args => :pale,
:array => :white, :array => :white,

View file

@ -28,7 +28,11 @@ module AwesomePrint
data = data.map do |key, value| data = data.map do |key, value|
indented do 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
end end
@ -42,6 +46,19 @@ module AwesomePrint
private 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 def plain_single_line
plain, multiline = options[:plain], options[:multiline] plain, multiline = options[:plain], options[:multiline]
options[:plain], options[:multiline] = true, false options[:plain], options[:multiline] = true, false

View file

@ -56,14 +56,15 @@ module AwesomePrint
def initialize(options = {}) def initialize(options = {})
@options = { @options = {
:indent => 4, # Indent using 4 spaces. :indent => 4, # Indent using 4 spaces.
:index => true, # Display array indices. :index => true, # Display array indices.
:html => false, # Use ANSI color codes rather than HTML. :html => false, # Use ANSI color codes rather than HTML.
:multiline => true, # Display in multiple lines. :multiline => true, # Display in multiple lines.
:plain => false, # Use colors. :plain => false, # Use colors.
:raw => false, # Do not recursively format object instance variables. :raw => false, # Do not recursively format object instance variables.
:sort_keys => false, # Do not sort hash keys. :sort_keys => false, # Do not sort hash keys.
:limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer. :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 => { :color => {
:args => :pale, :args => :pale,
:array => :white, :array => :white,

View file

@ -275,6 +275,22 @@ EOS
EOS EOS
end 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 it "plain multiline indented" do
expect(@hash.ai(:plain => true, :indent => 1)).to eq <<-EOS.strip expect(@hash.ai(:plain => true, :indent => 1)).to eq <<-EOS.strip
{ {
@ -311,6 +327,22 @@ EOS
EOS EOS
end 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 it "colored multiline indented" do
expect(@hash.ai(:indent => 2)).to eq <<-EOS.strip expect(@hash.ai(:indent => 2)).to eq <<-EOS.strip
{ {