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:
commit
3c90d58575
4 changed files with 68 additions and 17 deletions
17
README.md
17
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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue