forked from jimweirich/irb-setup
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lustro.rb
279 lines (246 loc) · 7.25 KB
/
lustro.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/ruby -wKU
module Lustro
PublicMethods = lambda { |cls| cls.public_instance_methods(false).sort }
PrivateMethods = lambda { |cls| cls.private_instance_methods(false).sort }
AllMethods = lambda { |cls|
(cls.public_instance_methods(false) +
cls.private_instance_methods(false)).uniq.sort
}
def self.formatter
@formatter ||= ColorFormatter.new
end
def self.formatter=(new_formatter)
@formatter = new_formatter
end
def self.methods_for_class(cls, getter=PublicMethods)
cls.ancestors.map { |ruby_class|
[ruby_class, normalize(getter[ruby_class])]
}
end
def self.methods_for_object(obj, getter=PublicMethods)
result = methods_for_class(obj.class, getter)
left_overs = obj.methods - obj.class.public_instance_methods
result.unshift([:singleton, normalize(left_overs)]) unless left_overs.empty?
result
end
def self.format_help
puts "Usage: m obj[, options]"
puts
puts "Options:"
puts " <int> -- Include only the first <int> ancestors."
puts " <-int> -- Omit the last <int> ancestors."
puts " /re/ -- Include only methods matching re."
puts " <class> -- Include only methods from class."
puts " :deep -- Include Object and its ancestors."
puts " :omit, <class> (:o) -- Exclude <class> and its ancestors."
puts " :instance (:i) -- Instance methods when obj is a class."
puts " :class (:c) -- Class methods from an instance."
puts " :public (:pub) -- Public methods only (default)."
puts " :private (:p) -- Private methods only."
puts " :all -- Public and private methods."
puts " :ancestors (:a) -- Display Ancestors."
puts " :singleton (:s) -- Include only singleton methods."
puts " :flat (:f) -- Flatten the display into a single list of methods."
puts " :full -- List all ancestors, even if method list is empty."
puts " :noalpha (:na) -- Disable alphabetic bin sorting."
puts " :nocolor (:nc) -- Display without color."
end
def self.format(*args)
if args.empty?
format_help
return
end
obj = args.shift
fmt = formatter
format_opts = {}
methods = methods_for_object(obj)
args << :omit << Object unless args.any? { |arg|
arg.is_a?(Integer) ||
arg.is_a?(Module) ||
arg == :singleton || arg == :s ||
arg == :deep || arg == :d
}
args.each do |arg|
if format_opts[:omit] == :omit
fail "'#{arg}' should be a class" unless arg.is_a?(Class)
format_opts[:omit] = arg
next
end
case arg
when Regexp
methods = filter(methods, arg)
when Integer
if arg < 0
methods = methods[0 .. arg-1]
elsif arg > 0
methods = methods[0,arg]
else
# do nothing
end
when Module, Class
methods = [methods.assoc(arg) || ["<#{arg} not found>", []]]
when :deep, :d
# do nothing
when :singleton, :s
methods = [methods.assoc(:singleton) || ["<:singleton not found>", []]]
when :instance, :i
fail ":instance requires a class object" unless obj.is_a?(Module)
methods = methods_for_class(obj)
when :class, :c
methods = methods_for_object(obj.class)
when :full
format_opts[:full] = true
when :private, :priv, :p
methods = methods_for_object(obj, PrivateMethods)
if methods.first.first == :singleton
methods.shift
end
format_opts[:label] = "private"
when :public, :pub
methods = methods_for_object(obj, PublicMethods)
when :all
methods = methods_for_object(obj, AllMethods)
format_opts[:label] = "all"
when :omit, :o
format_opts[:omit] = :omit
when :flat, :f
format_opts[:flat] = true
when :noalpha, :na
format_opts[:noalpha] = true
when :nocolor, :nc
fmt = Formatter.new
when :ancestors, :a
fmt.display_method_list(obj.class.ancestors)
return
else
puts "Unrecognized option: #{arg.inspect}"
return
end
end
fmt.display(methods, format_opts)
nil
end
def self.filter(methods, re)
methods.map { |rc, ms|
list = ms.grep(re)
[rc, list]
}
end
def self.normalize(list)
list.map { |it| it.to_s }.sort
end
class Formatter
attr_reader :options
def emit(string)
puts string
end
def display(methods, opts)
@options = opts
if options[:flat]
list = methods.map { |rc, ms| ms }.flatten.sort
the_class = methods.map { |m| m.first }.detect { |m| ! m.is_a?(Symbol) }
display_scope(["#{the_class} (flat)", list])
else
methods.each do |scope|
break if scope.first == options[:omit]
display_scope(scope)
end
end
@options = {}
end
def display_break
emit
end
def display_class(ruby_class, method_list)
string = ruby_class.to_s
string << "/#{options[:label]}" if options[:label]
string << " (#{method_list.size})"
emit string
end
def display_scope(scope)
ruby_class, method_list = scope
if ! method_list.empty? || options[:full]
display_class(ruby_class, method_list)
display_methods(method_list)
display_break
end
end
def display_methods(methods)
if options[:noalpha]
display_method_list(methods)
else
categories = categorize_methods(methods)
display_categories(categories)
end
end
def display_categories(categories)
categories.keys.sort.each do |k|
display_method_list(categories[k])
end
end
def display_method_list(method_list)
emit " #{method_list.join(' ')}"
end
def categorize_methods(methods)
result = Hash.new { |h,k| h[k] = [] }
methods.sort.each do |m|
if m =~ /^[a-zA-Z]/
result[m[0,1]] << m
else
result["@"] << m
end
end
result
end
end
module Color
#shamelessly stolen (and modified) from redgreen
COLORS = {
:clear => 0, :black => 30, :red => 31,
:green => 32, :yellow => 33, :blue => 34,
:magenta => 35, :cyan => 36,
}
module_function
COLORS.each do |color, value|
module_eval "def #{color}(string); colorize(string, #{value}); end"
module_function color
end
def colorize(string, color_value)
if ENV['NO_COLOR']
string
else
color(color_value) + string.to_s + color(COLORS[:clear])
end
end
def color(color_value)
"\e[#{color_value}m"
end
end
class ColorFormatter < Formatter
include Color
def emit(str="")
if @color
puts @color[str]
else
puts str
end
end
def c(color)
@color = lambda { |s| send(color, s) }
yield
ensure
@color = nil
end
def display_class(*args)
c(:yellow) { super }
end
def display_method_list(*args)
c(:cyan) { super }
end
end
self.formatter = ColorFormatter.new
end
def m(*args)
Lustro.format(*args)
nil
end