pp 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +39 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/pp.rb +593 -0
- data/pp.gemspec +22 -0
- metadata +56 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7781a81818bc667adae58438a03341d3a24aa6e5632739a0745ebd4c1e01c374
|
4
|
+
data.tar.gz: 2110bf8b8a49cbe79a8cef6882391cacb6ca98f235b342923fc1bf8f82c4b71c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f3edf21a3229d2909ff2a936baa0f1cca0728b150d788fc6db08a1bfc0c8ff2868169f0fda013f16ba074e01a6f4be5c6bca77a90e12f067fc24dd3a9a181f93
|
7
|
+
data.tar.gz: 7c85c18274cd2717c2fd211fb41b3bf9d8424fd382ff8bd19a3799570a4c986b29a840be6d7d0c1ce9b47fbd126fe3b0f98525e7434232be6de8e899b862b590
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved.
|
2
|
+
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
4
|
+
modification, are permitted provided that the following conditions
|
5
|
+
are met:
|
6
|
+
1. Redistributions of source code must retain the above copyright
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
|
+
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
13
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
14
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
15
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
16
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
17
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
18
|
+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
19
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
20
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
21
|
+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
22
|
+
SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# PP
|
2
|
+
|
3
|
+
A pretty-printer for Ruby objects.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'pp'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install pp
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
pp(obj) #=> obj
|
25
|
+
pp obj #=> obj
|
26
|
+
pp(obj1, obj2, ...) #=> [obj1, obj2, ...]
|
27
|
+
pp() #=> nil
|
28
|
+
```
|
29
|
+
|
30
|
+
## Development
|
31
|
+
|
32
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
33
|
+
|
34
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/pp.
|
39
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "pp"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/pp.rb
ADDED
@@ -0,0 +1,593 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'prettyprint'
|
4
|
+
|
5
|
+
##
|
6
|
+
# A pretty-printer for Ruby objects.
|
7
|
+
#
|
8
|
+
##
|
9
|
+
# == What PP Does
|
10
|
+
#
|
11
|
+
# Standard output by #p returns this:
|
12
|
+
# #<PP:0x81fedf0 @genspace=#<Proc:0x81feda0>, @group_queue=#<PrettyPrint::GroupQueue:0x81fed3c @queue=[[#<PrettyPrint::Group:0x81fed78 @breakables=[], @depth=0, @break=false>], []]>, @buffer=[], @newline="\n", @group_stack=[#<PrettyPrint::Group:0x81fed78 @breakables=[], @depth=0, @break=false>], @buffer_width=0, @indent=0, @maxwidth=79, @output_width=2, @output=#<IO:0x8114ee4>>
|
13
|
+
#
|
14
|
+
# Pretty-printed output returns this:
|
15
|
+
# #<PP:0x81fedf0
|
16
|
+
# @buffer=[],
|
17
|
+
# @buffer_width=0,
|
18
|
+
# @genspace=#<Proc:0x81feda0>,
|
19
|
+
# @group_queue=
|
20
|
+
# #<PrettyPrint::GroupQueue:0x81fed3c
|
21
|
+
# @queue=
|
22
|
+
# [[#<PrettyPrint::Group:0x81fed78 @break=false, @breakables=[], @depth=0>],
|
23
|
+
# []]>,
|
24
|
+
# @group_stack=
|
25
|
+
# [#<PrettyPrint::Group:0x81fed78 @break=false, @breakables=[], @depth=0>],
|
26
|
+
# @indent=0,
|
27
|
+
# @maxwidth=79,
|
28
|
+
# @newline="\n",
|
29
|
+
# @output=#<IO:0x8114ee4>,
|
30
|
+
# @output_width=2>
|
31
|
+
#
|
32
|
+
##
|
33
|
+
# == Usage
|
34
|
+
#
|
35
|
+
# pp(obj) #=> obj
|
36
|
+
# pp obj #=> obj
|
37
|
+
# pp(obj1, obj2, ...) #=> [obj1, obj2, ...]
|
38
|
+
# pp() #=> nil
|
39
|
+
#
|
40
|
+
# Output <tt>obj(s)</tt> to <tt>$></tt> in pretty printed format.
|
41
|
+
#
|
42
|
+
# It returns <tt>obj(s)</tt>.
|
43
|
+
#
|
44
|
+
##
|
45
|
+
# == Output Customization
|
46
|
+
#
|
47
|
+
# To define a customized pretty printing function for your classes,
|
48
|
+
# redefine method <code>#pretty_print(pp)</code> in the class.
|
49
|
+
#
|
50
|
+
# <code>#pretty_print</code> takes the +pp+ argument, which is an instance of the PP class.
|
51
|
+
# The method uses #text, #breakable, #nest, #group and #pp to print the
|
52
|
+
# object.
|
53
|
+
#
|
54
|
+
##
|
55
|
+
# == Pretty-Print JSON
|
56
|
+
#
|
57
|
+
# To pretty-print JSON refer to JSON#pretty_generate.
|
58
|
+
#
|
59
|
+
##
|
60
|
+
# == Author
|
61
|
+
# Tanaka Akira <akr@fsij.org>
|
62
|
+
|
63
|
+
class PP < PrettyPrint
|
64
|
+
# Outputs +obj+ to +out+ in pretty printed format of
|
65
|
+
# +width+ columns in width.
|
66
|
+
#
|
67
|
+
# If +out+ is omitted, <code>$></code> is assumed.
|
68
|
+
# If +width+ is omitted, 79 is assumed.
|
69
|
+
#
|
70
|
+
# PP.pp returns +out+.
|
71
|
+
def PP.pp(obj, out=$>, width=79)
|
72
|
+
q = PP.new(out, width)
|
73
|
+
q.guard_inspect_key {q.pp obj}
|
74
|
+
q.flush
|
75
|
+
#$pp = q
|
76
|
+
out << "\n"
|
77
|
+
end
|
78
|
+
|
79
|
+
# Outputs +obj+ to +out+ like PP.pp but with no indent and
|
80
|
+
# newline.
|
81
|
+
#
|
82
|
+
# PP.singleline_pp returns +out+.
|
83
|
+
def PP.singleline_pp(obj, out=$>)
|
84
|
+
q = SingleLine.new(out)
|
85
|
+
q.guard_inspect_key {q.pp obj}
|
86
|
+
q.flush
|
87
|
+
out
|
88
|
+
end
|
89
|
+
|
90
|
+
# :stopdoc:
|
91
|
+
def PP.mcall(obj, mod, meth, *args, &block)
|
92
|
+
mod.instance_method(meth).bind(obj).call(*args, &block)
|
93
|
+
end
|
94
|
+
# :startdoc:
|
95
|
+
|
96
|
+
@sharing_detection = false
|
97
|
+
class << self
|
98
|
+
# Returns the sharing detection flag as a boolean value.
|
99
|
+
# It is false by default.
|
100
|
+
attr_accessor :sharing_detection
|
101
|
+
end
|
102
|
+
|
103
|
+
module PPMethods
|
104
|
+
|
105
|
+
# Yields to a block
|
106
|
+
# and preserves the previous set of objects being printed.
|
107
|
+
def guard_inspect_key
|
108
|
+
if Thread.current[:__recursive_key__] == nil
|
109
|
+
Thread.current[:__recursive_key__] = {}.taint
|
110
|
+
end
|
111
|
+
|
112
|
+
if Thread.current[:__recursive_key__][:inspect] == nil
|
113
|
+
Thread.current[:__recursive_key__][:inspect] = {}.taint
|
114
|
+
end
|
115
|
+
|
116
|
+
save = Thread.current[:__recursive_key__][:inspect]
|
117
|
+
|
118
|
+
begin
|
119
|
+
Thread.current[:__recursive_key__][:inspect] = {}.taint
|
120
|
+
yield
|
121
|
+
ensure
|
122
|
+
Thread.current[:__recursive_key__][:inspect] = save
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# Check whether the object_id +id+ is in the current buffer of objects
|
127
|
+
# to be pretty printed. Used to break cycles in chains of objects to be
|
128
|
+
# pretty printed.
|
129
|
+
def check_inspect_key(id)
|
130
|
+
Thread.current[:__recursive_key__] &&
|
131
|
+
Thread.current[:__recursive_key__][:inspect] &&
|
132
|
+
Thread.current[:__recursive_key__][:inspect].include?(id)
|
133
|
+
end
|
134
|
+
|
135
|
+
# Adds the object_id +id+ to the set of objects being pretty printed, so
|
136
|
+
# as to not repeat objects.
|
137
|
+
def push_inspect_key(id)
|
138
|
+
Thread.current[:__recursive_key__][:inspect][id] = true
|
139
|
+
end
|
140
|
+
|
141
|
+
# Removes an object from the set of objects being pretty printed.
|
142
|
+
def pop_inspect_key(id)
|
143
|
+
Thread.current[:__recursive_key__][:inspect].delete id
|
144
|
+
end
|
145
|
+
|
146
|
+
# Adds +obj+ to the pretty printing buffer
|
147
|
+
# using Object#pretty_print or Object#pretty_print_cycle.
|
148
|
+
#
|
149
|
+
# Object#pretty_print_cycle is used when +obj+ is already
|
150
|
+
# printed, a.k.a the object reference chain has a cycle.
|
151
|
+
def pp(obj)
|
152
|
+
id = obj.object_id
|
153
|
+
|
154
|
+
if check_inspect_key(id)
|
155
|
+
group {obj.pretty_print_cycle self}
|
156
|
+
return
|
157
|
+
end
|
158
|
+
|
159
|
+
begin
|
160
|
+
push_inspect_key(id)
|
161
|
+
group {obj.pretty_print self}
|
162
|
+
ensure
|
163
|
+
pop_inspect_key(id) unless PP.sharing_detection
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
# A convenience method which is same as follows:
|
168
|
+
#
|
169
|
+
# group(1, '#<' + obj.class.name, '>') { ... }
|
170
|
+
def object_group(obj, &block) # :yield:
|
171
|
+
group(1, '#<' + obj.class.name, '>', &block)
|
172
|
+
end
|
173
|
+
|
174
|
+
# A convenience method, like object_group, but also reformats the Object's
|
175
|
+
# object_id.
|
176
|
+
def object_address_group(obj, &block)
|
177
|
+
str = Kernel.instance_method(:to_s).bind(obj).call
|
178
|
+
str.chomp!('>')
|
179
|
+
group(1, str, '>', &block)
|
180
|
+
end
|
181
|
+
|
182
|
+
# A convenience method which is same as follows:
|
183
|
+
#
|
184
|
+
# text ','
|
185
|
+
# breakable
|
186
|
+
def comma_breakable
|
187
|
+
text ','
|
188
|
+
breakable
|
189
|
+
end
|
190
|
+
|
191
|
+
# Adds a separated list.
|
192
|
+
# The list is separated by comma with breakable space, by default.
|
193
|
+
#
|
194
|
+
# #seplist iterates the +list+ using +iter_method+.
|
195
|
+
# It yields each object to the block given for #seplist.
|
196
|
+
# The procedure +separator_proc+ is called between each yields.
|
197
|
+
#
|
198
|
+
# If the iteration is zero times, +separator_proc+ is not called at all.
|
199
|
+
#
|
200
|
+
# If +separator_proc+ is nil or not given,
|
201
|
+
# +lambda { comma_breakable }+ is used.
|
202
|
+
# If +iter_method+ is not given, :each is used.
|
203
|
+
#
|
204
|
+
# For example, following 3 code fragments has similar effect.
|
205
|
+
#
|
206
|
+
# q.seplist([1,2,3]) {|v| xxx v }
|
207
|
+
#
|
208
|
+
# q.seplist([1,2,3], lambda { q.comma_breakable }, :each) {|v| xxx v }
|
209
|
+
#
|
210
|
+
# xxx 1
|
211
|
+
# q.comma_breakable
|
212
|
+
# xxx 2
|
213
|
+
# q.comma_breakable
|
214
|
+
# xxx 3
|
215
|
+
def seplist(list, sep=nil, iter_method=:each) # :yield: element
|
216
|
+
sep ||= lambda { comma_breakable }
|
217
|
+
first = true
|
218
|
+
list.__send__(iter_method) {|*v|
|
219
|
+
if first
|
220
|
+
first = false
|
221
|
+
else
|
222
|
+
sep.call
|
223
|
+
end
|
224
|
+
yield(*v)
|
225
|
+
}
|
226
|
+
end
|
227
|
+
|
228
|
+
# A present standard failsafe for pretty printing any given Object
|
229
|
+
def pp_object(obj)
|
230
|
+
object_address_group(obj) {
|
231
|
+
seplist(obj.pretty_print_instance_variables, lambda { text ',' }) {|v|
|
232
|
+
breakable
|
233
|
+
v = v.to_s if Symbol === v
|
234
|
+
text v
|
235
|
+
text '='
|
236
|
+
group(1) {
|
237
|
+
breakable ''
|
238
|
+
pp(obj.instance_eval(v))
|
239
|
+
}
|
240
|
+
}
|
241
|
+
}
|
242
|
+
end
|
243
|
+
|
244
|
+
# A pretty print for a Hash
|
245
|
+
def pp_hash(obj)
|
246
|
+
group(1, '{', '}') {
|
247
|
+
seplist(obj, nil, :each_pair) {|k, v|
|
248
|
+
group {
|
249
|
+
pp k
|
250
|
+
text '=>'
|
251
|
+
group(1) {
|
252
|
+
breakable ''
|
253
|
+
pp v
|
254
|
+
}
|
255
|
+
}
|
256
|
+
}
|
257
|
+
}
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
include PPMethods
|
262
|
+
|
263
|
+
class SingleLine < PrettyPrint::SingleLine # :nodoc:
|
264
|
+
include PPMethods
|
265
|
+
end
|
266
|
+
|
267
|
+
module ObjectMixin # :nodoc:
|
268
|
+
# 1. specific pretty_print
|
269
|
+
# 2. specific inspect
|
270
|
+
# 3. generic pretty_print
|
271
|
+
|
272
|
+
# A default pretty printing method for general objects.
|
273
|
+
# It calls #pretty_print_instance_variables to list instance variables.
|
274
|
+
#
|
275
|
+
# If +self+ has a customized (redefined) #inspect method,
|
276
|
+
# the result of self.inspect is used but it obviously has no
|
277
|
+
# line break hints.
|
278
|
+
#
|
279
|
+
# This module provides predefined #pretty_print methods for some of
|
280
|
+
# the most commonly used built-in classes for convenience.
|
281
|
+
def pretty_print(q)
|
282
|
+
method_method = Object.instance_method(:method).bind(self)
|
283
|
+
begin
|
284
|
+
inspect_method = method_method.call(:inspect)
|
285
|
+
rescue NameError
|
286
|
+
end
|
287
|
+
if inspect_method && inspect_method.owner != Kernel
|
288
|
+
q.text self.inspect
|
289
|
+
elsif !inspect_method && self.respond_to?(:inspect)
|
290
|
+
q.text self.inspect
|
291
|
+
else
|
292
|
+
q.pp_object(self)
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
296
|
+
# A default pretty printing method for general objects that are
|
297
|
+
# detected as part of a cycle.
|
298
|
+
def pretty_print_cycle(q)
|
299
|
+
q.object_address_group(self) {
|
300
|
+
q.breakable
|
301
|
+
q.text '...'
|
302
|
+
}
|
303
|
+
end
|
304
|
+
|
305
|
+
# Returns a sorted array of instance variable names.
|
306
|
+
#
|
307
|
+
# This method should return an array of names of instance variables as symbols or strings as:
|
308
|
+
# +[:@a, :@b]+.
|
309
|
+
def pretty_print_instance_variables
|
310
|
+
instance_variables.sort
|
311
|
+
end
|
312
|
+
|
313
|
+
# Is #inspect implementation using #pretty_print.
|
314
|
+
# If you implement #pretty_print, it can be used as follows.
|
315
|
+
#
|
316
|
+
# alias inspect pretty_print_inspect
|
317
|
+
#
|
318
|
+
# However, doing this requires that every class that #inspect is called on
|
319
|
+
# implement #pretty_print, or a RuntimeError will be raised.
|
320
|
+
def pretty_print_inspect
|
321
|
+
if Object.instance_method(:method).bind(self).call(:pretty_print).owner == PP::ObjectMixin
|
322
|
+
raise "pretty_print is not overridden for #{self.class}"
|
323
|
+
end
|
324
|
+
PP.singleline_pp(self, ''.dup)
|
325
|
+
end
|
326
|
+
end
|
327
|
+
end
|
328
|
+
|
329
|
+
class Array # :nodoc:
|
330
|
+
def pretty_print(q) # :nodoc:
|
331
|
+
q.group(1, '[', ']') {
|
332
|
+
q.seplist(self) {|v|
|
333
|
+
q.pp v
|
334
|
+
}
|
335
|
+
}
|
336
|
+
end
|
337
|
+
|
338
|
+
def pretty_print_cycle(q) # :nodoc:
|
339
|
+
q.text(empty? ? '[]' : '[...]')
|
340
|
+
end
|
341
|
+
end
|
342
|
+
|
343
|
+
class Hash # :nodoc:
|
344
|
+
def pretty_print(q) # :nodoc:
|
345
|
+
q.pp_hash self
|
346
|
+
end
|
347
|
+
|
348
|
+
def pretty_print_cycle(q) # :nodoc:
|
349
|
+
q.text(empty? ? '{}' : '{...}')
|
350
|
+
end
|
351
|
+
end
|
352
|
+
|
353
|
+
class << ENV # :nodoc:
|
354
|
+
def pretty_print(q) # :nodoc:
|
355
|
+
h = {}
|
356
|
+
ENV.keys.sort.each {|k|
|
357
|
+
h[k] = ENV[k]
|
358
|
+
}
|
359
|
+
q.pp_hash h
|
360
|
+
end
|
361
|
+
end
|
362
|
+
|
363
|
+
class Struct # :nodoc:
|
364
|
+
def pretty_print(q) # :nodoc:
|
365
|
+
q.group(1, sprintf("#<struct %s", PP.mcall(self, Kernel, :class).name), '>') {
|
366
|
+
q.seplist(PP.mcall(self, Struct, :members), lambda { q.text "," }) {|member|
|
367
|
+
q.breakable
|
368
|
+
q.text member.to_s
|
369
|
+
q.text '='
|
370
|
+
q.group(1) {
|
371
|
+
q.breakable ''
|
372
|
+
q.pp self[member]
|
373
|
+
}
|
374
|
+
}
|
375
|
+
}
|
376
|
+
end
|
377
|
+
|
378
|
+
def pretty_print_cycle(q) # :nodoc:
|
379
|
+
q.text sprintf("#<struct %s:...>", PP.mcall(self, Kernel, :class).name)
|
380
|
+
end
|
381
|
+
end
|
382
|
+
|
383
|
+
class Range # :nodoc:
|
384
|
+
def pretty_print(q) # :nodoc:
|
385
|
+
q.pp self.begin
|
386
|
+
q.breakable ''
|
387
|
+
q.text(self.exclude_end? ? '...' : '..')
|
388
|
+
q.breakable ''
|
389
|
+
q.pp self.end if self.end
|
390
|
+
end
|
391
|
+
end
|
392
|
+
|
393
|
+
class String # :nodoc:
|
394
|
+
def pretty_print(q) # :nodoc:
|
395
|
+
lines = self.lines
|
396
|
+
if lines.size > 1
|
397
|
+
q.group(0, '', '') do
|
398
|
+
q.seplist(lines, lambda { q.text ' +'; q.breakable }) do |v|
|
399
|
+
q.pp v
|
400
|
+
end
|
401
|
+
end
|
402
|
+
else
|
403
|
+
q.text inspect
|
404
|
+
end
|
405
|
+
end
|
406
|
+
end
|
407
|
+
|
408
|
+
class File < IO # :nodoc:
|
409
|
+
class Stat # :nodoc:
|
410
|
+
def pretty_print(q) # :nodoc:
|
411
|
+
require 'etc.so'
|
412
|
+
q.object_group(self) {
|
413
|
+
q.breakable
|
414
|
+
q.text sprintf("dev=0x%x", self.dev); q.comma_breakable
|
415
|
+
q.text "ino="; q.pp self.ino; q.comma_breakable
|
416
|
+
q.group {
|
417
|
+
m = self.mode
|
418
|
+
q.text sprintf("mode=0%o", m)
|
419
|
+
q.breakable
|
420
|
+
q.text sprintf("(%s %c%c%c%c%c%c%c%c%c)",
|
421
|
+
self.ftype,
|
422
|
+
(m & 0400 == 0 ? ?- : ?r),
|
423
|
+
(m & 0200 == 0 ? ?- : ?w),
|
424
|
+
(m & 0100 == 0 ? (m & 04000 == 0 ? ?- : ?S) :
|
425
|
+
(m & 04000 == 0 ? ?x : ?s)),
|
426
|
+
(m & 0040 == 0 ? ?- : ?r),
|
427
|
+
(m & 0020 == 0 ? ?- : ?w),
|
428
|
+
(m & 0010 == 0 ? (m & 02000 == 0 ? ?- : ?S) :
|
429
|
+
(m & 02000 == 0 ? ?x : ?s)),
|
430
|
+
(m & 0004 == 0 ? ?- : ?r),
|
431
|
+
(m & 0002 == 0 ? ?- : ?w),
|
432
|
+
(m & 0001 == 0 ? (m & 01000 == 0 ? ?- : ?T) :
|
433
|
+
(m & 01000 == 0 ? ?x : ?t)))
|
434
|
+
}
|
435
|
+
q.comma_breakable
|
436
|
+
q.text "nlink="; q.pp self.nlink; q.comma_breakable
|
437
|
+
q.group {
|
438
|
+
q.text "uid="; q.pp self.uid
|
439
|
+
begin
|
440
|
+
pw = Etc.getpwuid(self.uid)
|
441
|
+
rescue ArgumentError
|
442
|
+
end
|
443
|
+
if pw
|
444
|
+
q.breakable; q.text "(#{pw.name})"
|
445
|
+
end
|
446
|
+
}
|
447
|
+
q.comma_breakable
|
448
|
+
q.group {
|
449
|
+
q.text "gid="; q.pp self.gid
|
450
|
+
begin
|
451
|
+
gr = Etc.getgrgid(self.gid)
|
452
|
+
rescue ArgumentError
|
453
|
+
end
|
454
|
+
if gr
|
455
|
+
q.breakable; q.text "(#{gr.name})"
|
456
|
+
end
|
457
|
+
}
|
458
|
+
q.comma_breakable
|
459
|
+
q.group {
|
460
|
+
q.text sprintf("rdev=0x%x", self.rdev)
|
461
|
+
if self.rdev_major && self.rdev_minor
|
462
|
+
q.breakable
|
463
|
+
q.text sprintf('(%d, %d)', self.rdev_major, self.rdev_minor)
|
464
|
+
end
|
465
|
+
}
|
466
|
+
q.comma_breakable
|
467
|
+
q.text "size="; q.pp self.size; q.comma_breakable
|
468
|
+
q.text "blksize="; q.pp self.blksize; q.comma_breakable
|
469
|
+
q.text "blocks="; q.pp self.blocks; q.comma_breakable
|
470
|
+
q.group {
|
471
|
+
t = self.atime
|
472
|
+
q.text "atime="; q.pp t
|
473
|
+
q.breakable; q.text "(#{t.tv_sec})"
|
474
|
+
}
|
475
|
+
q.comma_breakable
|
476
|
+
q.group {
|
477
|
+
t = self.mtime
|
478
|
+
q.text "mtime="; q.pp t
|
479
|
+
q.breakable; q.text "(#{t.tv_sec})"
|
480
|
+
}
|
481
|
+
q.comma_breakable
|
482
|
+
q.group {
|
483
|
+
t = self.ctime
|
484
|
+
q.text "ctime="; q.pp t
|
485
|
+
q.breakable; q.text "(#{t.tv_sec})"
|
486
|
+
}
|
487
|
+
}
|
488
|
+
end
|
489
|
+
end
|
490
|
+
end
|
491
|
+
|
492
|
+
class MatchData # :nodoc:
|
493
|
+
def pretty_print(q) # :nodoc:
|
494
|
+
nc = []
|
495
|
+
self.regexp.named_captures.each {|name, indexes|
|
496
|
+
indexes.each {|i| nc[i] = name }
|
497
|
+
}
|
498
|
+
q.object_group(self) {
|
499
|
+
q.breakable
|
500
|
+
q.seplist(0...self.size, lambda { q.breakable }) {|i|
|
501
|
+
if i == 0
|
502
|
+
q.pp self[i]
|
503
|
+
else
|
504
|
+
if nc[i]
|
505
|
+
q.text nc[i]
|
506
|
+
else
|
507
|
+
q.pp i
|
508
|
+
end
|
509
|
+
q.text ':'
|
510
|
+
q.pp self[i]
|
511
|
+
end
|
512
|
+
}
|
513
|
+
}
|
514
|
+
end
|
515
|
+
end
|
516
|
+
|
517
|
+
class RubyVM::AbstractSyntaxTree::Node
|
518
|
+
def pretty_print_children(q, names = [])
|
519
|
+
children.zip(names) do |c, n|
|
520
|
+
if n
|
521
|
+
q.breakable
|
522
|
+
q.text "#{n}:"
|
523
|
+
end
|
524
|
+
q.group(2) do
|
525
|
+
q.breakable
|
526
|
+
q.pp c
|
527
|
+
end
|
528
|
+
end
|
529
|
+
end
|
530
|
+
|
531
|
+
def pretty_print(q)
|
532
|
+
q.group(1, "(#{type}@#{first_lineno}:#{first_column}-#{last_lineno}:#{last_column}", ")") {
|
533
|
+
case type
|
534
|
+
when :SCOPE
|
535
|
+
pretty_print_children(q, %w"tbl args body")
|
536
|
+
when :ARGS
|
537
|
+
pretty_print_children(q, %w[pre_num pre_init opt first_post post_num post_init rest kw kwrest block])
|
538
|
+
when :DEFN
|
539
|
+
pretty_print_children(q, %w[mid body])
|
540
|
+
when :ARYPTN
|
541
|
+
pretty_print_children(q, %w[const pre rest post])
|
542
|
+
when :HSHPTN
|
543
|
+
pretty_print_children(q, %w[const kw kwrest])
|
544
|
+
else
|
545
|
+
pretty_print_children(q)
|
546
|
+
end
|
547
|
+
}
|
548
|
+
end
|
549
|
+
end
|
550
|
+
|
551
|
+
class Object < BasicObject # :nodoc:
|
552
|
+
include PP::ObjectMixin
|
553
|
+
end
|
554
|
+
|
555
|
+
[Numeric, Symbol, FalseClass, TrueClass, NilClass, Module].each {|c|
|
556
|
+
c.class_eval {
|
557
|
+
def pretty_print_cycle(q)
|
558
|
+
q.text inspect
|
559
|
+
end
|
560
|
+
}
|
561
|
+
}
|
562
|
+
|
563
|
+
[Numeric, FalseClass, TrueClass, Module].each {|c|
|
564
|
+
c.class_eval {
|
565
|
+
def pretty_print(q)
|
566
|
+
q.text inspect
|
567
|
+
end
|
568
|
+
}
|
569
|
+
}
|
570
|
+
|
571
|
+
module Kernel
|
572
|
+
# Returns a pretty printed object as a string.
|
573
|
+
#
|
574
|
+
# In order to use this method you must first require the PP module:
|
575
|
+
#
|
576
|
+
# require 'pp'
|
577
|
+
#
|
578
|
+
# See the PP module for more information.
|
579
|
+
def pretty_inspect
|
580
|
+
PP.pp(self, ''.dup)
|
581
|
+
end
|
582
|
+
|
583
|
+
# prints arguments in pretty form.
|
584
|
+
#
|
585
|
+
# pp returns argument(s).
|
586
|
+
def pp(*objs)
|
587
|
+
objs.each {|obj|
|
588
|
+
PP.pp(obj)
|
589
|
+
}
|
590
|
+
objs.size <= 1 ? objs.first : objs
|
591
|
+
end
|
592
|
+
module_function :pp
|
593
|
+
end
|
data/pp.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "pp"
|
3
|
+
spec.version = "0.1.0"
|
4
|
+
spec.authors = ["Tanaka Akira"]
|
5
|
+
spec.email = ["akr@fsij.org"]
|
6
|
+
|
7
|
+
spec.summary = %q{Provides a PrettyPrinter for Ruby objects}
|
8
|
+
spec.description = %q{Provides a PrettyPrinter for Ruby objects}
|
9
|
+
spec.homepage = "https://github.com/ruby/pp"
|
10
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
11
|
+
spec.licenses = ["Ruby", "BSD-2-Clause"]
|
12
|
+
|
13
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
14
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
15
|
+
|
16
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
17
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tanaka Akira
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-09-18 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Provides a PrettyPrinter for Ruby objects
|
14
|
+
email:
|
15
|
+
- akr@fsij.org
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".travis.yml"
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- bin/console
|
27
|
+
- bin/setup
|
28
|
+
- lib/pp.rb
|
29
|
+
- pp.gemspec
|
30
|
+
homepage: https://github.com/ruby/pp
|
31
|
+
licenses:
|
32
|
+
- Ruby
|
33
|
+
- BSD-2-Clause
|
34
|
+
metadata:
|
35
|
+
homepage_uri: https://github.com/ruby/pp
|
36
|
+
source_code_uri: https://github.com/ruby/pp
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.3.0
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubygems_version: 3.2.0.rc.1
|
53
|
+
signing_key:
|
54
|
+
specification_version: 4
|
55
|
+
summary: Provides a PrettyPrinter for Ruby objects
|
56
|
+
test_files: []
|