awesome_print 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +53 -36
- data/VERSION +1 -1
- data/lib/ap/awesome_print.rb +5 -5
- data/lib/ap/core_ext/kernel.rb +8 -2
- data/lib/ap/core_ext/string.rb +2 -2
- data/spec/awesome_print_spec.rb +32 -48
- metadata +3 -3
data/README.md
CHANGED
@@ -14,38 +14,43 @@ objects are supported via included mixin.
|
|
14
14
|
$ git clone git://github.com/michaeldv/awesome_print_.git
|
15
15
|
|
16
16
|
### Usage ###
|
17
|
+
|
18
|
+
require "ap"
|
19
|
+
ap object, options = {}
|
20
|
+
|
21
|
+
Default options:
|
22
|
+
|
23
|
+
:miltiline => 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
|
17
49
|
require "ap"
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
:plain => false,
|
23
|
-
:indent => 4,
|
24
|
-
:colors => {
|
25
|
-
:array => :white,
|
26
|
-
:bignum => :blue,
|
27
|
-
:class => :yellow,
|
28
|
-
:date => :greenish,
|
29
|
-
:falseclass => :red,
|
30
|
-
:fixnum => :blue,
|
31
|
-
:float => :blue,
|
32
|
-
:hash => :gray,
|
33
|
-
:nilclass => :red,
|
34
|
-
:string => :yellowish,
|
35
|
-
:symbol => :cyanish,
|
36
|
-
:time => :greenish,
|
37
|
-
:trueclass => :green
|
38
|
-
}
|
39
|
-
|
40
|
-
Supported color names:
|
41
|
-
:gray, :red, :green, :yellow, :blue, :purple, :cyan, :white
|
42
|
-
:black, :redish, :greenish, :yellowish, :blueish, :purpleish, :cyanish, :pale
|
43
|
-
|
44
|
-
### Example (IRB) ###
|
45
|
-
$ irb
|
46
|
-
irb> require "ap"
|
47
|
-
irb> data = [ false, 42, %w(fourty two), { :now => Time.now, :class => Time.now.class, :distance => 42e42 } ]
|
48
|
-
irb> ap data
|
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
|
49
54
|
[
|
50
55
|
[0] false,
|
51
56
|
[1] 42,
|
@@ -59,17 +64,29 @@ objects are supported via included mixin.
|
|
59
64
|
:distance => 4.2e+43
|
60
65
|
}
|
61
66
|
]
|
62
|
-
|
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
|
63
74
|
{
|
64
75
|
:class => Time < Object,
|
65
76
|
:now => Fri Apr 02 19:55:53 -0700 2010,
|
66
77
|
:distance => 4.2e+43
|
67
78
|
}
|
68
|
-
irb> ap data, :multiline => false
|
69
|
-
[ false, 42, [ "fourty", "two" ], { :class => Time < Object, :distance => 4.2e+43, :now => Fri Apr 02 19:44:52 -0700 2010 } ]
|
70
|
-
irb>
|
71
79
|
|
72
|
-
|
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) ###
|
73
90
|
$ ruby script/console
|
74
91
|
Loading development environment (Rails 2.3.5)
|
75
92
|
rails> require "ap"
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/lib/ap/awesome_print.rb
CHANGED
@@ -33,16 +33,14 @@ class AwesomePrint
|
|
33
33
|
Thread.current[AP] ||= []
|
34
34
|
end
|
35
35
|
|
36
|
-
def puts(object)
|
37
|
-
Kernel.puts awesome(object)
|
38
|
-
end
|
39
|
-
|
40
36
|
|
41
37
|
private
|
42
38
|
|
43
39
|
# Format an array.
|
44
40
|
#------------------------------------------------------------------------------
|
45
41
|
def awesome_array(a)
|
42
|
+
return "[]" if a == []
|
43
|
+
|
46
44
|
if @options[:multiline]
|
47
45
|
width = (a.size - 1).to_s.size
|
48
46
|
data = a.inject([]) do |arr, item|
|
@@ -61,13 +59,15 @@ class AwesomePrint
|
|
61
59
|
# Format a hash. If @options[:indent] if negative left align hash keys.
|
62
60
|
#------------------------------------------------------------------------------
|
63
61
|
def awesome_hash(h)
|
62
|
+
return "{}" if h == {}
|
63
|
+
|
64
64
|
data = h.keys.inject([]) do |arr, key|
|
65
65
|
plain_single_line do
|
66
66
|
arr << [ awesome(key), h[key] ]
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
|
-
width = data.map { |key, | key.size }.max
|
70
|
+
width = data.map { |key, | key.size }.max || 0
|
71
71
|
width += @indentation if @options[:indent] > 0
|
72
72
|
|
73
73
|
data = data.inject([]) do |arr, (key, value)|
|
data/lib/ap/core_ext/kernel.rb
CHANGED
@@ -5,10 +5,16 @@
|
|
5
5
|
#------------------------------------------------------------------------------
|
6
6
|
module Kernel
|
7
7
|
|
8
|
-
def
|
8
|
+
def ai(options = {})
|
9
9
|
ap = AwesomePrint.new(options)
|
10
|
-
ap.
|
10
|
+
ap.send(:awesome, self)
|
11
|
+
end
|
12
|
+
alias :awesome_inspect :ai
|
13
|
+
|
14
|
+
def ap(object, options = {})
|
15
|
+
puts object.ai(options)
|
11
16
|
end
|
17
|
+
alias :awesome_print :ap
|
12
18
|
|
13
19
|
module_function :ap
|
14
20
|
end
|
data/lib/ap/core_ext/string.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
# Awesome Print is freely distributable under the terms of MIT license.
|
4
4
|
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
|
5
5
|
#------------------------------------------------------------------------------
|
6
|
-
class String
|
6
|
+
class String
|
7
7
|
|
8
8
|
[ :gray, :red, :green, :yellow, :blue, :purple, :cyan, :white ].each_with_index do |color, i|
|
9
9
|
if STDOUT.tty? && ENV['TERM'] && ENV['TERM'] != 'dumb'
|
@@ -11,7 +11,7 @@ class String # :nodoc:
|
|
11
11
|
define_method :"#{color}ish" do "\033[0;#{30+i}m#{self}\033[0m" end
|
12
12
|
else
|
13
13
|
define_method color do self end
|
14
|
-
|
14
|
+
alias_method :"#{color}ish", color
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
data/spec/awesome_print_spec.rb
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe "AwesomePrint" do
|
4
|
-
before(:each) do
|
5
|
-
@color_ap = AwesomePrint.new
|
6
|
-
@plain_ap = AwesomePrint.new(:plain => true)
|
7
|
-
end
|
8
4
|
|
9
|
-
#------------------------------------------------------------------------------
|
10
5
|
describe "Array" do
|
11
6
|
before(:each) do
|
12
7
|
@arr = [ 1, :two, "three", [ nil, [ true, false] ] ]
|
13
8
|
end
|
14
9
|
|
10
|
+
it "empty array" do
|
11
|
+
[].ai.should == "[]"
|
12
|
+
end
|
13
|
+
|
15
14
|
it "plain multiline" do
|
16
|
-
|
17
|
-
ap.send(:awesome, @arr).should == <<-EOS.strip
|
15
|
+
@arr.ai(:plain => true).should == <<-EOS.strip
|
18
16
|
[
|
19
17
|
[0] 1,
|
20
18
|
[1] :two,
|
@@ -31,8 +29,7 @@ EOS
|
|
31
29
|
end
|
32
30
|
|
33
31
|
it "plain multiline indented" do
|
34
|
-
|
35
|
-
ap.send(:awesome, @arr).should == <<-EOS.strip
|
32
|
+
@arr.ai(:plain => true, :indent => 2).should == <<-EOS.strip
|
36
33
|
[
|
37
34
|
[0] 1,
|
38
35
|
[1] :two,
|
@@ -49,13 +46,11 @@ EOS
|
|
49
46
|
end
|
50
47
|
|
51
48
|
it "plain single line" do
|
52
|
-
|
53
|
-
ap.send(:awesome, @arr).should == '[ 1, :two, "three", [ nil, [ true, false ] ] ]'
|
49
|
+
@arr.ai(:plain => true, :multiline => false).should == '[ 1, :two, "three", [ nil, [ true, false ] ] ]'
|
54
50
|
end
|
55
51
|
|
56
|
-
it "colored multiline" do
|
57
|
-
|
58
|
-
ap.send(:awesome, @arr).should == <<-EOS.strip
|
52
|
+
it "colored multiline (default)" do
|
53
|
+
@arr.ai.should == <<-EOS.strip
|
59
54
|
[
|
60
55
|
\e[1;37m [0] \e[0m\e[1;34m1\e[0m,
|
61
56
|
\e[1;37m [1] \e[0m\e[0;36m:two\e[0m,
|
@@ -72,8 +67,7 @@ EOS
|
|
72
67
|
end
|
73
68
|
|
74
69
|
it "colored multiline indented" do
|
75
|
-
|
76
|
-
ap.send(:awesome, @arr).should == <<-EOS.strip
|
70
|
+
@arr.ai(:indent => 8).should == <<-EOS.strip
|
77
71
|
[
|
78
72
|
\e[1;37m [0] \e[0m\e[1;34m1\e[0m,
|
79
73
|
\e[1;37m [1] \e[0m\e[0;36m:two\e[0m,
|
@@ -90,8 +84,7 @@ EOS
|
|
90
84
|
end
|
91
85
|
|
92
86
|
it "colored single line" do
|
93
|
-
|
94
|
-
ap.send(:awesome, @arr).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 ] ] ]"
|
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 ] ] ]"
|
95
88
|
end
|
96
89
|
end
|
97
90
|
|
@@ -103,8 +96,7 @@ EOS
|
|
103
96
|
end
|
104
97
|
|
105
98
|
it "plain multiline" do
|
106
|
-
|
107
|
-
ap.send(:awesome, @arr).should == <<-EOS.strip
|
99
|
+
@arr.ai(:plain => true).should == <<-EOS.strip
|
108
100
|
[
|
109
101
|
[0] 1,
|
110
102
|
[1] 2,
|
@@ -114,8 +106,7 @@ EOS
|
|
114
106
|
end
|
115
107
|
|
116
108
|
it "plain single line" do
|
117
|
-
|
118
|
-
ap.send(:awesome, @arr).should == "[ 1, 2, [...] ]"
|
109
|
+
@arr.ai(:plain => true, :multiline => false).should == "[ 1, 2, [...] ]"
|
119
110
|
end
|
120
111
|
end
|
121
112
|
|
@@ -124,10 +115,13 @@ EOS
|
|
124
115
|
before(:each) do
|
125
116
|
@hash = { 1 => { :sym => { "str" => { [1, 2, 3] => { { :k => :v } => Hash } } } } }
|
126
117
|
end
|
127
|
-
|
118
|
+
|
119
|
+
it "empty hash" do
|
120
|
+
{}.ai.should == "{}"
|
121
|
+
end
|
122
|
+
|
128
123
|
it "plain multiline" do
|
129
|
-
|
130
|
-
ap.send(:awesome, @hash).should == <<-EOS.strip
|
124
|
+
@hash.ai(:plain => true).should == <<-EOS.strip
|
131
125
|
{
|
132
126
|
1 => {
|
133
127
|
:sym => {
|
@@ -143,8 +137,7 @@ EOS
|
|
143
137
|
end
|
144
138
|
|
145
139
|
it "plain multiline indented" do
|
146
|
-
|
147
|
-
ap.send(:awesome, @hash).should == <<-EOS.strip
|
140
|
+
@hash.ai(:plain => true, :indent => 1).should == <<-EOS.strip
|
148
141
|
{
|
149
142
|
1 => {
|
150
143
|
:sym => {
|
@@ -160,13 +153,11 @@ EOS
|
|
160
153
|
end
|
161
154
|
|
162
155
|
it "plain single line" do
|
163
|
-
|
164
|
-
ap.send(:awesome, @hash).should == '{ 1 => { :sym => { "str" => { [ 1, 2, 3 ] => { { :k => :v } => Hash < Object } } } } }'
|
156
|
+
@hash.ai(:plain => true, :multiline => false).should == '{ 1 => { :sym => { "str" => { [ 1, 2, 3 ] => { { :k => :v } => Hash < Object } } } } }'
|
165
157
|
end
|
166
158
|
|
167
|
-
it "colored multiline" do
|
168
|
-
|
169
|
-
ap.send(:awesome, @hash).should == <<-EOS.strip
|
159
|
+
it "colored multiline (default)" do
|
160
|
+
@hash.ai.should == <<-EOS.strip
|
170
161
|
{
|
171
162
|
1\e[1;30m => \e[0m{
|
172
163
|
:sym\e[1;30m => \e[0m{
|
@@ -182,8 +173,7 @@ EOS
|
|
182
173
|
end
|
183
174
|
|
184
175
|
it "colored multiline indented" do
|
185
|
-
|
186
|
-
ap.send(:awesome, @hash).should == <<-EOS.strip
|
176
|
+
@hash.ai(:indent => 2).should == <<-EOS.strip
|
187
177
|
{
|
188
178
|
1\e[1;30m => \e[0m{
|
189
179
|
:sym\e[1;30m => \e[0m{
|
@@ -199,8 +189,7 @@ EOS
|
|
199
189
|
end
|
200
190
|
|
201
191
|
it "colored single line" do
|
202
|
-
|
203
|
-
ap.send(:awesome, @hash).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 } } } } }"
|
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 } } } } }"
|
204
193
|
end
|
205
194
|
|
206
195
|
end
|
@@ -213,8 +202,7 @@ EOS
|
|
213
202
|
end
|
214
203
|
|
215
204
|
it "plain multiline" do
|
216
|
-
|
217
|
-
ap.send(:awesome, @hash).should == <<-EOS.strip
|
205
|
+
@hash.ai(:plain => true).should == <<-EOS.strip
|
218
206
|
{
|
219
207
|
:a => {...}
|
220
208
|
}
|
@@ -222,8 +210,7 @@ EOS
|
|
222
210
|
end
|
223
211
|
|
224
212
|
it "plain single line" do
|
225
|
-
|
226
|
-
ap.send(:awesome, @hash).should == '{ :a => {...} }'
|
213
|
+
@hash.ai(:plain => true, :multiline => false).should == '{ :a => {...} }'
|
227
214
|
end
|
228
215
|
end
|
229
216
|
|
@@ -234,8 +221,7 @@ EOS
|
|
234
221
|
end
|
235
222
|
|
236
223
|
it "hash keys must be left aligned" do
|
237
|
-
|
238
|
-
out = ap.send(:awesome, @hash)
|
224
|
+
out = @hash.ai(:plain => true, :indent => -4)
|
239
225
|
out.start_with?("{\n").should == true
|
240
226
|
out.include?(' :red => "rgb(255, 0, 0)"').should == true
|
241
227
|
out.include?(' "magenta" => "rgb(255, 0, 255)"').should == true
|
@@ -247,13 +233,11 @@ EOS
|
|
247
233
|
#------------------------------------------------------------------------------
|
248
234
|
describe "Class" do
|
249
235
|
it "shoud show superclass (plain)" do
|
250
|
-
|
251
|
-
ap.send(:awesome, self.class).should == "#{self.class} < #{self.class.superclass}"
|
236
|
+
self.class.ai(:plain => true).should == "#{self.class} < #{self.class.superclass}"
|
252
237
|
end
|
253
238
|
|
254
239
|
it "shoud show superclass (color)" do
|
255
|
-
|
256
|
-
ap.send(:awesome_class, self.class).should == "#{self.class} < #{self.class.superclass}".yellow
|
240
|
+
self.class.ai.should == "#{self.class} < #{self.class.superclass}".yellow
|
257
241
|
end
|
258
242
|
end
|
259
243
|
|
@@ -261,7 +245,7 @@ EOS
|
|
261
245
|
describe "File" do
|
262
246
|
it "should display a file (plain)" do
|
263
247
|
File.open(__FILE__, "r") do |f|
|
264
|
-
|
248
|
+
f.ai(:plain => true).should == "#{f.inspect}\n" << `ls -alF #{f.path}`.chop
|
265
249
|
end
|
266
250
|
end
|
267
251
|
end
|
@@ -270,7 +254,7 @@ EOS
|
|
270
254
|
describe "Dir" do
|
271
255
|
it "should display a direcory (plain)" do
|
272
256
|
Dir.open(File.dirname(__FILE__)) do |d|
|
273
|
-
|
257
|
+
d.ai(:plain => true).should == "#{d.inspect}\n" << `ls -alF #{d.path}`.chop
|
274
258
|
end
|
275
259
|
end
|
276
260
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 2
|
9
|
+
version: 0.1.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Michael Dvorkin
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-04-
|
17
|
+
date: 2010-04-05 00:00:00 -07:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|