jonbell-awesome_print 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2010 Michael Dvorkin
2
+ %w(mike dvorkin.net) * "@" || %w(mike fatfreecrm.com) * "@"
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,162 @@
1
+ ## Awesome Print ##
2
+ Awesome Print is Ruby library that pretty prints Ruby objects in full color
3
+ exposing their internal structure with proper indentation. Rails ActiveRecord
4
+ objects are supported via included mixin.
5
+
6
+ ### Installation ###
7
+ # Installing as Ruby gem
8
+ $ gem install awesome_print
9
+
10
+ # Installing as Rails plugin
11
+ $ ruby script/plugin install http://github.com/michaeldv/awesome_print_.git
12
+
13
+ # Cloning the repository
14
+ $ git clone git://github.com/michaeldv/awesome_print_.git
15
+
16
+ ### Usage ###
17
+
18
+ require "ap"
19
+ ap object, options = {}
20
+
21
+ Default options:
22
+
23
+ :multiline => true,
24
+ :plain => false,
25
+ :indent => 4,
26
+ :colors => {
27
+ :array => :white,
28
+ :bignum => :blue,
29
+ :class => :yellow,
30
+ :date => :greenish,
31
+ :falseclass => :red,
32
+ :fixnum => :blue,
33
+ :float => :blue,
34
+ :hash => :gray,
35
+ :nilclass => :red,
36
+ :string => :yellowish,
37
+ :symbol => :cyanish,
38
+ :time => :greenish,
39
+ :trueclass => :green
40
+ }
41
+
42
+ Supported color names:
43
+
44
+ :gray, :red, :green, :yellow, :blue, :purple, :cyan, :white
45
+ :black, :redish, :greenish, :yellowish, :blueish, :purpleish, :cyanish, :pale
46
+
47
+ ### Examples ###
48
+ $ cat > 1.rb
49
+ require "ap"
50
+ data = [ false, 42, %w(fourty two), { :now => Time.now, :class => Time.now.class, :distance => 42e42 } ]
51
+ ap data
52
+ ^D
53
+ $ ruby 1.rb
54
+ [
55
+ [0] false,
56
+ [1] 42,
57
+ [2] [
58
+ [0] "fourty",
59
+ [1] "two"
60
+ ],
61
+ [3] {
62
+ :class => Time < Object,
63
+ :now => Fri Apr 02 19:55:53 -0700 2010,
64
+ :distance => 4.2e+43
65
+ }
66
+ ]
67
+
68
+ $ cat > 2.rb
69
+ require "ap"
70
+ data = { :now => Time.now, :class => Time.now.class, :distance => 42e42 }
71
+ ap data, :indent => -2 # <-- Left align hash keys.
72
+ ^D
73
+ $ ruby 2.rb
74
+ {
75
+ :class => Time < Object,
76
+ :now => Fri Apr 02 19:55:53 -0700 2010,
77
+ :distance => 4.2e+43
78
+ }
79
+
80
+ $ cat > 3.rb
81
+ require "ap"
82
+ data = [ false, 42, %w(fourty two) ]
83
+ data << data # <-- Nested array.
84
+ ap data, :multiline => false
85
+ ^D
86
+ $ ruby 3.rb
87
+ [ false, 42, [ "fourty", "two" ], [...] ]
88
+
89
+ ### Example (Rails console) ###
90
+ $ ruby script/console
91
+ Loading development environment (Rails 2.3.5)
92
+ rails> require "ap"
93
+ rails> ap Account.all(:limit => 2)
94
+ [
95
+ [0] #<Account:0x1033220b8> {
96
+ :id => 1,
97
+ :user_id => 5,
98
+ :assigned_to => 7,
99
+ :name => "Hayes-DuBuque",
100
+ :access => "Public",
101
+ :website => "http://www.hayesdubuque.com",
102
+ :toll_free_phone => "1-800-932-6571",
103
+ :phone => "(111)549-5002",
104
+ :fax => "(349)415-2266",
105
+ :deleted_at => nil,
106
+ :created_at => Sat, 06 Mar 2010 09:46:10 UTC +00:00,
107
+ :updated_at => Sat, 06 Mar 2010 16:33:10 UTC +00:00,
108
+ :email => "info@hayesdubuque.com",
109
+ :background_info => nil
110
+ },
111
+ [1] #<Account:0x103321ff0> {
112
+ :id => 2,
113
+ :user_id => 4,
114
+ :assigned_to => 4,
115
+ :name => "Ziemann-Streich",
116
+ :access => "Public",
117
+ :website => "http://www.ziemannstreich.com",
118
+ :toll_free_phone => "1-800-871-0619",
119
+ :phone => "(042)056-1534",
120
+ :fax => "(106)017-8792",
121
+ :deleted_at => nil,
122
+ :created_at => Tue, 09 Feb 2010 13:32:10 UTC +00:00,
123
+ :updated_at => Tue, 09 Feb 2010 20:05:01 UTC +00:00,
124
+ :email => "info@ziemannstreich.com",
125
+ :background_info => nil
126
+ }
127
+ ]
128
+ rails> ap Account
129
+ class Account < ActiveRecord::Base {
130
+ :id => :integer,
131
+ :user_id => :integer,
132
+ :assigned_to => :integer,
133
+ :name => :string,
134
+ :access => :string,
135
+ :website => :string,
136
+ :toll_free_phone => :string,
137
+ :phone => :string,
138
+ :fax => :string,
139
+ :deleted_at => :datetime,
140
+ :created_at => :datetime,
141
+ :updated_at => :datetime,
142
+ :email => :string,
143
+ :background_info => :string
144
+ }
145
+ rails>
146
+
147
+ ### Known Issues ###
148
+
149
+ * Windows...
150
+
151
+ ### Note on Patches/Pull Requests ###
152
+ * Fork the project on Github.
153
+ * Make your feature addition or bug fix.
154
+ * Add specs for it, making sure $ rake spec is all green.
155
+ * Commit, do not mess with rakefile, version, or history.
156
+ * Send me a pull request.
157
+
158
+ ### License ###
159
+ Copyright (c) 2010 Michael Dvorkin
160
+ %w(mike dvorkin.net) * "@" || %w(mike fatfreecrm.com) * "@"
161
+
162
+ Released under the MIT license. See LICENSE file for details.
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "jonbell-awesome_print"
8
+ gem.rubyforge_project = "awesome_print"
9
+ gem.summary = %Q{Pretty print Ruby objects with proper indentation and colors.}
10
+ gem.description = %Q{Great Ruby dubugging companion: pretty print Ruby objects to visualize their structure. Supports MongoMapper objects via included mixin.}
11
+ gem.email = "jonbell@spamcop.net"
12
+ gem.homepage = "http://github.com/jonbell/awesome_print"
13
+ gem.authors = ["Michael Dvorkin", "Jonathan Bell"]
14
+ gem.add_development_dependency "rspec", ">= 1.2.9"
15
+ gem.files = FileList["[A-Z]*", "lib/**/*.rb", "rails/*.rb", "spec/*", "init.rb"]
16
+ gem.has_rdoc = false
17
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
22
+ end
23
+
24
+ require 'rake/rdoctask'
25
+ Rake::RDocTask.new do |rdoc|
26
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
27
+
28
+ rdoc.rdoc_dir = 'rdoc'
29
+ rdoc.title = "ap #{version}"
30
+ rdoc.rdoc_files.include('README*')
31
+ rdoc.rdoc_files.include('lib/**/*.rb')
32
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.2
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), "lib", "ap")
data/lib/ap.rb ADDED
@@ -0,0 +1,10 @@
1
+ # Copyright (c) 2010 Michael Dvorkin
2
+ #
3
+ # Awesome Print is freely distributable under the terms of MIT license.
4
+ # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
+ #------------------------------------------------------------------------------
6
+ require File.dirname(__FILE__) + "/ap/core_ext/string"
7
+ require File.dirname(__FILE__) + "/ap/core_ext/kernel"
8
+ require File.dirname(__FILE__) + "/ap/awesome_print"
9
+
10
+ require File.dirname(__FILE__) + "/ap/mixin/rails" if defined?(::Rails)
@@ -0,0 +1,199 @@
1
+ # Copyright (c) 2010 Michael Dvorkin
2
+ #
3
+ # Awesome Print is freely distributable under the terms of MIT license.
4
+ # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
+ #------------------------------------------------------------------------------
6
+ class AwesomePrint
7
+ AP = :__awesome_print__
8
+ CORE = [ :array, :hash, :class, :file, :dir ]
9
+
10
+ def initialize(options = {})
11
+ @options = {
12
+ :multiline => true,
13
+ :plain => false,
14
+ :indent => 4,
15
+ :color => {
16
+ :array => :white,
17
+ :bignum => :blue,
18
+ :class => :yellow,
19
+ :date => :greenish,
20
+ :falseclass => :red,
21
+ :fixnum => :blue,
22
+ :float => :blue,
23
+ :hash => :gray,
24
+ :nilclass => :red,
25
+ :string => :yellowish,
26
+ :symbol => :cyanish,
27
+ :time => :greenish,
28
+ :trueclass => :green
29
+ }.merge(options.delete(:color) || {})
30
+ }.merge(options)
31
+
32
+ @indentation = @options[:indent].abs
33
+ Thread.current[AP] ||= []
34
+ end
35
+
36
+
37
+ private
38
+
39
+ # Format an array.
40
+ #------------------------------------------------------------------------------
41
+ def awesome_array(a)
42
+ return "[]" if a == []
43
+
44
+ if @options[:multiline]
45
+ width = (a.size - 1).to_s.size
46
+ data = a.inject([]) do |arr, item|
47
+ index = colorize("#{indent}[#{arr.size.to_s.rjust(width)}] ", :array)
48
+ indented do
49
+ arr << (index << awesome(item))
50
+ end
51
+ end
52
+ "[\n" << data.join(",\n") << "\n#{outdent}]"
53
+ else
54
+ data = a.inject([]) { |arr, item| arr << awesome(item) }
55
+ "[ #{data.join(', ')} ]"
56
+ end
57
+ end
58
+
59
+ # Format a hash. If @options[:indent] if negative left align hash keys.
60
+ #------------------------------------------------------------------------------
61
+ def awesome_hash(h)
62
+ return "{}" if h == {}
63
+
64
+ data = h.keys.inject([]) do |arr, key|
65
+ plain_single_line do
66
+ arr << [ awesome(key), h[key] ]
67
+ end
68
+ end
69
+
70
+ width = data.map { |key, | key.size }.max || 0
71
+ width += @indentation if @options[:indent] > 0
72
+
73
+ data = data.inject([]) do |arr, (key, value)|
74
+ if @options[:multiline]
75
+ formatted_key = (@options[:indent] >= 0 ? key.rjust(width) : indent + key.ljust(width))
76
+ else
77
+ formatted_key = key
78
+ end
79
+ indented do
80
+ arr << (formatted_key << colorize(" => ", :hash) << awesome(value))
81
+ end
82
+ end
83
+ if @options[:multiline]
84
+ "{\n" << data.join(",\n") << "\n#{outdent}}"
85
+ else
86
+ "{ #{data.join(', ')} }"
87
+ end
88
+ end
89
+
90
+ # Format Class object.
91
+ #------------------------------------------------------------------------------
92
+ def awesome_class(c)
93
+ if superclass = c.superclass # <-- Assign and test if nil.
94
+ awesome_self(c, :with => " < #{superclass}")
95
+ else
96
+ awesome_self(c)
97
+ end
98
+ end
99
+
100
+ # Format File object.
101
+ #------------------------------------------------------------------------------
102
+ def awesome_file(f)
103
+ ls = File.directory?(f) ? `ls -adlF #{f.path}` : `ls -alF #{f.path}`
104
+ awesome_self(f, :with => ls.empty? ? nil : "\n#{ls.chop}")
105
+ end
106
+
107
+ # Format Dir object.
108
+ #------------------------------------------------------------------------------
109
+ def awesome_dir(d)
110
+ ls = `ls -alF #{d.path}`
111
+ awesome_self(d, :with => ls.empty? ? nil : "\n#{ls.chop}")
112
+ end
113
+
114
+ # Catch all method to format an arbitrary object.
115
+ #------------------------------------------------------------------------------
116
+ def awesome_self(object, appear = {})
117
+ colorize(object.inspect << appear[:with].to_s, appear[:as] || declassify(object))
118
+ end
119
+
120
+ # Dispatcher that detects data nesting and invokes object-aware formatter.
121
+ #------------------------------------------------------------------------------
122
+ def awesome(object)
123
+ if Thread.current[AP].include?(object.object_id)
124
+ nested(object)
125
+ else
126
+ begin
127
+ Thread.current[AP] << object.object_id
128
+ send(:"awesome_#{printable(object)}", object)
129
+ ensure
130
+ Thread.current[AP].pop
131
+ end
132
+ end
133
+ end
134
+
135
+ # Format nested data, for example:
136
+ # arr = [1, 2]; arr << arr
137
+ # => [1,2, [...]]
138
+ # hsh = { :a => 1 }; hsh[:b] = hsh
139
+ # => { :a => 1, :b => {...} }
140
+ #------------------------------------------------------------------------------
141
+ def nested(object)
142
+ case printable(object)
143
+ when :array then colorize("[...]", :array)
144
+ when :hash then colorize("{...}", :hash)
145
+ else colorize("...#{object.class}...", :class)
146
+ end
147
+ end
148
+
149
+ # Return one of the "core" types that have a formatter of :self otherwise.
150
+ #------------------------------------------------------------------------------
151
+ def printable(object)
152
+ CORE.grep(declassify(object))[0] || :self
153
+ end
154
+
155
+ # Turn class name into symbol, ex: Hello::World => :hello_world.
156
+ #------------------------------------------------------------------------------
157
+ def declassify(object)
158
+ object.class.to_s.gsub(/:+/, "_").downcase.to_sym
159
+ end
160
+
161
+ # Pick the color and apply it to the given string as necessary.
162
+ #------------------------------------------------------------------------------
163
+ def colorize(s, type)
164
+ if @options[:plain] || @options[:color][type].nil?
165
+ s
166
+ else
167
+ s.send(@options[:color][type])
168
+ end
169
+ end
170
+
171
+ # Format hash keys as plain string regardless of underlying data type.
172
+ #------------------------------------------------------------------------------
173
+ def plain_single_line
174
+ plain, multiline = @options[:plain], @options[:multiline]
175
+ @options[:plain], @options[:multiline] = true, false
176
+ yield
177
+ ensure
178
+ @options[:plain], @options[:multiline] = plain, multiline
179
+ end
180
+
181
+ #------------------------------------------------------------------------------
182
+ def indented
183
+ @indentation += @options[:indent].abs
184
+ yield
185
+ ensure
186
+ @indentation -= @options[:indent].abs
187
+ end
188
+
189
+ #------------------------------------------------------------------------------
190
+ def indent
191
+ @indent = ' ' * @indentation
192
+ end
193
+
194
+ #------------------------------------------------------------------------------
195
+ def outdent
196
+ @outdent = ' ' * (@indentation - @options[:indent].abs)
197
+ end
198
+
199
+ end
@@ -0,0 +1,20 @@
1
+ # Copyright (c) 2010 Michael Dvorkin
2
+ #
3
+ # Awesome Print is freely distributable under the terms of MIT license.
4
+ # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
+ #------------------------------------------------------------------------------
6
+ module Kernel
7
+
8
+ def ai(options = {})
9
+ ap = AwesomePrint.new(options)
10
+ ap.send(:awesome, self)
11
+ end
12
+ alias :awesome_inspect :ai
13
+
14
+ def ap(object, options = {})
15
+ puts object.ai(options)
16
+ end
17
+ alias :awesome_print :ap
18
+
19
+ module_function :ap
20
+ end
@@ -0,0 +1,21 @@
1
+ # Copyright (c) 2010 Michael Dvorkin
2
+ #
3
+ # Awesome Print is freely distributable under the terms of MIT license.
4
+ # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
+ #------------------------------------------------------------------------------
6
+ class String
7
+
8
+ [ :gray, :red, :green, :yellow, :blue, :purple, :cyan, :white ].each_with_index do |color, i|
9
+ if STDOUT.tty? && ENV['TERM'] && ENV['TERM'] != 'dumb'
10
+ define_method color do "\033[1;#{30+i}m#{self}\033[0m" end
11
+ define_method :"#{color}ish" do "\033[0;#{30+i}m#{self}\033[0m" end
12
+ else
13
+ define_method color do self end
14
+ alias_method :"#{color}ish", color
15
+ end
16
+ end
17
+
18
+ alias :black :grayish
19
+ alias :pale :whiteish
20
+
21
+ end
@@ -0,0 +1,56 @@
1
+ # Copyright (c) 2010 Michael Dvorkin
2
+ #
3
+ # Awesome Print is freely distributable under the terms of MIT license.
4
+ # See LICENSE file or http://www.opensource.org/licenses/mit-license.php
5
+ #------------------------------------------------------------------------------
6
+ module AwesomePrintRails
7
+
8
+ def self.included(base)
9
+ base.alias_method_chain :printable, :rails
10
+ end
11
+
12
+ # Add ActiveRecord class names to the dispatcher pipeline.
13
+ #------------------------------------------------------------------------------
14
+ def printable_with_rails(object)
15
+ printable = printable_without_rails(object)
16
+ if printable == :self
17
+ if object.class.include?(MongoMapper::Document) || object.class.include?(MongoMapper::EmbeddedDocument)
18
+ printable = :mongo_mapper_instance
19
+ elsif object.is_a?(ActiveSupport::TimeWithZone)
20
+ printable = :active_support_time
21
+ end
22
+ elsif printable == :class && (object.class.include?(MongoMapper::Document) || object.class.include?(MongoMapper::EmbeddedDocument))
23
+ printable = :mongo_mapper_class
24
+ end
25
+ printable
26
+ end
27
+
28
+ # Format ActiveRecord instance object.
29
+ #------------------------------------------------------------------------------
30
+ def awesome_mongo_mapper_instance(object)
31
+ data = object.keys.keys.inject(ActiveSupport::OrderedHash.new) do |hash, name|
32
+ hash[name.to_sym] = object.send(name)
33
+ hash
34
+ end
35
+ "#{object} " + awesome_hash(data)
36
+ end
37
+
38
+ # Format ActiveRecord class object.
39
+ #------------------------------------------------------------------------------
40
+ def awesome_mongo_mapper_class(object)
41
+ data = object.keys.keys.inject(ActiveSupport::OrderedHash.new) do |hash, c|
42
+ hash[c] = object.keys[c].type
43
+ hash
44
+ end
45
+ "class #{object} < #{object.superclass} " << awesome_hash(data)
46
+ end
47
+
48
+ # Format ActiveSupport::TimeWithZone as standard Time.
49
+ #------------------------------------------------------------------------------
50
+ def awesome_active_support_time(object)
51
+ awesome_self(object, :as => :time)
52
+ end
53
+
54
+ end
55
+
56
+ AwesomePrint.send(:include, AwesomePrintRails)
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), "..", "init")
@@ -0,0 +1,262 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "AwesomePrint" do
4
+
5
+ describe "Array" do
6
+ before(:each) do
7
+ @arr = [ 1, :two, "three", [ nil, [ true, false] ] ]
8
+ end
9
+
10
+ it "empty array" do
11
+ [].ai.should == "[]"
12
+ end
13
+
14
+ it "plain multiline" do
15
+ @arr.ai(:plain => true).should == <<-EOS.strip
16
+ [
17
+ [0] 1,
18
+ [1] :two,
19
+ [2] "three",
20
+ [3] [
21
+ [0] nil,
22
+ [1] [
23
+ [0] true,
24
+ [1] false
25
+ ]
26
+ ]
27
+ ]
28
+ EOS
29
+ end
30
+
31
+ it "plain multiline indented" do
32
+ @arr.ai(:plain => true, :indent => 2).should == <<-EOS.strip
33
+ [
34
+ [0] 1,
35
+ [1] :two,
36
+ [2] "three",
37
+ [3] [
38
+ [0] nil,
39
+ [1] [
40
+ [0] true,
41
+ [1] false
42
+ ]
43
+ ]
44
+ ]
45
+ EOS
46
+ end
47
+
48
+ it "plain single line" do
49
+ @arr.ai(:plain => true, :multiline => false).should == '[ 1, :two, "three", [ nil, [ true, false ] ] ]'
50
+ end
51
+
52
+ it "colored multiline (default)" do
53
+ @arr.ai.should == <<-EOS.strip
54
+ [
55
+ \e[1;37m [0] \e[0m\e[1;34m1\e[0m,
56
+ \e[1;37m [1] \e[0m\e[0;36m:two\e[0m,
57
+ \e[1;37m [2] \e[0m\e[0;33m\"three\"\e[0m,
58
+ \e[1;37m [3] \e[0m[
59
+ \e[1;37m [0] \e[0m\e[1;31mnil\e[0m,
60
+ \e[1;37m [1] \e[0m[
61
+ \e[1;37m [0] \e[0m\e[1;32mtrue\e[0m,
62
+ \e[1;37m [1] \e[0m\e[1;31mfalse\e[0m
63
+ ]
64
+ ]
65
+ ]
66
+ EOS
67
+ end
68
+
69
+ it "colored multiline indented" do
70
+ @arr.ai(:indent => 8).should == <<-EOS.strip
71
+ [
72
+ \e[1;37m [0] \e[0m\e[1;34m1\e[0m,
73
+ \e[1;37m [1] \e[0m\e[0;36m:two\e[0m,
74
+ \e[1;37m [2] \e[0m\e[0;33m\"three\"\e[0m,
75
+ \e[1;37m [3] \e[0m[
76
+ \e[1;37m [0] \e[0m\e[1;31mnil\e[0m,
77
+ \e[1;37m [1] \e[0m[
78
+ \e[1;37m [0] \e[0m\e[1;32mtrue\e[0m,
79
+ \e[1;37m [1] \e[0m\e[1;31mfalse\e[0m
80
+ ]
81
+ ]
82
+ ]
83
+ EOS
84
+ end
85
+
86
+ it "colored single line" do
87
+ @arr.ai(:multiline => false).should == "[ \e[1;34m1\e[0m, \e[0;36m:two\e[0m, \e[0;33m\"three\"\e[0m, [ \e[1;31mnil\e[0m, [ \e[1;32mtrue\e[0m, \e[1;31mfalse\e[0m ] ] ]"
88
+ end
89
+ end
90
+
91
+ #------------------------------------------------------------------------------
92
+ describe "Nested Array" do
93
+ before(:each) do
94
+ @arr = [ 1, 2 ]
95
+ @arr << @arr
96
+ end
97
+
98
+ it "plain multiline" do
99
+ @arr.ai(:plain => true).should == <<-EOS.strip
100
+ [
101
+ [0] 1,
102
+ [1] 2,
103
+ [2] [...]
104
+ ]
105
+ EOS
106
+ end
107
+
108
+ it "plain single line" do
109
+ @arr.ai(:plain => true, :multiline => false).should == "[ 1, 2, [...] ]"
110
+ end
111
+ end
112
+
113
+ #------------------------------------------------------------------------------
114
+ describe "Hash" do
115
+ before(:each) do
116
+ @hash = { 1 => { :sym => { "str" => { [1, 2, 3] => { { :k => :v } => Hash } } } } }
117
+ end
118
+
119
+ it "empty hash" do
120
+ {}.ai.should == "{}"
121
+ end
122
+
123
+ it "plain multiline" do
124
+ @hash.ai(:plain => true).should == <<-EOS.strip
125
+ {
126
+ 1 => {
127
+ :sym => {
128
+ "str" => {
129
+ [ 1, 2, 3 ] => {
130
+ { :k => :v } => Hash < Object
131
+ }
132
+ }
133
+ }
134
+ }
135
+ }
136
+ EOS
137
+ end
138
+
139
+ it "plain multiline indented" do
140
+ @hash.ai(:plain => true, :indent => 1).should == <<-EOS.strip
141
+ {
142
+ 1 => {
143
+ :sym => {
144
+ "str" => {
145
+ [ 1, 2, 3 ] => {
146
+ { :k => :v } => Hash < Object
147
+ }
148
+ }
149
+ }
150
+ }
151
+ }
152
+ EOS
153
+ end
154
+
155
+ it "plain single line" do
156
+ @hash.ai(:plain => true, :multiline => false).should == '{ 1 => { :sym => { "str" => { [ 1, 2, 3 ] => { { :k => :v } => Hash < Object } } } } }'
157
+ end
158
+
159
+ it "colored multiline (default)" do
160
+ @hash.ai.should == <<-EOS.strip
161
+ {
162
+ 1\e[1;30m => \e[0m{
163
+ :sym\e[1;30m => \e[0m{
164
+ \"str\"\e[1;30m => \e[0m{
165
+ [ 1, 2, 3 ]\e[1;30m => \e[0m{
166
+ { :k => :v }\e[1;30m => \e[0m\e[1;33mHash < Object\e[0m
167
+ }
168
+ }
169
+ }
170
+ }
171
+ }
172
+ EOS
173
+ end
174
+
175
+ it "colored multiline indented" do
176
+ @hash.ai(:indent => 2).should == <<-EOS.strip
177
+ {
178
+ 1\e[1;30m => \e[0m{
179
+ :sym\e[1;30m => \e[0m{
180
+ \"str\"\e[1;30m => \e[0m{
181
+ [ 1, 2, 3 ]\e[1;30m => \e[0m{
182
+ { :k => :v }\e[1;30m => \e[0m\e[1;33mHash < Object\e[0m
183
+ }
184
+ }
185
+ }
186
+ }
187
+ }
188
+ EOS
189
+ end
190
+
191
+ it "colored single line" do
192
+ @hash.ai(:multiline => false).should == "{ 1\e[1;30m => \e[0m{ :sym\e[1;30m => \e[0m{ \"str\"\e[1;30m => \e[0m{ [ 1, 2, 3 ]\e[1;30m => \e[0m{ { :k => :v }\e[1;30m => \e[0m\e[1;33mHash < Object\e[0m } } } } }"
193
+ end
194
+
195
+ end
196
+
197
+ #------------------------------------------------------------------------------
198
+ describe "Nested Hash" do
199
+ before(:each) do
200
+ @hash = {}
201
+ @hash[:a] = @hash
202
+ end
203
+
204
+ it "plain multiline" do
205
+ @hash.ai(:plain => true).should == <<-EOS.strip
206
+ {
207
+ :a => {...}
208
+ }
209
+ EOS
210
+ end
211
+
212
+ it "plain single line" do
213
+ @hash.ai(:plain => true, :multiline => false).should == '{ :a => {...} }'
214
+ end
215
+ end
216
+
217
+ #------------------------------------------------------------------------------
218
+ describe "Negative options[:indent]" do
219
+ before(:each) do
220
+ @hash = { [0, 0, 255] => :yellow, :red => "rgb(255, 0, 0)", "magenta" => "rgb(255, 0, 255)" }
221
+ end
222
+
223
+ it "hash keys must be left aligned" do
224
+ out = @hash.ai(:plain => true, :indent => -4)
225
+ out.start_with?("{\n").should == true
226
+ out.include?(' :red => "rgb(255, 0, 0)"').should == true
227
+ out.include?(' "magenta" => "rgb(255, 0, 255)"').should == true
228
+ out.include?(' [ 0, 0, 255 ] => :yellow').should == true
229
+ out.end_with?("\n}").should == true
230
+ end
231
+ end
232
+
233
+ #------------------------------------------------------------------------------
234
+ describe "Class" do
235
+ it "shoud show superclass (plain)" do
236
+ self.class.ai(:plain => true).should == "#{self.class} < #{self.class.superclass}"
237
+ end
238
+
239
+ it "shoud show superclass (color)" do
240
+ self.class.ai.should == "#{self.class} < #{self.class.superclass}".yellow
241
+ end
242
+ end
243
+
244
+ #------------------------------------------------------------------------------
245
+ describe "File" do
246
+ it "should display a file (plain)" do
247
+ File.open(__FILE__, "r") do |f|
248
+ f.ai(:plain => true).should == "#{f.inspect}\n" << `ls -alF #{f.path}`.chop
249
+ end
250
+ end
251
+ end
252
+
253
+ #------------------------------------------------------------------------------
254
+ describe "Dir" do
255
+ it "should display a direcory (plain)" do
256
+ Dir.open(File.dirname(__FILE__)) do |d|
257
+ d.ai(:plain => true).should == "#{d.inspect}\n" << `ls -alF #{d.path}`.chop
258
+ end
259
+ end
260
+ end
261
+
262
+ end
@@ -0,0 +1,83 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ if defined?(::Rails)
4
+
5
+ # Create tableless ActiveRecord model.
6
+ #------------------------------------------------------------------------------
7
+ class User < ActiveRecord::Base
8
+ def self.columns()
9
+ @columns ||= []
10
+ end
11
+
12
+ def self.column(name, sql_type = nil, default = nil, null = true)
13
+ columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
14
+ end
15
+
16
+ column :id, :integer
17
+ column :name, :string
18
+ column :rank, :integer
19
+ column :admin, :boolean
20
+ column :created_at, :datetime
21
+ end
22
+
23
+ #------------------------------------------------------------------------------
24
+ describe "ActiveRecord instance" do
25
+ before(:each) do
26
+ @diana = User.new(:name => "Diana", :rank => 1, :admin => false, :created_at => "1992-10-10 12:30:00")
27
+ @laura = User.new(:name => "Laura", :rank => 2, :admin => true, :created_at => "2003-05-26 14:15:00")
28
+ @ap = AwesomePrint.new(:plain => true)
29
+ end
30
+
31
+ it "display single record" do
32
+ out = @ap.send(:awesome, @diana)
33
+ out.gsub(/0x([a-f\d]+)/, "0x01234567").should == <<-EOS.strip
34
+ #<User:0x01234567> {
35
+ :id => nil,
36
+ :name => "Diana",
37
+ :rank => 1,
38
+ :admin => false,
39
+ :created_at => Sat, 10 Oct 1992 12:30:00 UTC +00:00
40
+ }
41
+ EOS
42
+ end
43
+
44
+ it "display multiple records" do
45
+ out = @ap.send(:awesome, [ @diana, @laura ])
46
+ out.gsub(/0x([a-f\d]+)/, "0x01234567").should == <<-EOS.strip
47
+ [
48
+ [0] #<User:0x01234567> {
49
+ :id => nil,
50
+ :name => "Diana",
51
+ :rank => 1,
52
+ :admin => false,
53
+ :created_at => Sat, 10 Oct 1992 12:30:00 UTC +00:00
54
+ },
55
+ [1] #<User:0x01234567> {
56
+ :id => nil,
57
+ :name => "Laura",
58
+ :rank => 2,
59
+ :admin => true,
60
+ :created_at => Mon, 26 May 2003 14:15:00 UTC +00:00
61
+ }
62
+ ]
63
+ EOS
64
+ end
65
+ end
66
+
67
+ #------------------------------------------------------------------------------
68
+ describe "ActiveRecord class" do
69
+ it "should" do
70
+ @ap = AwesomePrint.new(:plain => true)
71
+ @ap.send(:awesome, User).should == <<-EOS.strip
72
+ class User < ActiveRecord::Base {
73
+ :id => :integer,
74
+ :name => :string,
75
+ :rank => :integer,
76
+ :admin => :boolean,
77
+ :created_at => :datetime
78
+ }
79
+ EOS
80
+ end
81
+ end
82
+
83
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'ap'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "String extensions" do
4
+ [ :gray, :red, :green, :yellow, :blue, :purple, :cyan, :white ].each_with_index do |color, i|
5
+ it "should have #{color} color" do
6
+ color.to_s.send(color).should == "\033[1;#{30+i}m#{color}\033[0m"
7
+ end
8
+
9
+ it "should have #{color}ish color" do
10
+ color.to_s.send(:"#{color}ish").should == "\033[0;#{30+i}m#{color}\033[0m"
11
+ end
12
+ end
13
+
14
+ it "should have black and pale colors" do
15
+ "black".send(:black).should == "black".send(:grayish)
16
+ "pale".send(:pale).should == "pale".send(:whiteish)
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jonbell-awesome_print
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 2
9
+ version: 0.1.2
10
+ platform: ruby
11
+ authors:
12
+ - Michael Dvorkin
13
+ - Jonathan Bell
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-04-07 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 2
31
+ - 9
32
+ version: 1.2.9
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: "Great Ruby dubugging companion: pretty print Ruby objects to visualize their structure. Supports MongoMapper objects via included mixin."
36
+ email: jonbell@spamcop.net
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.md
44
+ files:
45
+ - LICENSE
46
+ - README.md
47
+ - Rakefile
48
+ - VERSION
49
+ - init.rb
50
+ - lib/ap.rb
51
+ - lib/ap/awesome_print.rb
52
+ - lib/ap/core_ext/kernel.rb
53
+ - lib/ap/core_ext/string.rb
54
+ - lib/ap/mixin/rails.rb
55
+ - rails/init.rb
56
+ - spec/awesome_print_spec.rb
57
+ - spec/rails_spec.rb
58
+ - spec/spec.opts
59
+ - spec/spec_helper.rb
60
+ - spec/string_spec.rb
61
+ has_rdoc: true
62
+ homepage: http://github.com/jonbell/awesome_print
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options:
67
+ - --charset=UTF-8
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project: awesome_print
87
+ rubygems_version: 1.3.6
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Pretty print Ruby objects with proper indentation and colors.
91
+ test_files:
92
+ - spec/awesome_print_spec.rb
93
+ - spec/rails_spec.rb
94
+ - spec/spec_helper.rb
95
+ - spec/string_spec.rb