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

If @options[:indent] if negative left align hash keys

This commit is contained in:
Mike Dvorkin 2010-04-04 20:10:16 -07:00
parent c6acd66ee8
commit 29d041778e
2 changed files with 30 additions and 8 deletions

View file

@ -29,7 +29,7 @@ class AwesomePrint
}.merge(options.delete(:color) || {})
}.merge(options)
@indentation = @options[:indent]
@indentation = @options[:indent].abs
Thread.current[AP] ||= []
end
@ -58,7 +58,7 @@ class AwesomePrint
end
end
# Format a hash.
# Format a hash. If @options[:indent] if negative left align hash keys.
#------------------------------------------------------------------------------
def awesome_hash(h)
data = h.keys.inject([]) do |arr, key|
@ -67,12 +67,17 @@ class AwesomePrint
end
end
width = data.map { |key, | key.size }.max + @indentation
width = data.map { |key, | key.size }.max
width += @indentation if @options[:indent] > 0
data = data.inject([]) do |arr, (key, value)|
formatted_key = (@options[:multiline] ? key.rjust(width) : key) << colorize(" => ", :hash)
if @options[:multiline]
formatted_key = (@options[:indent] > 0 ? key.rjust(width) : indent + key.ljust(width))
else
formatted_key = key
end
indented do
arr << (formatted_key << awesome(value))
arr << (formatted_key << colorize(" => ", :hash) << awesome(value))
end
end
if @options[:multiline]
@ -175,10 +180,10 @@ class AwesomePrint
#------------------------------------------------------------------------------
def indented
@indentation += @options[:indent]
@indentation += @options[:indent].abs
yield
ensure
@indentation -= @options[:indent]
@indentation -= @options[:indent].abs
end
#------------------------------------------------------------------------------
@ -188,7 +193,7 @@ class AwesomePrint
#------------------------------------------------------------------------------
def outdent
@outdent = ' ' * (@indentation - @options[:indent])
@outdent = ' ' * (@indentation - @options[:indent].abs)
end
end

View file

@ -227,6 +227,23 @@ EOS
end
end
#------------------------------------------------------------------------------
describe "Negative options[:indent]" do
before(:each) do
@hash = { [0, 0, 255] => :yellow, :red => "rgb(255, 0, 0)", "magenta" => "rgb(255, 0, 255)" }
end
it "hash keys must be left aligned" do
ap = AwesomePrint.new(:plain => true, :indent => -4)
out = ap.send(:awesome, @hash)
out.start_with?("{\n").should == true
out.include?(' :red => "rgb(255, 0, 0)"').should == true
out.include?(' "magenta" => "rgb(255, 0, 255)"').should == true
out.include?(' [ 0, 0, 255 ] => :yellow').should == true
out.end_with?("\n}").should == true
end
end
#------------------------------------------------------------------------------
describe "Class" do
it "shoud show superclass (plain)" do