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
17
README.md
17
README.md
|
@ -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 foo: 'bar' syntax, when the key is a symbol
|
||||||
:color => {
|
:color => {
|
||||||
:args => :pale,
|
:args => :pale,
|
||||||
:array => :white,
|
:array => :white,
|
||||||
|
|
|
@ -43,7 +43,11 @@ module AwesomePrint
|
||||||
|
|
||||||
@data = @data.map do |key, value|
|
@data = @data.map do |key, value|
|
||||||
formatter.indented do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -65,6 +69,19 @@ module AwesomePrint
|
||||||
ensure
|
ensure
|
||||||
options[:plain], options[:multiline] = plain, multiline
|
options[:plain], options[:multiline] = plain, multiline
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,14 +6,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 foo: 'bar' syntax, when the key is a symbol
|
||||||
:color => {
|
:color => {
|
||||||
:args => :pale,
|
:args => :pale,
|
||||||
:array => :white,
|
:array => :white,
|
||||||
|
|
|
@ -275,6 +275,24 @@ 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 +329,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
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue