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

added support for Struct

(cherry picked from commit 34f268f66c852709415c96dbb803794fc645b5b3)
This commit is contained in:
Michael Mullis 2010-05-10 17:44:13 -04:00 committed by Mike Dvorkin
parent ba89f3ddaf
commit 92dfe6df04
2 changed files with 67 additions and 2 deletions

View file

@ -7,7 +7,7 @@ require "shellwords"
class AwesomePrint class AwesomePrint
AP = :__awesome_print__ AP = :__awesome_print__
CORE = [ :array, :hash, :class, :file, :dir, :bigdecimal, :rational ] CORE = [ :array, :hash, :class, :file, :dir, :bigdecimal, :rational, :struct ]
def initialize(options = {}) def initialize(options = {})
@options = { @options = {
@ -23,6 +23,7 @@ class AwesomePrint
:fixnum => :blue, :fixnum => :blue,
:float => :blue, :float => :blue,
:hash => :pale, :hash => :pale,
:struct => :pale,
:nilclass => :red, :nilclass => :red,
:string => :yellowish, :string => :yellowish,
:symbol => :cyanish, :symbol => :cyanish,
@ -92,6 +93,14 @@ class AwesomePrint
end end
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. # Format Class object.
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
def awesome_class(c) def awesome_class(c)
@ -154,6 +163,7 @@ class AwesomePrint
case printable(object) case printable(object)
when :array then colorize("[...]", :array) when :array then colorize("[...]", :array)
when :hash then colorize("{...}", :hash) when :hash then colorize("{...}", :hash)
when :struct then colorize("{...}", :struct)
else colorize("...#{object.class}...", :class) else colorize("...#{object.class}...", :class)
end end
end end
@ -167,12 +177,20 @@ class AwesomePrint
# Turn class name into symbol, ex: Hello::World => :hello_world. # Turn class name into symbol, ex: Hello::World => :hello_world.
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
def declassify(object) 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 end
# Pick the color and apply it to the given string as necessary. # Pick the color and apply it to the given string as necessary.
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
def colorize(s, type) 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? if @options[:plain] || @options[:color][type].nil?
s s
else else

View file

@ -288,4 +288,51 @@ EOS
end end
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 end