awesome_print 0.4.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +22 -0
- data/CHANGELOG +8 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +26 -0
- data/README.md +52 -14
- data/Rakefile +2 -46
- data/lib/ap.rb +3 -17
- data/lib/awesome_print.rb +16 -12
- data/lib/{ap → awesome_print}/core_ext/array.rb +0 -0
- data/lib/{ap → awesome_print}/core_ext/class.rb +0 -0
- data/lib/{ap → awesome_print}/core_ext/kernel.rb +2 -2
- data/lib/awesome_print/core_ext/logger.rb +20 -0
- data/lib/{ap → awesome_print}/core_ext/method.rb +0 -0
- data/lib/{ap → awesome_print}/core_ext/object.rb +0 -0
- data/lib/{ap → awesome_print}/core_ext/string.rb +8 -5
- data/lib/awesome_print/ext/action_view.rb +18 -0
- data/lib/awesome_print/ext/active_record.rb +40 -0
- data/lib/awesome_print/ext/active_support.rb +40 -0
- data/lib/awesome_print/ext/mongo_mapper.rb +38 -0
- data/lib/awesome_print/ext/mongoid.rb +39 -0
- data/lib/awesome_print/ext/nokogiri.rb +45 -0
- data/lib/awesome_print/formatter.rb +350 -0
- data/lib/awesome_print/inspector.rb +140 -0
- data/{rails/init.rb → lib/awesome_print/version.rb} +5 -4
- data/spec/colors_spec.rb +106 -0
- data/spec/{awesome_print_spec.rb → formats_spec.rb} +187 -37
- data/spec/methods_spec.rb +20 -0
- data/spec/objects_spec.rb +79 -0
- data/spec/spec_helper.rb +1 -1
- metadata +49 -53
- data/VERSION +0 -1
- data/init.rb +0 -1
- data/lib/ap/awesome_print.rb +0 -352
- data/lib/ap/core_ext/logger.rb +0 -18
- data/lib/ap/mixin/action_view.rb +0 -17
- data/lib/ap/mixin/active_record.rb +0 -54
- data/lib/ap/mixin/active_support.rb +0 -46
- data/lib/ap/mixin/mongo_mapper.rb +0 -54
- data/spec/action_view_spec.rb +0 -25
- data/spec/active_record_spec.rb +0 -136
- data/spec/colorization_spec.rb +0 -84
- data/spec/logger_spec.rb +0 -43
- data/spec/mongo_mapper_spec.rb +0 -63
- data/spec/string_spec.rb +0 -20
data/spec/methods_spec.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
3
|
describe "Single method" do
|
4
|
+
before do
|
5
|
+
stub_dotfile!
|
6
|
+
end
|
7
|
+
|
4
8
|
after do
|
5
9
|
Object.instance_eval{ remove_const :Hello } if defined?(Hello)
|
6
10
|
end
|
@@ -77,6 +81,10 @@ describe "Single method" do
|
|
77
81
|
end
|
78
82
|
|
79
83
|
describe "Object methods" do
|
84
|
+
before do
|
85
|
+
stub_dotfile!
|
86
|
+
end
|
87
|
+
|
80
88
|
after do
|
81
89
|
Object.instance_eval{ remove_const :Hello } if defined?(Hello)
|
82
90
|
end
|
@@ -194,6 +202,10 @@ describe "Object methods" do
|
|
194
202
|
end
|
195
203
|
|
196
204
|
describe "Class methods" do
|
205
|
+
before do
|
206
|
+
stub_dotfile!
|
207
|
+
end
|
208
|
+
|
197
209
|
after do
|
198
210
|
Object.instance_eval{ remove_const :Hello } if defined?(Hello)
|
199
211
|
end
|
@@ -301,6 +313,10 @@ end
|
|
301
313
|
|
302
314
|
if RUBY_VERSION >= '1.9.2'
|
303
315
|
describe "Ruby 1.9.2+ Method#parameters" do
|
316
|
+
before do
|
317
|
+
stub_dotfile!
|
318
|
+
end
|
319
|
+
|
304
320
|
after do
|
305
321
|
Object.instance_eval{ remove_const :Hello } if defined?(Hello)
|
306
322
|
end
|
@@ -354,6 +370,7 @@ describe "Methods arrays" do
|
|
354
370
|
end
|
355
371
|
|
356
372
|
it "obj1.methods - obj2.methods should be awesome printed" do
|
373
|
+
stub_dotfile!
|
357
374
|
class Hello
|
358
375
|
def self.m1; end
|
359
376
|
end
|
@@ -362,6 +379,7 @@ describe "Methods arrays" do
|
|
362
379
|
end
|
363
380
|
|
364
381
|
it "obj1.methods & obj2.methods should be awesome printed" do
|
382
|
+
stub_dotfile!
|
365
383
|
class Hello
|
366
384
|
def self.m1; end
|
367
385
|
def self.m2; end
|
@@ -374,6 +392,7 @@ describe "Methods arrays" do
|
|
374
392
|
end
|
375
393
|
|
376
394
|
it "obj1.methods.grep(pattern) should be awesome printed" do
|
395
|
+
stub_dotfile!
|
377
396
|
class Hello
|
378
397
|
def self.m1; end
|
379
398
|
def self.m2; end
|
@@ -396,6 +415,7 @@ describe "Methods arrays" do
|
|
396
415
|
end
|
397
416
|
|
398
417
|
it "obj1.methods.grep(pattern, &block) should be awesome printed" do
|
418
|
+
stub_dotfile!
|
399
419
|
class Hello
|
400
420
|
def self.m0; end
|
401
421
|
def self.none; end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Single method" do
|
4
|
+
before do
|
5
|
+
stub_dotfile!
|
6
|
+
end
|
7
|
+
|
8
|
+
after do
|
9
|
+
Object.instance_eval{ remove_const :Hello } if defined?(Hello)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "object" do
|
13
|
+
it "attributes" do
|
14
|
+
class Hello
|
15
|
+
attr_reader :abra
|
16
|
+
attr_writer :ca
|
17
|
+
attr_accessor :dabra
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
@abra, @ca, @dabra = 1, 2, 3
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
out = Hello.new.ai(:plain => true)
|
25
|
+
str = <<-EOS.strip
|
26
|
+
#<Hello:0x01234567
|
27
|
+
attr_accessor :dabra = 3,
|
28
|
+
attr_reader :abra = 1,
|
29
|
+
attr_writer :ca = 2
|
30
|
+
>
|
31
|
+
EOS
|
32
|
+
out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
|
33
|
+
end
|
34
|
+
|
35
|
+
it "instance variables" do
|
36
|
+
class Hello
|
37
|
+
def initialize
|
38
|
+
@abra, @ca, @dabra = 1, 2, 3
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
out = Hello.new.ai(:plain => true)
|
43
|
+
str = <<-EOS.strip
|
44
|
+
#<Hello:0x01234567
|
45
|
+
@abra = 1,
|
46
|
+
@ca = 2,
|
47
|
+
@dabra = 3
|
48
|
+
>
|
49
|
+
EOS
|
50
|
+
out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
|
51
|
+
end
|
52
|
+
|
53
|
+
it "attributes and instance variables" do
|
54
|
+
class Hello
|
55
|
+
attr_reader :abra
|
56
|
+
attr_writer :ca
|
57
|
+
attr_accessor :dabra
|
58
|
+
|
59
|
+
def initialize
|
60
|
+
@abra, @ca, @dabra = 1, 2, 3
|
61
|
+
@scooby, @dooby, @doo = 3, 2, 1
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
out = Hello.new.ai(:plain => true)
|
66
|
+
str = <<-EOS.strip
|
67
|
+
#<Hello:0x01234567
|
68
|
+
@doo = 1,
|
69
|
+
@dooby = 2,
|
70
|
+
@scooby = 3,
|
71
|
+
attr_accessor :dabra = 3,
|
72
|
+
attr_reader :abra = 1,
|
73
|
+
attr_writer :ca = 2
|
74
|
+
>
|
75
|
+
EOS
|
76
|
+
out.gsub(/0x([a-f\d]+)/, "0x01234567").should == str
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -19,7 +19,7 @@ end
|
|
19
19
|
|
20
20
|
# The following is needed for the Infinity Test. It runs tests as subprocesses,
|
21
21
|
# which sets STDOUT.tty? to false and would otherwise prematurely disallow colors.
|
22
|
-
AwesomePrint.force_colors!
|
22
|
+
### AwesomePrint.force_colors!
|
23
23
|
|
24
24
|
# Ruby 1.8.6 only: define missing String methods that are needed for the specs to pass.
|
25
25
|
if RUBY_VERSION < '1.8.7'
|
metadata
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: awesome_print
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 4
|
9
|
-
- 0
|
10
|
-
version: 0.4.0
|
4
|
+
prerelease:
|
5
|
+
version: 1.0.0
|
11
6
|
platform: ruby
|
12
7
|
authors:
|
13
8
|
- Michael Dvorkin
|
@@ -15,8 +10,7 @@ autorequire:
|
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
12
|
|
18
|
-
date: 2011-
|
19
|
-
default_executable:
|
13
|
+
date: 2011-11-08 00:00:00 Z
|
20
14
|
dependencies:
|
21
15
|
- !ruby/object:Gem::Dependency
|
22
16
|
name: rspec
|
@@ -26,55 +20,59 @@ dependencies:
|
|
26
20
|
requirements:
|
27
21
|
- - ">="
|
28
22
|
- !ruby/object:Gem::Version
|
29
|
-
|
30
|
-
segments:
|
31
|
-
- 2
|
32
|
-
- 5
|
33
|
-
- 0
|
34
|
-
version: 2.5.0
|
23
|
+
version: 2.6.0
|
35
24
|
type: :development
|
36
25
|
version_requirements: *id001
|
37
|
-
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: fakefs
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 0.2.1
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
description: "Great Ruby dubugging companion: pretty print Ruby objects to visualize their structure. Supports custom object formatting via plugins"
|
38
38
|
email: mike@dvorkin.net
|
39
39
|
executables: []
|
40
40
|
|
41
41
|
extensions: []
|
42
42
|
|
43
|
-
extra_rdoc_files:
|
44
|
-
|
45
|
-
- README.md
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
46
45
|
files:
|
47
46
|
- CHANGELOG
|
47
|
+
- Gemfile
|
48
|
+
- Gemfile.lock
|
48
49
|
- LICENSE
|
49
|
-
- README.md
|
50
50
|
- Rakefile
|
51
|
-
-
|
52
|
-
- init.rb
|
51
|
+
- README.md
|
53
52
|
- lib/ap.rb
|
54
|
-
- lib/
|
55
|
-
- lib/
|
56
|
-
- lib/
|
57
|
-
- lib/
|
58
|
-
- lib/
|
59
|
-
- lib/
|
60
|
-
- lib/
|
61
|
-
- lib/
|
62
|
-
- lib/
|
63
|
-
- lib/
|
64
|
-
- lib/
|
65
|
-
- lib/
|
53
|
+
- lib/awesome_print/core_ext/array.rb
|
54
|
+
- lib/awesome_print/core_ext/class.rb
|
55
|
+
- lib/awesome_print/core_ext/kernel.rb
|
56
|
+
- lib/awesome_print/core_ext/logger.rb
|
57
|
+
- lib/awesome_print/core_ext/method.rb
|
58
|
+
- lib/awesome_print/core_ext/object.rb
|
59
|
+
- lib/awesome_print/core_ext/string.rb
|
60
|
+
- lib/awesome_print/ext/action_view.rb
|
61
|
+
- lib/awesome_print/ext/active_record.rb
|
62
|
+
- lib/awesome_print/ext/active_support.rb
|
63
|
+
- lib/awesome_print/ext/mongo_mapper.rb
|
64
|
+
- lib/awesome_print/ext/mongoid.rb
|
65
|
+
- lib/awesome_print/ext/nokogiri.rb
|
66
|
+
- lib/awesome_print/formatter.rb
|
67
|
+
- lib/awesome_print/inspector.rb
|
68
|
+
- lib/awesome_print/version.rb
|
66
69
|
- lib/awesome_print.rb
|
67
|
-
-
|
68
|
-
- spec/
|
69
|
-
- spec/active_record_spec.rb
|
70
|
-
- spec/awesome_print_spec.rb
|
71
|
-
- spec/colorization_spec.rb
|
72
|
-
- spec/logger_spec.rb
|
70
|
+
- spec/colors_spec.rb
|
71
|
+
- spec/formats_spec.rb
|
73
72
|
- spec/methods_spec.rb
|
74
|
-
- spec/
|
73
|
+
- spec/objects_spec.rb
|
75
74
|
- spec/spec_helper.rb
|
76
|
-
-
|
77
|
-
has_rdoc: true
|
75
|
+
- .gitignore
|
78
76
|
homepage: http://github.com/michaeldv/awesome_print
|
79
77
|
licenses: []
|
80
78
|
|
@@ -88,25 +86,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
86
|
requirements:
|
89
87
|
- - ">="
|
90
88
|
- !ruby/object:Gem::Version
|
91
|
-
hash: 3
|
92
|
-
segments:
|
93
|
-
- 0
|
94
89
|
version: "0"
|
95
90
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
91
|
none: false
|
97
92
|
requirements:
|
98
93
|
- - ">="
|
99
94
|
- !ruby/object:Gem::Version
|
100
|
-
hash: 3
|
101
|
-
segments:
|
102
|
-
- 0
|
103
95
|
version: "0"
|
104
96
|
requirements: []
|
105
97
|
|
106
98
|
rubyforge_project: awesome_print
|
107
|
-
rubygems_version: 1.
|
99
|
+
rubygems_version: 1.8.5
|
108
100
|
signing_key:
|
109
101
|
specification_version: 3
|
110
|
-
summary: Pretty print Ruby objects with proper indentation and colors
|
111
|
-
test_files:
|
112
|
-
|
102
|
+
summary: Pretty print Ruby objects with proper indentation and colors
|
103
|
+
test_files:
|
104
|
+
- spec/colors_spec.rb
|
105
|
+
- spec/formats_spec.rb
|
106
|
+
- spec/methods_spec.rb
|
107
|
+
- spec/objects_spec.rb
|
108
|
+
- spec/spec_helper.rb
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.4.0
|
data/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "lib", "awesome_print")
|
data/lib/ap/awesome_print.rb
DELETED
@@ -1,352 +0,0 @@
|
|
1
|
-
# Copyright (c) 2010-2011 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 "cgi"
|
7
|
-
require "shellwords"
|
8
|
-
|
9
|
-
class AwesomePrint
|
10
|
-
AP = :__awesome_print__ unless defined?(AwesomePrint::AP)
|
11
|
-
CORE = [ :array, :hash, :class, :file, :dir, :bigdecimal, :rational, :struct, :method, :unboundmethod ] unless defined?(AwesomePrint::CORE)
|
12
|
-
@@force_colors = false
|
13
|
-
|
14
|
-
def initialize(options = {})
|
15
|
-
@options = {
|
16
|
-
:multiline => true, # Display in multiple lines.
|
17
|
-
:plain => false, # Use colors.
|
18
|
-
:indent => 4, # Indent using 4 spaces.
|
19
|
-
:index => true, # Display array indices.
|
20
|
-
:html => false, # Use ANSI color codes rather than HTML.
|
21
|
-
:sorted_hash_keys => false, # Do not sort hash keys.
|
22
|
-
:color => {
|
23
|
-
:array => :white,
|
24
|
-
:bigdecimal => :blue,
|
25
|
-
:class => :yellow,
|
26
|
-
:date => :greenish,
|
27
|
-
:falseclass => :red,
|
28
|
-
:fixnum => :blue,
|
29
|
-
:float => :blue,
|
30
|
-
:hash => :pale,
|
31
|
-
:struct => :pale,
|
32
|
-
:nilclass => :red,
|
33
|
-
:string => :yellowish,
|
34
|
-
:symbol => :cyanish,
|
35
|
-
:time => :greenish,
|
36
|
-
:trueclass => :green,
|
37
|
-
:method => :purpleish,
|
38
|
-
:args => :pale
|
39
|
-
}
|
40
|
-
}
|
41
|
-
|
42
|
-
# Merge custom defaults and let explicit options parameter override them.
|
43
|
-
merge_custom_defaults!
|
44
|
-
merge_options!(options)
|
45
|
-
|
46
|
-
@indentation = @options[:indent].abs
|
47
|
-
Thread.current[AP] ||= []
|
48
|
-
end
|
49
|
-
|
50
|
-
|
51
|
-
private
|
52
|
-
|
53
|
-
# Format an array.
|
54
|
-
#------------------------------------------------------------------------------
|
55
|
-
def awesome_array(a)
|
56
|
-
return "[]" if a == []
|
57
|
-
|
58
|
-
if a.instance_variable_defined?('@__awesome_methods__')
|
59
|
-
methods_array(a)
|
60
|
-
elsif @options[:multiline]
|
61
|
-
width = (a.size - 1).to_s.size
|
62
|
-
data = a.inject([]) do |arr, item|
|
63
|
-
index = if @options[:index]
|
64
|
-
colorize("#{indent}[#{arr.size.to_s.rjust(width)}] ", :array)
|
65
|
-
else
|
66
|
-
colorize(indent, :array)
|
67
|
-
end
|
68
|
-
indented do
|
69
|
-
arr << (index << awesome(item))
|
70
|
-
end
|
71
|
-
end
|
72
|
-
"[\n" << data.join(",\n") << "\n#{outdent}]"
|
73
|
-
else
|
74
|
-
"[ " << a.map{ |item| awesome(item) }.join(", ") << " ]"
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
# Format a hash. If @options[:indent] if negative left align hash keys.
|
79
|
-
#------------------------------------------------------------------------------
|
80
|
-
def awesome_hash(h)
|
81
|
-
return "{}" if h == {}
|
82
|
-
|
83
|
-
keys = @options[:sorted_hash_keys] ? h.keys.sort { |a, b| a.to_s <=> b.to_s } : h.keys
|
84
|
-
data = keys.map do |key|
|
85
|
-
plain_single_line do
|
86
|
-
[ awesome(key), h[key] ]
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
width = data.map { |key, | key.size }.max || 0
|
91
|
-
width += @indentation if @options[:indent] > 0
|
92
|
-
|
93
|
-
data = data.map do |key, value|
|
94
|
-
if @options[:multiline]
|
95
|
-
formatted_key = (@options[:indent] >= 0 ? key.rjust(width) : indent + key.ljust(width))
|
96
|
-
else
|
97
|
-
formatted_key = key
|
98
|
-
end
|
99
|
-
indented do
|
100
|
-
formatted_key << colorize(" => ", :hash) << awesome(value)
|
101
|
-
end
|
102
|
-
end
|
103
|
-
if @options[:multiline]
|
104
|
-
"{\n" << data.join(",\n") << "\n#{outdent}}"
|
105
|
-
else
|
106
|
-
"{ #{data.join(', ')} }"
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
# Format a Struct. If @options[:indent] is negative left align hash keys.
|
111
|
-
#------------------------------------------------------------------------------
|
112
|
-
def awesome_struct(s)
|
113
|
-
h = {}
|
114
|
-
s.each_pair { |k,v| h[k] = v }
|
115
|
-
awesome_hash(h)
|
116
|
-
end
|
117
|
-
|
118
|
-
# Format Class object.
|
119
|
-
#------------------------------------------------------------------------------
|
120
|
-
def awesome_class(c)
|
121
|
-
if superclass = c.superclass # <-- Assign and test if nil.
|
122
|
-
awesome_self(c, :with => " < #{superclass}")
|
123
|
-
else
|
124
|
-
awesome_self(c)
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
# Format File object.
|
129
|
-
#------------------------------------------------------------------------------
|
130
|
-
def awesome_file(f)
|
131
|
-
ls = File.directory?(f) ? `ls -adlF #{f.path.shellescape}` : `ls -alF #{f.path.shellescape}`
|
132
|
-
awesome_self(f, :with => ls.empty? ? nil : "\n#{ls.chop}")
|
133
|
-
end
|
134
|
-
|
135
|
-
# Format Dir object.
|
136
|
-
#------------------------------------------------------------------------------
|
137
|
-
def awesome_dir(d)
|
138
|
-
ls = `ls -alF #{d.path.shellescape}`
|
139
|
-
awesome_self(d, :with => ls.empty? ? nil : "\n#{ls.chop}")
|
140
|
-
end
|
141
|
-
|
142
|
-
# Format BigDecimal and Rational objects by convering them to Float.
|
143
|
-
#------------------------------------------------------------------------------
|
144
|
-
def awesome_bigdecimal(n)
|
145
|
-
awesome_self(n.to_f, :as => :bigdecimal)
|
146
|
-
end
|
147
|
-
alias :awesome_rational :awesome_bigdecimal
|
148
|
-
|
149
|
-
# Format a method.
|
150
|
-
#------------------------------------------------------------------------------
|
151
|
-
def awesome_method(m)
|
152
|
-
name, args, owner = method_tuple(m)
|
153
|
-
"#{colorize(owner, :class)}##{colorize(name, :method)}#{colorize(args, :args)}"
|
154
|
-
end
|
155
|
-
alias :awesome_unboundmethod :awesome_method
|
156
|
-
|
157
|
-
# Catch all method to format an arbitrary object.
|
158
|
-
#------------------------------------------------------------------------------
|
159
|
-
def awesome_self(object, appear = {})
|
160
|
-
colorize(object.inspect.to_s << appear[:with].to_s, appear[:as] || declassify(object))
|
161
|
-
end
|
162
|
-
|
163
|
-
# Dispatcher that detects data nesting and invokes object-aware formatter.
|
164
|
-
#------------------------------------------------------------------------------
|
165
|
-
def awesome(object)
|
166
|
-
if Thread.current[AP].include?(object.object_id)
|
167
|
-
nested(object)
|
168
|
-
else
|
169
|
-
begin
|
170
|
-
Thread.current[AP] << object.object_id
|
171
|
-
send(:"awesome_#{printable(object)}", object)
|
172
|
-
ensure
|
173
|
-
Thread.current[AP].pop
|
174
|
-
end
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
# Format object.methods array.
|
179
|
-
#------------------------------------------------------------------------------
|
180
|
-
def methods_array(a)
|
181
|
-
object = a.instance_variable_get('@__awesome_methods__')
|
182
|
-
tuples = a.map do |name|
|
183
|
-
tuple = if object.respond_to?(name, true) # Is this a regular method?
|
184
|
-
the_method = object.method(name) rescue nil # Avoid potential ArgumentError if object#method is overridden.
|
185
|
-
if the_method && the_method.respond_to?(:arity) # Is this original object#method?
|
186
|
-
method_tuple(the_method) # Yes, we are good.
|
187
|
-
end
|
188
|
-
elsif object.respond_to?(:instance_method) # Is this an unbound method?
|
189
|
-
method_tuple(object.instance_method(name))
|
190
|
-
end
|
191
|
-
tuple || [ name.to_s, '(?)', '' ] # Return WTF default if all the above fails.
|
192
|
-
end
|
193
|
-
|
194
|
-
width = (tuples.size - 1).to_s.size
|
195
|
-
name_width = tuples.map { |item| item[0].size }.max || 0
|
196
|
-
args_width = tuples.map { |item| item[1].size }.max || 0
|
197
|
-
|
198
|
-
data = tuples.inject([]) do |arr, item|
|
199
|
-
index = if @options[:index]
|
200
|
-
"#{indent}[#{arr.size.to_s.rjust(width)}]"
|
201
|
-
else
|
202
|
-
indent
|
203
|
-
end
|
204
|
-
indented do
|
205
|
-
arr << "#{index} #{colorize(item[0].rjust(name_width), :method)}#{colorize(item[1].ljust(args_width), :args)} #{colorize(item[2], :class)}"
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
"[\n" << data.join("\n") << "\n#{outdent}]"
|
210
|
-
end
|
211
|
-
|
212
|
-
# Format nested data, for example:
|
213
|
-
# arr = [1, 2]; arr << arr
|
214
|
-
# => [1,2, [...]]
|
215
|
-
# hsh = { :a => 1 }; hsh[:b] = hsh
|
216
|
-
# => { :a => 1, :b => {...} }
|
217
|
-
#------------------------------------------------------------------------------
|
218
|
-
def nested(object)
|
219
|
-
case printable(object)
|
220
|
-
when :array then colorize("[...]", :array)
|
221
|
-
when :hash then colorize("{...}", :hash)
|
222
|
-
when :struct then colorize("{...}", :struct)
|
223
|
-
else colorize("...#{object.class}...", :class)
|
224
|
-
end
|
225
|
-
end
|
226
|
-
|
227
|
-
# Return one of the "core" types that have a formatter of :self otherwise.
|
228
|
-
#------------------------------------------------------------------------------
|
229
|
-
def printable(object)
|
230
|
-
CORE.grep(declassify(object))[0] || :self
|
231
|
-
end
|
232
|
-
|
233
|
-
# Turn class name into symbol, ex: Hello::World => :hello_world. Classes that
|
234
|
-
# inherit from Array, Hash, File, Dir, and Struct are treated as the base class.
|
235
|
-
#------------------------------------------------------------------------------
|
236
|
-
def declassify(object)
|
237
|
-
case object
|
238
|
-
when Array then :array
|
239
|
-
when Hash then :hash
|
240
|
-
when File then :file
|
241
|
-
when Dir then :dir
|
242
|
-
when Struct then :struct
|
243
|
-
else object.class.to_s.gsub(/:+/, "_").downcase.to_sym
|
244
|
-
end
|
245
|
-
end
|
246
|
-
|
247
|
-
# Pick the color and apply it to the given string as necessary.
|
248
|
-
#------------------------------------------------------------------------------
|
249
|
-
def colorize(s, type)
|
250
|
-
s = CGI.escapeHTML(s) if @options[:html]
|
251
|
-
if @options[:plain] || !@options[:color][type] || !colorize?
|
252
|
-
@options[:html] ? "<pre>#{s}</pre>" : s
|
253
|
-
else
|
254
|
-
s.send(@options[:color][type], @options[:html])
|
255
|
-
end
|
256
|
-
end
|
257
|
-
|
258
|
-
# Return [ name, arguments, owner ] tuple for a given method.
|
259
|
-
#------------------------------------------------------------------------------
|
260
|
-
def method_tuple(method)
|
261
|
-
if method.respond_to?(:parameters) # Ruby 1.9.2+
|
262
|
-
# See http://ruby.runpaint.org/methods#method-objects-parameters
|
263
|
-
args = method.parameters.inject([]) do |arr, (type, name)|
|
264
|
-
name ||= (type == :block ? 'block' : "arg#{arr.size + 1}")
|
265
|
-
arr << case type
|
266
|
-
when :req then name.to_s
|
267
|
-
when :opt, :rest then "*#{name}"
|
268
|
-
when :block then "&#{name}"
|
269
|
-
else '?'
|
270
|
-
end
|
271
|
-
end
|
272
|
-
else # See http://ruby-doc.org/core/classes/Method.html#M001902
|
273
|
-
args = (1..method.arity.abs).map { |i| "arg#{i}" }
|
274
|
-
args[-1] = "*#{args[-1]}" if method.arity < 0
|
275
|
-
end
|
276
|
-
|
277
|
-
if method.to_s =~ /(Unbound)*Method: (.*?)[#\.]/
|
278
|
-
owner = "#{$2}#{$1 ? '(unbound)' : ''}".gsub('(', ' (')
|
279
|
-
end
|
280
|
-
|
281
|
-
[ method.name.to_s, "(#{args.join(', ')})", owner.to_s ]
|
282
|
-
end
|
283
|
-
|
284
|
-
# Format hash keys as plain string regardless of underlying data type.
|
285
|
-
#------------------------------------------------------------------------------
|
286
|
-
def plain_single_line
|
287
|
-
plain, multiline = @options[:plain], @options[:multiline]
|
288
|
-
@options[:plain], @options[:multiline] = true, false
|
289
|
-
yield
|
290
|
-
ensure
|
291
|
-
@options[:plain], @options[:multiline] = plain, multiline
|
292
|
-
end
|
293
|
-
|
294
|
-
#------------------------------------------------------------------------------
|
295
|
-
def indented
|
296
|
-
@indentation += @options[:indent].abs
|
297
|
-
yield
|
298
|
-
ensure
|
299
|
-
@indentation -= @options[:indent].abs
|
300
|
-
end
|
301
|
-
|
302
|
-
def indent
|
303
|
-
@indent = ' ' * @indentation
|
304
|
-
end
|
305
|
-
|
306
|
-
def outdent
|
307
|
-
@outdent = ' ' * (@indentation - @options[:indent].abs)
|
308
|
-
end
|
309
|
-
|
310
|
-
# Update @options by first merging the :color hash and then the remaining keys.
|
311
|
-
#------------------------------------------------------------------------------
|
312
|
-
def merge_options!(options = {})
|
313
|
-
@options[:color].merge!(options.delete(:color) || {})
|
314
|
-
@options.merge!(options)
|
315
|
-
end
|
316
|
-
|
317
|
-
# Load ~/.aprc file with custom defaults that override default options.
|
318
|
-
#------------------------------------------------------------------------------
|
319
|
-
def merge_custom_defaults!
|
320
|
-
dotfile = File.join(ENV["HOME"], ".aprc")
|
321
|
-
if File.readable?(dotfile)
|
322
|
-
load dotfile
|
323
|
-
merge_options!(self.class.defaults)
|
324
|
-
end
|
325
|
-
rescue => e
|
326
|
-
$stderr.puts "Could not load #{dotfile}: #{e}"
|
327
|
-
end
|
328
|
-
|
329
|
-
# Return true if we are to colorize the output.
|
330
|
-
#------------------------------------------------------------------------------
|
331
|
-
def colorize?
|
332
|
-
@@force_colors || (STDOUT.tty? && ((ENV['TERM'] && ENV['TERM'] != 'dumb') || ENV['ANSICON']))
|
333
|
-
end
|
334
|
-
|
335
|
-
# Class accessor to force colorized output (ex. forked subprocess where TERM
|
336
|
-
# might be dumb).
|
337
|
-
#------------------------------------------------------------------------------
|
338
|
-
def self.force_colors!(value = true)
|
339
|
-
@@force_colors = value
|
340
|
-
end
|
341
|
-
|
342
|
-
# Class accessors for custom defaults.
|
343
|
-
#------------------------------------------------------------------------------
|
344
|
-
def self.defaults
|
345
|
-
@@defaults ||= {}
|
346
|
-
end
|
347
|
-
|
348
|
-
def self.defaults=(args = {})
|
349
|
-
@@defaults = args
|
350
|
-
end
|
351
|
-
|
352
|
-
end
|