From 34f268f66c852709415c96dbb803794fc645b5b3 Mon Sep 17 00:00:00 2001 From: Michael Mullis Date: Mon, 10 May 2010 17:44:13 -0400 Subject: [PATCH] added support for Struct --- lib/ap/awesome_print.rb | 22 ++++++++++++++++-- spec/awesome_print_spec.rb | 47 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/lib/ap/awesome_print.rb b/lib/ap/awesome_print.rb index be5f9f93..0ea5bf74 100755 --- a/lib/ap/awesome_print.rb +++ b/lib/ap/awesome_print.rb @@ -7,7 +7,7 @@ class AwesomePrint AP = :__awesome_print__ - CORE = [ :array, :hash, :class, :file, :dir, :bigdecimal, :rational ] + CORE = [ :array, :hash, :class, :file, :dir, :bigdecimal, :rational, :struct ] def initialize(options = {}) @options = { @@ -23,6 +23,7 @@ def initialize(options = {}) :fixnum => :blue, :float => :blue, :hash => :pale, + :struct => :pale, :nilclass => :red, :string => :yellowish, :symbol => :cyanish, @@ -92,6 +93,14 @@ def awesome_hash(h) end end + # Format a Struct. If @options[:indent] if negative left align hash keys. + #------------------------------------------------------------------------------ + def awesome_struct(s) + h = {} + s.each_pair { |k,v| h[k] = v } + awesome_hash(h) + end + # Format Class object. #------------------------------------------------------------------------------ def awesome_class(c) @@ -154,6 +163,7 @@ def nested(object) case printable(object) when :array then colorize("[...]", :array) when :hash then colorize("{...}", :hash) + when :struct then colorize("{...}", :struct) else colorize("...#{object.class}...", :class) end end @@ -167,12 +177,20 @@ def printable(object) # Turn class name into symbol, ex: Hello::World => :hello_world. #------------------------------------------------------------------------------ def declassify(object) - object.class.to_s.gsub(/:+/, "_").downcase.to_sym + if object.class.to_s.downcase =~ /^struct/ + result = :struct + else + result = object.class.to_s.gsub(/:+/, "_").downcase.to_sym + end + result end # Pick the color and apply it to the given string as necessary. #------------------------------------------------------------------------------ def colorize(s, type) + if type && !type.to_s.empty? + type = type.to_s.gsub(/^struct_.*/,'struct').to_sym + end if @options[:plain] || @options[:color][type].nil? s else diff --git a/spec/awesome_print_spec.rb b/spec/awesome_print_spec.rb index 3dd777f0..db7b3ed6 100644 --- a/spec/awesome_print_spec.rb +++ b/spec/awesome_print_spec.rb @@ -288,4 +288,51 @@ end end + + #------------------------------------------------------------------------------ + describe "Struct" do + before(:each) do + struct = Struct.new("SimpleStruct", :name, :address) + @struct = struct.new + @struct.name = "Herman Munster" + @struct.address = "1313 Mockingbird Lane" + end + + it "empty struct" do + Struct.new("EmptyStruct").ai.should == "\e[1;33mStruct::EmptyStruct < Struct\e[0m" + end + + it "plain multiline" do + @struct.ai(:plain => true).should == <<-EOS.strip +{ + :address => "1313 Mockingbird Lane", + :name => "Herman Munster" +} +EOS + end + + it "plain multiline indented" do + @struct.ai(:plain => true, :indent => 1).should == <<-EOS.strip +{ + :address => "1313 Mockingbird Lane", + :name => "Herman Munster" +} +EOS + end + + it "plain single line" do + @struct.ai(:plain => true, :multiline => false).should == "{ :address => \"1313 Mockingbird Lane\", :name => \"Herman Munster\" }" + end + + it "colored multiline (default)" do + @struct.ai.should == <<-EOS.strip +{ + :address\e[0;37m => \e[0m\e[0;33m\"1313 Mockingbird Lane\"\e[0m, + :name\e[0;37m => \e[0m\e[0;33m\"Herman Munster\"\e[0m +} +EOS + end + end + + end