pp-colour 0.1.0
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/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +25 -0
- data/Rakefile +55 -0
- data/VERSION +1 -0
- data/lib/pp-colour.rb +11 -0
- data/lib/pp-colour/array.rb +14 -0
- data/lib/pp-colour/file.rb +82 -0
- data/lib/pp-colour/hash.rb +10 -0
- data/lib/pp-colour/kernel.rb +19 -0
- data/lib/pp-colour/matchdata.rb +11 -0
- data/lib/pp-colour/object.rb +3 -0
- data/lib/pp-colour/other.rb +23 -0
- data/lib/pp-colour/range.rb +10 -0
- data/lib/pp-colour/string.rb +55 -0
- data/lib/pp-colour/struct.rb +20 -0
- data/lib/pp-colour/temp.rb +294 -0
- data/pp-colour.gemspec +67 -0
- data/test/helper.rb +15 -0
- data/test/setup.rb +46 -0
- data/test/test_test.rb +135 -0
- metadata +88 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Stefan Penner
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
= pp-colour
|
2
|
+
|
3
|
+
A colourized version of ruby's builtin pp. Not fully complete, but it coveres the common cases.
|
4
|
+
|
5
|
+
= install
|
6
|
+
sudo gem install pp-colour
|
7
|
+
|
8
|
+
= usage
|
9
|
+
require 'pp-colour'
|
10
|
+
pp some_awesomely_huge_datastructure
|
11
|
+
> syntax highlighted goodness
|
12
|
+
|
13
|
+
== Note on Patches/Pull Requests
|
14
|
+
|
15
|
+
* Fork the project.
|
16
|
+
* Make your feature addition or bug fix.
|
17
|
+
* Add tests for it. This is important so I don't break it in a
|
18
|
+
future version unintentionally.
|
19
|
+
* Commit, do not mess with rakefile, version, or history.
|
20
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
21
|
+
* Send me a pull request. Bonus points for topic branches.
|
22
|
+
|
23
|
+
== Copyright
|
24
|
+
|
25
|
+
Copyright (c) 2009 Stefan Penner. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "pp-colour"
|
8
|
+
gem.summary = %Q{colourized pp}
|
9
|
+
gem.description = %Q{colourized version of the builtin pp}
|
10
|
+
gem.email = "stefan.penner@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/stefanpenner/pp-colour"
|
12
|
+
gem.authors = ["Stefan Penner"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "pp-colour #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
54
|
+
|
55
|
+
Jeweler::GemcutterTasks.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/pp-colour.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'prettyprint'
|
2
|
+
require 'pp-colour/string'
|
3
|
+
require 'pp-colour/kernel'
|
4
|
+
require 'pp-colour/temp'
|
5
|
+
require 'pp-colour/array'
|
6
|
+
require 'pp-colour/hash'
|
7
|
+
require 'pp-colour/struct'
|
8
|
+
require 'pp-colour/file'
|
9
|
+
require 'pp-colour/range'
|
10
|
+
require 'pp-colour/matchdata'
|
11
|
+
require 'pp-colour/other'
|
@@ -0,0 +1,82 @@
|
|
1
|
+
class File
|
2
|
+
class Stat
|
3
|
+
def pretty_print(q)
|
4
|
+
require 'etc.so'
|
5
|
+
q.object_group(self) {
|
6
|
+
q.breakable
|
7
|
+
q.text sprintf("dev=0x%x", self.dev); q.comma_breakable
|
8
|
+
q.text "ino="; q.pp self.ino; q.comma_breakable
|
9
|
+
q.group {
|
10
|
+
m = self.mode
|
11
|
+
q.text sprintf("mode=0%o", m)
|
12
|
+
q.breakable
|
13
|
+
q.text sprintf("(%s %c%c%c%c%c%c%c%c%c)",
|
14
|
+
self.ftype,
|
15
|
+
(m & 0400 == 0 ? ?- : ?r),
|
16
|
+
(m & 0200 == 0 ? ?- : ?w),
|
17
|
+
(m & 0100 == 0 ? (m & 04000 == 0 ? ?- : ?S) :
|
18
|
+
(m & 04000 == 0 ? ?x : ?s)),
|
19
|
+
(m & 0040 == 0 ? ?- : ?r),
|
20
|
+
(m & 0020 == 0 ? ?- : ?w),
|
21
|
+
(m & 0010 == 0 ? (m & 02000 == 0 ? ?- : ?S) :
|
22
|
+
(m & 02000 == 0 ? ?x : ?s)),
|
23
|
+
(m & 0004 == 0 ? ?- : ?r),
|
24
|
+
(m & 0002 == 0 ? ?- : ?w),
|
25
|
+
(m & 0001 == 0 ? (m & 01000 == 0 ? ?- : ?T) :
|
26
|
+
(m & 01000 == 0 ? ?x : ?t)))
|
27
|
+
}
|
28
|
+
q.comma_breakable
|
29
|
+
q.text "nlink="; q.pp self.nlink; q.comma_breakable
|
30
|
+
q.group {
|
31
|
+
q.text "uid="; q.pp self.uid
|
32
|
+
begin
|
33
|
+
pw = Etc.getpwuid(self.uid)
|
34
|
+
rescue ArgumentError
|
35
|
+
end
|
36
|
+
if pw
|
37
|
+
q.breakable; q.text "(#{pw.name})"
|
38
|
+
end
|
39
|
+
}
|
40
|
+
q.comma_breakable
|
41
|
+
q.group {
|
42
|
+
q.text "gid="; q.pp self.gid
|
43
|
+
begin
|
44
|
+
gr = Etc.getgrgid(self.gid)
|
45
|
+
rescue ArgumentError
|
46
|
+
end
|
47
|
+
if gr
|
48
|
+
q.breakable; q.text "(#{gr.name})"
|
49
|
+
end
|
50
|
+
}
|
51
|
+
q.comma_breakable
|
52
|
+
q.group {
|
53
|
+
q.text sprintf("rdev=0x%x", self.rdev)
|
54
|
+
q.breakable
|
55
|
+
q.text sprintf('(%d, %d)', self.rdev_major, self.rdev_minor)
|
56
|
+
}
|
57
|
+
q.comma_breakable
|
58
|
+
q.text "size="; q.pp self.size; q.comma_breakable
|
59
|
+
q.text "blksize="; q.pp self.blksize; q.comma_breakable
|
60
|
+
q.text "blocks="; q.pp self.blocks; q.comma_breakable
|
61
|
+
q.group {
|
62
|
+
t = self.atime
|
63
|
+
q.text "atime="; q.pp t
|
64
|
+
q.breakable; q.text "(#{t.tv_sec})"
|
65
|
+
}
|
66
|
+
q.comma_breakable
|
67
|
+
q.group {
|
68
|
+
t = self.mtime
|
69
|
+
q.text "mtime="; q.pp t
|
70
|
+
q.breakable; q.text "(#{t.tv_sec})"
|
71
|
+
}
|
72
|
+
q.comma_breakable
|
73
|
+
q.group {
|
74
|
+
t = self.ctime
|
75
|
+
q.text "ctime="; q.pp t
|
76
|
+
q.breakable; q.text "(#{t.tv_sec})"
|
77
|
+
}
|
78
|
+
}
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Kernel
|
2
|
+
# returns a pretty printed object as a string.
|
3
|
+
def pretty_inspect
|
4
|
+
PP.pp(self, '')
|
5
|
+
end
|
6
|
+
|
7
|
+
private
|
8
|
+
# prints arguments in pretty form.
|
9
|
+
#
|
10
|
+
# pp returns nil.
|
11
|
+
def pp(*objs) # :doc:
|
12
|
+
objs.each {|obj|
|
13
|
+
PP.pp(obj)
|
14
|
+
}
|
15
|
+
nil
|
16
|
+
end
|
17
|
+
module_function :pp
|
18
|
+
end
|
19
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
[Numeric, Symbol, FalseClass, TrueClass, NilClass, Module].each do |c|
|
2
|
+
c.class_eval do
|
3
|
+
def pretty_print_cycle(q)
|
4
|
+
q.text inspect.cyan
|
5
|
+
end
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
[Numeric].each do |c|
|
10
|
+
c.class_eval do
|
11
|
+
def pretty_print(q)
|
12
|
+
q.text inspect.cyan
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
[FalseClass, TrueClass, Module].each do |c|
|
18
|
+
c.class_eval do
|
19
|
+
def pretty_print(q)
|
20
|
+
q.text inspect.green.bold
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# credit goes to burke@burkelibbey.org
|
2
|
+
class String
|
3
|
+
COLORS = {
|
4
|
+
:black => '0',
|
5
|
+
:red => '1',
|
6
|
+
:green => '2',
|
7
|
+
:yellow => '3',
|
8
|
+
:blue => '4',
|
9
|
+
:magenta => '5',
|
10
|
+
:cyan => '6',
|
11
|
+
:white => '7'
|
12
|
+
}
|
13
|
+
|
14
|
+
FORMAT_CODES = {
|
15
|
+
:bold => '1',
|
16
|
+
:italic => '3',
|
17
|
+
:underline => '4',
|
18
|
+
:blink => '5',
|
19
|
+
}
|
20
|
+
|
21
|
+
def ansi_formatted
|
22
|
+
ansi_csi = "#{(0x1B).chr}["
|
23
|
+
formats = {}
|
24
|
+
FORMAT_CODES.each do |name, code|
|
25
|
+
formats[name] = code if @properties[name]
|
26
|
+
end
|
27
|
+
formats[:color] = "3#{COLORS[@properties[:color]]}" if @properties[:color]
|
28
|
+
"#{ansi_csi}#{formats.values.join(';')}m#{self}#{ansi_csi}m"
|
29
|
+
end
|
30
|
+
|
31
|
+
def make_colorized(color)
|
32
|
+
@properties ||= {}
|
33
|
+
@properties[:color] = color
|
34
|
+
ansi_formatted
|
35
|
+
end
|
36
|
+
|
37
|
+
COLORS.each do |color,v|
|
38
|
+
eval <<-EOS
|
39
|
+
def #{color.to_s}
|
40
|
+
make_colorized(:#{color})
|
41
|
+
end
|
42
|
+
EOS
|
43
|
+
end
|
44
|
+
|
45
|
+
FORMAT_CODES.each do |name, code|
|
46
|
+
eval <<-EOS
|
47
|
+
def #{name}
|
48
|
+
@properties ||= {}
|
49
|
+
@properties[:#{name}] = true
|
50
|
+
ansi_formatted
|
51
|
+
end
|
52
|
+
EOS
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Struct
|
2
|
+
def pretty_print(q)
|
3
|
+
q.group(1, '#<struct ' + PP.mcall(self, Kernel, :class).name, '>') {
|
4
|
+
q.seplist(PP.mcall(self, Struct, :members), lambda { q.text "," }) {|member|
|
5
|
+
q.breakable
|
6
|
+
q.text member.to_s
|
7
|
+
q.text '='
|
8
|
+
q.group(1) {
|
9
|
+
q.breakable ''
|
10
|
+
q.pp self[member]
|
11
|
+
}
|
12
|
+
}
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
def pretty_print_cycle(q)
|
17
|
+
q.text sprintf("#<struct %s:...>", PP.mcall(self, Kernel, :class).name)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,294 @@
|
|
1
|
+
|
2
|
+
# == Pretty-printer for Ruby objects.
|
3
|
+
#
|
4
|
+
# = Which seems better?
|
5
|
+
#
|
6
|
+
# non-pretty-printed output by #p is:
|
7
|
+
# #<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>>
|
8
|
+
#
|
9
|
+
# pretty-printed output by #pp is:
|
10
|
+
# #<PP:0x81fedf0
|
11
|
+
# @buffer=[],
|
12
|
+
# @buffer_width=0,
|
13
|
+
# @genspace=#<Proc:0x81feda0>,
|
14
|
+
# @group_queue=
|
15
|
+
# #<PrettyPrint::GroupQueue:0x81fed3c
|
16
|
+
# @queue=
|
17
|
+
# [[#<PrettyPrint::Group:0x81fed78 @break=false, @breakables=[], @depth=0>],
|
18
|
+
# []]>,
|
19
|
+
# @group_stack=
|
20
|
+
# [#<PrettyPrint::Group:0x81fed78 @break=false, @breakables=[], @depth=0>],
|
21
|
+
# @indent=0,
|
22
|
+
# @maxwidth=79,
|
23
|
+
# @newline="\n",
|
24
|
+
# @output=#<IO:0x8114ee4>,
|
25
|
+
# @output_width=2>
|
26
|
+
#
|
27
|
+
# I like the latter. If you do too, this library is for you.
|
28
|
+
#
|
29
|
+
# = Usage
|
30
|
+
#
|
31
|
+
# pp(obj)
|
32
|
+
#
|
33
|
+
# output +obj+ to +$>+ in pretty printed format.
|
34
|
+
#
|
35
|
+
# It returns +nil+.
|
36
|
+
#
|
37
|
+
# = Output Customization
|
38
|
+
# To define your customized pretty printing function for your classes,
|
39
|
+
# redefine a method #pretty_print(+pp+) in the class.
|
40
|
+
# It takes an argument +pp+ which is an instance of the class PP.
|
41
|
+
# The method should use PP#text, PP#breakable, PP#nest, PP#group and
|
42
|
+
# PP#pp to print the object.
|
43
|
+
#
|
44
|
+
# = Author
|
45
|
+
# Tanaka Akira <akr@m17n.org>
|
46
|
+
require 'prettyprint'
|
47
|
+
|
48
|
+
class PP < PrettyPrint
|
49
|
+
# Outputs +obj+ to +out+ in pretty printed format of
|
50
|
+
# +width+ columns in width.
|
51
|
+
#
|
52
|
+
# If +out+ is omitted, +$>+ is assumed.
|
53
|
+
# If +width+ is omitted, 79 is assumed.
|
54
|
+
#
|
55
|
+
# PP.pp returns +out+.
|
56
|
+
def PP.pp(obj, out=$>, width=79)
|
57
|
+
q = PP.new(out, width)
|
58
|
+
q.guard_inspect_key {q.pp obj}
|
59
|
+
q.flush
|
60
|
+
#$pp = q
|
61
|
+
out << "\n"
|
62
|
+
out
|
63
|
+
end
|
64
|
+
|
65
|
+
# Outputs +obj+ to +out+ like PP.pp but with no indent and
|
66
|
+
# newline.
|
67
|
+
#
|
68
|
+
# PP.singleline_pp returns +out+.
|
69
|
+
def PP.singleline_pp(obj, out=$>)
|
70
|
+
q = SingleLine.new(out)
|
71
|
+
q.guard_inspect_key {q.pp obj}
|
72
|
+
q.flush
|
73
|
+
out
|
74
|
+
end
|
75
|
+
|
76
|
+
# :stopdoc:
|
77
|
+
def PP.mcall(obj, mod, meth, *args, &block)
|
78
|
+
mod.instance_method(meth).bind(obj).call(*args, &block)
|
79
|
+
end
|
80
|
+
# :startdoc:
|
81
|
+
|
82
|
+
@sharing_detection = false
|
83
|
+
class << self
|
84
|
+
# Returns the sharing detection flag as a boolean value.
|
85
|
+
# It is false by default.
|
86
|
+
attr_accessor :sharing_detection
|
87
|
+
end
|
88
|
+
|
89
|
+
module PPMethods
|
90
|
+
InspectKey = :__inspect_key__ unless defined? InspectKey
|
91
|
+
|
92
|
+
def guard_inspect_key
|
93
|
+
if Thread.current[InspectKey] == nil
|
94
|
+
Thread.current[InspectKey] = []
|
95
|
+
end
|
96
|
+
|
97
|
+
save = Thread.current[InspectKey]
|
98
|
+
|
99
|
+
begin
|
100
|
+
Thread.current[InspectKey] = []
|
101
|
+
yield
|
102
|
+
ensure
|
103
|
+
Thread.current[InspectKey] = save
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# Adds +obj+ to the pretty printing buffer
|
108
|
+
# using Object#pretty_print or Object#pretty_print_cycle.
|
109
|
+
#
|
110
|
+
# Object#pretty_print_cycle is used when +obj+ is already
|
111
|
+
# printed, a.k.a the object reference chain has a cycle.
|
112
|
+
def pp(obj)
|
113
|
+
id = obj.__id__
|
114
|
+
|
115
|
+
if Thread.current[InspectKey].include? id
|
116
|
+
group {obj.pretty_print_cycle self}
|
117
|
+
return
|
118
|
+
end
|
119
|
+
|
120
|
+
begin
|
121
|
+
Thread.current[InspectKey] << id
|
122
|
+
group {obj.pretty_print self}
|
123
|
+
ensure
|
124
|
+
Thread.current[InspectKey].pop unless PP.sharing_detection
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
# A convenience method which is same as follows:
|
129
|
+
#
|
130
|
+
# group(1, '#<' + obj.class.name, '>') { ... }
|
131
|
+
def object_group(obj, &block) # :yield:
|
132
|
+
group(1, '#<'.green + obj.class.name.magenta, '>'.green, &block)
|
133
|
+
end
|
134
|
+
|
135
|
+
def object_address_group(obj, &block)
|
136
|
+
id = "%x" % (obj.__id__ * 2)
|
137
|
+
id.sub!(/\Af(?=[[:xdigit:]]{2}+\z)/, '') if id.sub!(/\A\.\./, '')
|
138
|
+
group(1, "\#<#{obj.class.to_s.magenta}:".green + "0x#{id}".blue, '>'.green, &block)
|
139
|
+
end
|
140
|
+
|
141
|
+
# A convenience method which is same as follows:
|
142
|
+
#
|
143
|
+
# text ','
|
144
|
+
# breakable
|
145
|
+
def comma_breakable
|
146
|
+
text ','.blue
|
147
|
+
breakable
|
148
|
+
end
|
149
|
+
|
150
|
+
# Adds a separated list.
|
151
|
+
# The list is separated by comma with breakable space, by default.
|
152
|
+
#
|
153
|
+
# #seplist iterates the +list+ using +iter_method+.
|
154
|
+
# It yields each object to the block given for #seplist.
|
155
|
+
# The procedure +separator_proc+ is called between each yields.
|
156
|
+
#
|
157
|
+
# If the iteration is zero times, +separator_proc+ is not called at all.
|
158
|
+
#
|
159
|
+
# If +separator_proc+ is nil or not given,
|
160
|
+
# +lambda { comma_breakable }+ is used.
|
161
|
+
# If +iter_method+ is not given, :each is used.
|
162
|
+
#
|
163
|
+
# For example, following 3 code fragments has similar effect.
|
164
|
+
#
|
165
|
+
# q.seplist([1,2,3]) {|v| xxx v }
|
166
|
+
#
|
167
|
+
# q.seplist([1,2,3], lambda { comma_breakable }, :each) {|v| xxx v }
|
168
|
+
#
|
169
|
+
# xxx 1
|
170
|
+
# q.comma_breakable
|
171
|
+
# xxx 2
|
172
|
+
# q.comma_breakable
|
173
|
+
# xxx 3
|
174
|
+
def seplist(list, sep=nil, iter_method=:each) # :yield: element
|
175
|
+
sep ||= lambda { comma_breakable }
|
176
|
+
first = true
|
177
|
+
list.__send__(iter_method) {|*v|
|
178
|
+
if first
|
179
|
+
first = false
|
180
|
+
else
|
181
|
+
sep.call
|
182
|
+
end
|
183
|
+
yield(*v)
|
184
|
+
}
|
185
|
+
end
|
186
|
+
|
187
|
+
def pp_object(obj)
|
188
|
+
object_address_group(obj) {
|
189
|
+
seplist(obj.pretty_print_instance_variables, lambda { text ','.green }) {|v|
|
190
|
+
breakable
|
191
|
+
v = v.to_s if Symbol === v
|
192
|
+
text v
|
193
|
+
text '='
|
194
|
+
group(1) {
|
195
|
+
breakable ''
|
196
|
+
pp(obj.instance_eval(v))
|
197
|
+
}
|
198
|
+
}
|
199
|
+
}
|
200
|
+
end
|
201
|
+
|
202
|
+
def pp_hash(obj)
|
203
|
+
group(1, '{'.green, '}'.green) {
|
204
|
+
seplist(obj, nil, :each_pair) {|k, v|
|
205
|
+
group {
|
206
|
+
pp k
|
207
|
+
text '=>'.blue
|
208
|
+
group(1) {
|
209
|
+
breakable ''
|
210
|
+
pp v
|
211
|
+
}
|
212
|
+
}
|
213
|
+
}
|
214
|
+
}
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
include PPMethods
|
219
|
+
|
220
|
+
class SingleLine < PrettyPrint::SingleLine
|
221
|
+
include PPMethods
|
222
|
+
end
|
223
|
+
|
224
|
+
module ObjectMixin
|
225
|
+
# 1. specific pretty_print
|
226
|
+
# 2. specific inspect
|
227
|
+
# 3. specific to_s if instance variable is empty
|
228
|
+
# 4. generic pretty_print
|
229
|
+
|
230
|
+
# A default pretty printing method for general objects.
|
231
|
+
# It calls #pretty_print_instance_variables to list instance variables.
|
232
|
+
#
|
233
|
+
# If +self+ has a customized (redefined) #inspect method,
|
234
|
+
# the result of self.inspect is used but it obviously has no
|
235
|
+
# line break hints.
|
236
|
+
#
|
237
|
+
# This module provides predefined #pretty_print methods for some of
|
238
|
+
# the most commonly used built-in classes for convenience.
|
239
|
+
def pretty_print(q)
|
240
|
+
if /\(Kernel\)#/ !~ Object.instance_method(:method).bind(self).call(:inspect).inspect
|
241
|
+
q.text self.inspect.yellow.bold
|
242
|
+
elsif /\(Kernel\)#/ !~ Object.instance_method(:method).bind(self).call(:to_s).inspect && instance_variables.empty?
|
243
|
+
q.text self.to_s
|
244
|
+
else
|
245
|
+
q.pp_object(self)
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
# A default pretty printing method for general objects that are
|
250
|
+
# detected as part of a cycle.
|
251
|
+
def pretty_print_cycle(q)
|
252
|
+
q.object_address_group(self) {
|
253
|
+
q.breakable
|
254
|
+
q.text '...'.green
|
255
|
+
}
|
256
|
+
end
|
257
|
+
|
258
|
+
# Returns a sorted array of instance variable names.
|
259
|
+
#
|
260
|
+
# This method should return an array of names of instance variables as symbols or strings as:
|
261
|
+
# +[:@a, :@b]+.
|
262
|
+
def pretty_print_instance_variables
|
263
|
+
instance_variables.sort
|
264
|
+
end
|
265
|
+
|
266
|
+
# Is #inspect implementation using #pretty_print.
|
267
|
+
# If you implement #pretty_print, it can be used as follows.
|
268
|
+
#
|
269
|
+
# alias inspect pretty_print_inspect
|
270
|
+
#
|
271
|
+
# However, doing this requires that every class that #inspect is called on
|
272
|
+
# implement #pretty_print, or a RuntimeError will be raised.
|
273
|
+
def pretty_print_inspect
|
274
|
+
if /\(PP::ObjectMixin\)#/ =~ Object.instance_method(:method).bind(self).call(:pretty_print).inspect
|
275
|
+
raise "pretty_print is not overridden for #{self.class}"
|
276
|
+
end
|
277
|
+
PP.singleline_pp(self, '')
|
278
|
+
end
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
class << ENV
|
283
|
+
def pretty_print(q)
|
284
|
+
q.pp_hash self
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
# :enddoc:
|
289
|
+
if __FILE__ == $0
|
290
|
+
require 'test/unit'
|
291
|
+
|
292
|
+
end
|
293
|
+
|
294
|
+
|
data/pp-colour.gemspec
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{pp-colour}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Stefan Penner"]
|
12
|
+
s.date = %q{2009-11-26}
|
13
|
+
s.description = %q{colourized version of the builtin pp}
|
14
|
+
s.email = %q{stefan.penner@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/pp-colour.rb",
|
27
|
+
"lib/pp-colour/array.rb",
|
28
|
+
"lib/pp-colour/file.rb",
|
29
|
+
"lib/pp-colour/hash.rb",
|
30
|
+
"lib/pp-colour/kernel.rb",
|
31
|
+
"lib/pp-colour/matchdata.rb",
|
32
|
+
"lib/pp-colour/object.rb",
|
33
|
+
"lib/pp-colour/other.rb",
|
34
|
+
"lib/pp-colour/range.rb",
|
35
|
+
"lib/pp-colour/string.rb",
|
36
|
+
"lib/pp-colour/struct.rb",
|
37
|
+
"lib/pp-colour/temp.rb",
|
38
|
+
"pp-colour.gemspec",
|
39
|
+
"test/helper.rb",
|
40
|
+
"test/setup.rb",
|
41
|
+
"test/test_test.rb"
|
42
|
+
]
|
43
|
+
s.homepage = %q{http://github.com/stefanpenner/pp-colour}
|
44
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
45
|
+
s.require_paths = ["lib"]
|
46
|
+
s.rubygems_version = %q{1.3.5}
|
47
|
+
s.summary = %q{colourized pp}
|
48
|
+
s.test_files = [
|
49
|
+
"test/helper.rb",
|
50
|
+
"test/setup.rb",
|
51
|
+
"test/test_test.rb"
|
52
|
+
]
|
53
|
+
|
54
|
+
if s.respond_to? :specification_version then
|
55
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
56
|
+
s.specification_version = 3
|
57
|
+
|
58
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
59
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
60
|
+
else
|
61
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
62
|
+
end
|
63
|
+
else
|
64
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..','lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'test/unit'
|
6
|
+
require 'shoulda'
|
7
|
+
|
8
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
9
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
10
|
+
require 'pp-colour'
|
11
|
+
require 'setup'
|
12
|
+
|
13
|
+
class Test::Unit::TestCase
|
14
|
+
end
|
15
|
+
|
data/test/setup.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
class HasInspect
|
2
|
+
def initialize(a)
|
3
|
+
@a = a
|
4
|
+
end
|
5
|
+
|
6
|
+
def inspect
|
7
|
+
return "<inspect:#{@a.inspect}>"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class HasPrettyPrint
|
12
|
+
def initialize(a)
|
13
|
+
@a = a
|
14
|
+
end
|
15
|
+
|
16
|
+
def pretty_print(q)
|
17
|
+
q.text "<pretty_print:"
|
18
|
+
q.pp @a
|
19
|
+
q.text ">"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class HasBoth
|
24
|
+
def initialize(a)
|
25
|
+
@a = a
|
26
|
+
end
|
27
|
+
|
28
|
+
def inspect
|
29
|
+
return "<inspect:#{@a.inspect}>"
|
30
|
+
end
|
31
|
+
|
32
|
+
def pretty_print(q)
|
33
|
+
q.text "<pretty_print:"
|
34
|
+
q.pp @a
|
35
|
+
q.text ">"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class PrettyPrintInspect < HasPrettyPrint
|
40
|
+
alias inspect pretty_print_inspect
|
41
|
+
end
|
42
|
+
|
43
|
+
class PrettyPrintInspectWithoutPrettyPrint
|
44
|
+
alias inspect pretty_print_inspect
|
45
|
+
end
|
46
|
+
|
data/test/test_test.rb
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestPPColour < Test::Unit::TestCase
|
4
|
+
def test_list0123_12
|
5
|
+
assert_equal("[0, 1, 2, 3]\n", PP.pp([0,1,2,3], '', 12))
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_list0123_11
|
9
|
+
assert_equal("[0,\n 1,\n 2,\n 3]\n", PP.pp([0,1,2,3], '', 11))
|
10
|
+
end
|
11
|
+
|
12
|
+
OverriddenStruct = Struct.new("OverriddenStruct", :members, :class)
|
13
|
+
def test_struct_override_members # [ruby-core:7865]
|
14
|
+
a = OverriddenStruct.new(1,2)
|
15
|
+
assert_equal("#<struct Struct::OverriddenStruct members=1, class=2>\n", PP.pp(a, ''))
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_redefined_method
|
19
|
+
o = ""
|
20
|
+
def o.method
|
21
|
+
end
|
22
|
+
assert_equal(%(""\n), PP.pp(o, ""))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class PPInspectTest < Test::Unit::TestCase
|
27
|
+
def test_hasinspect
|
28
|
+
a = HasInspect.new(1)
|
29
|
+
assert_equal("<inspect:1>\n", PP.pp(a, ''))
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_hasprettyprint
|
33
|
+
a = HasPrettyPrint.new(1)
|
34
|
+
assert_equal("<pretty_print:1>\n", PP.pp(a, ''))
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_hasboth
|
38
|
+
a = HasBoth.new(1)
|
39
|
+
assert_equal("<pretty_print:1>\n", PP.pp(a, ''))
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_pretty_print_inspect
|
43
|
+
a = PrettyPrintInspect.new(1)
|
44
|
+
assert_equal("<pretty_print:1>", a.inspect)
|
45
|
+
a = PrettyPrintInspectWithoutPrettyPrint.new
|
46
|
+
assert_raise(RuntimeError) { a.inspect }
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_proc
|
50
|
+
a = proc {1}
|
51
|
+
assert_equal("#{a.inspect}\n", PP.pp(a, ''))
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_to_s_with_iv
|
55
|
+
a = Object.new
|
56
|
+
def a.to_s() "aaa" end
|
57
|
+
a.instance_eval { @a = nil }
|
58
|
+
result = PP.pp(a, '')
|
59
|
+
assert_equal("#{a.inspect}\n", result)
|
60
|
+
assert_match(/\A#<Object.*>\n\z/m, result)
|
61
|
+
a = 1.0
|
62
|
+
a.instance_eval { @a = nil }
|
63
|
+
result = PP.pp(a, '')
|
64
|
+
assert_equal("#{a.inspect}\n", result)
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_to_s_without_iv
|
68
|
+
a = Object.new
|
69
|
+
def a.to_s() "aaa" end
|
70
|
+
result = PP.pp(a, '')
|
71
|
+
assert_equal("#{a.inspect}\n", result)
|
72
|
+
assert_equal("aaa\n", result)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class PPCycleTest < Test::Unit::TestCase
|
77
|
+
def test_array
|
78
|
+
a = []
|
79
|
+
a << a
|
80
|
+
assert_equal("[[...]]\n", PP.pp(a, ''))
|
81
|
+
assert_equal("#{a.inspect}\n", PP.pp(a, ''))
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_hash
|
85
|
+
a = {}
|
86
|
+
a[0] = a
|
87
|
+
assert_equal("{0=>{...}}\n", PP.pp(a, ''))
|
88
|
+
assert_equal("#{a.inspect}\n", PP.pp(a, ''))
|
89
|
+
end
|
90
|
+
|
91
|
+
S = Struct.new("S", :a, :b)
|
92
|
+
def test_struct
|
93
|
+
a = S.new(1,2)
|
94
|
+
a.b = a
|
95
|
+
assert_equal("#<struct Struct::S a=1, b=#<struct Struct::S:...>>\n", PP.pp(a, ''))
|
96
|
+
assert_equal("#{a.inspect}\n", PP.pp(a, ''))
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_object
|
100
|
+
a = Object.new
|
101
|
+
a.instance_eval {@a = a}
|
102
|
+
assert_equal(a.inspect + "\n", PP.pp(a, ''))
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_anonymous
|
106
|
+
a = Class.new.new
|
107
|
+
assert_equal(a.inspect + "\n", PP.pp(a, ''))
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_withinspect
|
111
|
+
a = []
|
112
|
+
a << HasInspect.new(a)
|
113
|
+
assert_equal("[<inspect:[...]>]\n", PP.pp(a, ''))
|
114
|
+
assert_equal("#{a.inspect}\n", PP.pp(a, ''))
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_share_nil
|
118
|
+
begin
|
119
|
+
PP.sharing_detection = true
|
120
|
+
a = [nil, nil]
|
121
|
+
assert_equal("[nil, nil]\n", PP.pp(a, ''))
|
122
|
+
ensure
|
123
|
+
PP.sharing_detection = false
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
class PPSingleLineTest < Test::Unit::TestCase
|
129
|
+
def test_hash
|
130
|
+
assert_equal("{1=>1}", PP.singleline_pp({ 1 => 1}, '')) # [ruby-core:02699]
|
131
|
+
assert_equal("[1#{', 1'*99}]", PP.singleline_pp([1]*100, ''))
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pp-colour
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stefan Penner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-26 00:00:00 -06:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: thoughtbot-shoulda
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: colourized version of the builtin pp
|
26
|
+
email: stefan.penner@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- lib/pp-colour.rb
|
42
|
+
- lib/pp-colour/array.rb
|
43
|
+
- lib/pp-colour/file.rb
|
44
|
+
- lib/pp-colour/hash.rb
|
45
|
+
- lib/pp-colour/kernel.rb
|
46
|
+
- lib/pp-colour/matchdata.rb
|
47
|
+
- lib/pp-colour/object.rb
|
48
|
+
- lib/pp-colour/other.rb
|
49
|
+
- lib/pp-colour/range.rb
|
50
|
+
- lib/pp-colour/string.rb
|
51
|
+
- lib/pp-colour/struct.rb
|
52
|
+
- lib/pp-colour/temp.rb
|
53
|
+
- pp-colour.gemspec
|
54
|
+
- test/helper.rb
|
55
|
+
- test/setup.rb
|
56
|
+
- test/test_test.rb
|
57
|
+
has_rdoc: true
|
58
|
+
homepage: http://github.com/stefanpenner/pp-colour
|
59
|
+
licenses: []
|
60
|
+
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options:
|
63
|
+
- --charset=UTF-8
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: "0"
|
77
|
+
version:
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.3.5
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: colourized pp
|
85
|
+
test_files:
|
86
|
+
- test/helper.rb
|
87
|
+
- test/setup.rb
|
88
|
+
- test/test_test.rb
|