Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
If @options[:indent] if negative left align hash keys
  • Loading branch information
Mike Dvorkin committed Apr 5, 2010
1 parent c6acd66 commit 29d0417
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
21 changes: 13 additions & 8 deletions lib/ap/awesome_print.rb
Expand Up @@ -29,7 +29,7 @@ def initialize(options = {})
}.merge(options.delete(:color) || {})
}.merge(options)

@indentation = @options[:indent]
@indentation = @options[:indent].abs
Thread.current[AP] ||= []
end

Expand Down Expand Up @@ -58,7 +58,7 @@ def awesome_array(a)
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|
Expand All @@ -67,12 +67,17 @@ def awesome_hash(h)
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]
Expand Down Expand Up @@ -175,10 +180,10 @@ def plain_single_line

#------------------------------------------------------------------------------
def indented
@indentation += @options[:indent]
@indentation += @options[:indent].abs
yield
ensure
@indentation -= @options[:indent]
@indentation -= @options[:indent].abs
end

#------------------------------------------------------------------------------
Expand All @@ -188,7 +193,7 @@ def indent

#------------------------------------------------------------------------------
def outdent
@outdent = ' ' * (@indentation - @options[:indent])
@outdent = ' ' * (@indentation - @options[:indent].abs)
end

end
17 changes: 17 additions & 0 deletions spec/awesome_print_spec.rb
Expand Up @@ -227,6 +227,23 @@
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
Expand Down

0 comments on commit 29d0417

Please sign in to comment.