colorful_inspect 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +121 -0
  2. data/Rakefile +18 -0
  3. data/lib/colorful_inspect.rb +318 -0
  4. metadata +77 -0
data/README.md ADDED
@@ -0,0 +1,121 @@
1
+ # What is it?
2
+
3
+ PP is a library from stdlib that allows to print objects with a nice formatting,
4
+ using PrettyPrint from stdlib. It is quite easy to customize it.
5
+
6
+ AwesomePrint is a gem to print objects using fancy colors. The output is nice,
7
+ but it's not as extensible as PP/PrettyPrint and it monkey patches several
8
+ classes from core (e.g. Object#methods, String to add colors, …). It also won't
9
+ use the pretty_print methods from classes that implement it.
10
+
11
+ ColorfulInspect is only changing the pretty_print method from several classes to
12
+ add colored formatting.
13
+
14
+ # Installation
15
+
16
+ gem install colorful_inspect
17
+
18
+ # Example
19
+
20
+ Just require it and use pp as usual:
21
+
22
+ require 'colorful_inspect'
23
+
24
+ Person = Struct.new :first_name, :last_name
25
+
26
+ pp [
27
+ 1, 2, true, 3, false, 4, nil, Rational(3, 4), 10 ** 50, Complex(3, 2),
28
+ [], {},
29
+ {
30
+ :alpha => "foo",
31
+ :beta => "bar",
32
+ :delta => "baz",
33
+ [1, 3] => "some\nthing"
34
+ },
35
+ Date.new(2011, 10, 24),
36
+ Time.now,
37
+ String, Array, BasicObject, Enumerable,
38
+ Array.instance_method(:each), 3.method(:succ), "foo".method(:pp),
39
+ Person.new("John", "Smith"),
40
+ $stdout, $stdin,
41
+ LoadError.new("could not find a good message"),
42
+ PrettyPrint.new
43
+ ]
44
+
45
+ And here's the output (without the colors):
46
+
47
+ [
48
+ [ 0] 1,
49
+ [ 1] 2,
50
+ [ 2] true,
51
+ [ 3] 3,
52
+ [ 4] false,
53
+ [ 5] 4,
54
+ [ 6] nil,
55
+ [ 7] (3/4) ≈ 0.75,
56
+ [ 8] 100000000000000000000000000000000000000000000000000,
57
+ [ 9] 3+2i,
58
+ [10] [],
59
+ [11] {},
60
+ [12] {
61
+ :alpha => "foo",
62
+ :beta => "bar",
63
+ :delta => "baz",
64
+ [
65
+ [0] 1,
66
+ [1] 3
67
+ ] => "some\nthing"
68
+ },
69
+ [13] Mon Oct 24 00:00:00 2011,
70
+ [14] Fri Jun 10 01:43:18 2011,
71
+ [15] String < Object,
72
+ [16] Array < Object,
73
+ [17] BasicObject,
74
+ [18] Enumerable,
75
+ [19] Array#each() [unbound],
76
+ [20] Fixnum#succ(),
77
+ [21] Kernel#pp(*objs),
78
+ [22] (Person < Struct) {
79
+ :first_name => "John",
80
+ :last_name => "Smith"
81
+ },
82
+ [23] #<IO:<STDOUT>>,
83
+ [24] #<IO:<STDIN>>,
84
+ [25] LoadError < ScriptError: could not find a good message,
85
+ [26] #<PrettyPrint:0x1b1db58
86
+ @buffer=[],
87
+ @buffer_width=0,
88
+ @genspace=
89
+ #<Proc:0x00000001b1dab8@/usr/lib/ruby/1.9.1/prettyprint.rb:82 (lambda)>,
90
+ @group_queue=
91
+ #<PrettyPrint::GroupQueue:0x1b1d9f0
92
+ @queue=
93
+ [
94
+ [0] [
95
+ [0] #<PrettyPrint::Group:0x1b1da68
96
+ @break=false,
97
+ @breakables=[],
98
+ @depth=0>
99
+ ]
100
+ ]>,
101
+ @group_stack=
102
+ [
103
+ [0] #<PrettyPrint::Group:0x1b1da68
104
+ @break=false,
105
+ @breakables=[],
106
+ @depth=0>
107
+ ],
108
+ @indent=0,
109
+ @maxwidth=79,
110
+ @newline="\n",
111
+ @output="",
112
+ @output_width=0>
113
+ ]
114
+
115
+
116
+ You can change the indentation as well as the used colors:
117
+
118
+ ColorfulInspect.indent = 4 # defaults to two
119
+
120
+ # pp ColorfulInspect.colors to see available colors :)
121
+ ColorfulInspect.colors[:nil] = :red
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'jeweler'
3
+
4
+ Jeweler::Tasks.new do |s|
5
+ s.name = "colorful_inspect"
6
+
7
+ s.summary = "A small library to print objects with colors"
8
+ s.description = s.summary
9
+ s.homepage = "http://github.com/Mon-Ouie/colorful_inspect"
10
+
11
+ s.email = "mon.ouie@gmail.com"
12
+ s.authors = ["Mon ouïe"]
13
+
14
+ s.files = ["Rakefile", "README.md", "lib/colorful_inspect.rb"]
15
+
16
+ s.add_dependency "term-ansicolor"
17
+ s.add_development_dependency "jeweler"
18
+ end
@@ -0,0 +1,318 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ require 'pp'
4
+ require 'date'
5
+ require 'time'
6
+ require 'term/ansicolor'
7
+
8
+ module ColorfulInspect
9
+ class << self
10
+ attr_accessor :indent
11
+ attr_accessor :colors
12
+ end
13
+
14
+ self.indent = 2
15
+
16
+ self.colors = {
17
+ :numeric => [:blue, :bold],
18
+ :bignum => [:blue],
19
+ :fixnum => [:blue, :bold],
20
+ :float => [:blue, :bold],
21
+ :rational => [:blue],
22
+ :true => [:green, :bold],
23
+ :false => [:red, :bold],
24
+ :nil => [:red],
25
+ :symbol => [:cyan, :bold],
26
+ :string => [:green],
27
+ :date => [:black],
28
+ :time => [:black, :bold],
29
+ :class => [:yellow, :bold],
30
+ :module => [:yellow],
31
+ :method => [:magenta],
32
+ :exception => [:red],
33
+ :ivar => [:cyan]
34
+ }
35
+
36
+ module_function
37
+ def group(q, open, close, &block)
38
+ q.group(ColorfulInspect.indent, open, "\n#{q.genspace[q.indent]}#{close}",
39
+ &block)
40
+ end
41
+
42
+ def break(q)
43
+ q.breakable ''
44
+ end
45
+
46
+ def colorize(q, type, string)
47
+ Array(ColorfulInspect.colors[type]).inject(string) do |str, msg|
48
+ Term::ANSIColor.send(msg, str)
49
+ end
50
+ end
51
+
52
+ def method_info(method)
53
+ args = ''
54
+
55
+ if method.respond_to?(:parameters) && (arg_ary = method.parameters)
56
+ arg_ary.map!.each_with_index do |(type, name), index|
57
+ name ||= "arg#{index + 1}"
58
+
59
+ case type
60
+ when :req then "#{name}"
61
+ when :opt then "#{name} = ?"
62
+ when :rest then "*#{name}"
63
+ when :block then "&#{name}"
64
+ else name
65
+ end
66
+ end
67
+
68
+ args = '(' + arg_ary.join(', ') + ')'
69
+ elsif method.arity == 0
70
+ args = "()"
71
+ elsif method.arity > 0
72
+ n = method.arity
73
+ args = '(' + (1..n).map { |i| "arg#{i}" }.join(", ") + ')'
74
+ elsif method.arity < 0
75
+ n = -method.arity
76
+ args = '(' + (1..n).map { |i| "arg#{i}" }.join(", ") + ')'
77
+ end
78
+
79
+ klass = if method.respond_to? :owner
80
+ method.owner.name
81
+ elsif method.inspect =~ /Method: (.*?)#/
82
+ $1
83
+ end
84
+
85
+ location = if method.respond_to? :source_location
86
+ file, line = method.source_location
87
+ "#{file}:#{line}" if file && line
88
+ end
89
+
90
+ [method.name.to_s, args, klass.to_s, location]
91
+ end
92
+ end
93
+
94
+ class Array
95
+ alias colorless_pretty_print pretty_print
96
+
97
+ def pretty_print(q)
98
+ return q.text("[]") if empty?
99
+
100
+ ColorfulInspect.group(q, "[", "]") do
101
+ index_width = (size - 1).to_s.size
102
+
103
+ each_with_index do |elem, n|
104
+ q.breakable ''
105
+ q.text "[#{n.to_s.rjust(index_width)}] "
106
+ q.pp elem
107
+ q.text "," if n != size - 1
108
+ end
109
+ end
110
+ end
111
+ end
112
+
113
+ class Hash
114
+ alias colorless_pretty_print pretty_print
115
+
116
+ def pretty_print(q)
117
+ return q.text("{}") if empty?
118
+
119
+ ColorfulInspect.group(q, "{", "}") do
120
+ each_with_index do |(key, val), n|
121
+ q.breakable ''
122
+ q.pp key
123
+ q.text " => "
124
+ q.pp val
125
+ q.text "," if n != size - 1
126
+ end
127
+ end
128
+ end
129
+ end
130
+
131
+ [Numeric, Bignum, Fixnum, Float].each do |klass|
132
+ type = klass.to_s.downcase.to_sym
133
+
134
+ klass.class_eval do
135
+ alias colorless_pretty_print pretty_print
136
+
137
+ define_method :pretty_print do |q|
138
+ q.text ColorfulInspect.colorize(q, type, to_s)
139
+ end
140
+ end
141
+ end
142
+
143
+ class Rational
144
+ alias colorless_pretty_print pretty_print
145
+
146
+ def pretty_print(q)
147
+ q.text ColorfulInspect.colorize(q, :rational, "#{inspect} ≈ #{to_f}")
148
+ end
149
+ end
150
+
151
+ [true, false, nil].each do |obj|
152
+ obj.class.class_eval do
153
+ alias colorless_pretty_print pretty_print
154
+
155
+ def pretty_print(q)
156
+ q.text ColorfulInspect.colorize(q, inspect.to_sym, inspect)
157
+ end
158
+ end
159
+ end
160
+
161
+ class Symbol
162
+ alias colorless_pretty_print pretty_print
163
+
164
+ def pretty_print(q)
165
+ q.text ColorfulInspect.colorize(q, :symbol, inspect)
166
+ end
167
+ end
168
+
169
+ class String
170
+ alias colorless_pretty_print pretty_print
171
+
172
+ def pretty_print(q)
173
+ q.text ColorfulInspect.colorize(q, :string, inspect)
174
+ end
175
+ end
176
+
177
+ [Date, Time].each do |klass|
178
+ type = klass.to_s.downcase.to_sym
179
+
180
+ klass.class_eval do
181
+ alias colorless_pretty_print pretty_print
182
+
183
+ define_method :pretty_print do |q|
184
+ q.text ColorfulInspect.colorize(q, type, ctime)
185
+ end
186
+ end
187
+ end
188
+
189
+ class Time
190
+ alias colorless_pretty_print pretty_print
191
+
192
+ def pretty_print(q)
193
+ q.text ColorfulInspect.colorize(q, :time, ctime)
194
+ end
195
+ end
196
+
197
+ class Class
198
+ alias colorless_pretty_print pretty_print
199
+
200
+ def pretty_print(q)
201
+ str = "#{inspect}#{" < #{superclass.inspect}" if superclass}"
202
+ q.text ColorfulInspect.colorize(q, :class, str)
203
+ end
204
+ end
205
+
206
+ class Module
207
+ alias colorless_pretty_print pretty_print
208
+
209
+ def pretty_print(q)
210
+ q.text ColorfulInspect.colorize(q, :module, inspect)
211
+ end
212
+ end
213
+
214
+ [Method, UnboundMethod].each do |klass|
215
+ klass.class_eval do
216
+ alias colorless_pretty_print pretty_print
217
+
218
+ public
219
+ def pretty_print(q)
220
+ name, args, owner = ColorfulInspect.method_info(self)
221
+
222
+ q.text ColorfulInspect.colorize(q, :class, owner)
223
+ q.text '#'
224
+ q.text ColorfulInspect.colorize(q, :method, name)
225
+ q.text ColorfulInspect.colorize(q, :args, args)
226
+
227
+ q.text " [unbound]" if kind_of? UnboundMethod
228
+ end
229
+ end
230
+ end
231
+
232
+ class Struct
233
+ alias colorless_pretty_print pretty_print
234
+
235
+ def pretty_print(q)
236
+ q.text '('
237
+ q.pp self.class
238
+ q.text ") "
239
+
240
+ ColorfulInspect.group(q, "{", "}") do
241
+ each_pair.with_index do |(key, val), n|
242
+ q.breakable ''
243
+ q.pp key
244
+ q.text " => "
245
+ q.pp val
246
+ q.text "," if n != size - 1
247
+ end
248
+ end
249
+ end
250
+ end
251
+
252
+ class Exception
253
+ alias colorless_pretty_print pretty_print
254
+
255
+ def pretty_print(q)
256
+ q.pp self.class
257
+ q.text ": "
258
+ q.text ColorfulInspect.colorize(q, :exception, message)
259
+ end
260
+ end
261
+
262
+ class Object
263
+ alias colorless_pretty_print pretty_print
264
+
265
+ def pretty_print(q)
266
+ # Check from pp.rb
267
+ if /\(Kernel\)#/ !~ Object.instance_method(:method).bind(self).call(:inspect).inspect
268
+ q.text self.inspect
269
+ elsif /\(Kernel\)#/ !~ Object.instance_method(:method).bind(self).call(:to_s).inspect &&
270
+ instance_variables.empty?
271
+ q.text self.to_s
272
+ else
273
+ id = "%x" % (__id__ * 2)
274
+ id.sub!(/\Af(?=[[:xdigit:]]{2}+\z)/, '') if id.sub!(/\A\.\./, '')
275
+
276
+ klass = ColorfulInspect.colorize(q, :class, self.class.to_s)
277
+
278
+ q.group(ColorfulInspect.indent, "\#<#{klass}:0x#{id}", '>') do
279
+ q.seplist(pretty_print_instance_variables, lambda { q.text ',' }) do |ivar|
280
+ q.breakable
281
+
282
+ q.text ColorfulInspect.colorize(q, :ivar, ivar.to_s)
283
+ q.text '='
284
+
285
+ q.group(ColorfulInspect.indent) do
286
+ q.breakable ''
287
+ q.pp instance_variable_get(ivar)
288
+ end
289
+ end
290
+ end
291
+ end
292
+ end
293
+ end
294
+
295
+ if $PROGRAM_NAME == __FILE__
296
+ # There is but one test to see if this works: see if the output is pretty.
297
+
298
+ Person = Struct.new :first_name, :last_name
299
+
300
+ pp [
301
+ 1, 2, true, 3, false, 4, nil, Rational(3, 4), 10 ** 50, Complex(3, 2),
302
+ [], {},
303
+ {
304
+ :alpha => "foo",
305
+ :beta => "bar",
306
+ :delta => "baz",
307
+ [1, 3] => "some\nthing"
308
+ },
309
+ Date.new(2011, 10, 24),
310
+ Time.now,
311
+ String, Array, BasicObject, Enumerable,
312
+ Array.instance_method(:each), 3.method(:succ), "foo".method(:pp),
313
+ Person.new("John", "Smith"),
314
+ $stdout, $stdin,
315
+ LoadError.new("could not find a good message"),
316
+ PrettyPrint.new
317
+ ]
318
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: colorful_inspect
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - "Mon ou\xC3\xAFe"
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-09 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: term-ansicolor
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: jeweler
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :development
36
+ version_requirements: *id002
37
+ description: A small library to print objects with colors
38
+ email: mon.ouie@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - README.md
45
+ files:
46
+ - README.md
47
+ - Rakefile
48
+ - lib/colorful_inspect.rb
49
+ homepage: http://github.com/Mon-Ouie/colorful_inspect
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.8.5
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: A small library to print objects with colors
76
+ test_files: []
77
+