2021-03-18 03:28:04 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-03-21 02:15:51 -04:00
|
|
|
require "reline"
|
2021-03-18 03:28:04 -04:00
|
|
|
require_relative "nop"
|
|
|
|
require_relative "../color"
|
|
|
|
|
|
|
|
# :stopdoc:
|
|
|
|
module IRB
|
|
|
|
module ExtendCommand
|
|
|
|
class Ls < Nop
|
|
|
|
def execute(*arg, grep: nil)
|
|
|
|
o = Output.new(grep: grep)
|
|
|
|
|
|
|
|
obj = arg.empty? ? irb_context.workspace.main : arg.first
|
|
|
|
locals = arg.empty? ? irb_context.workspace.binding.local_variables : []
|
|
|
|
klass = (obj.class == Class || obj.class == Module ? obj : obj.class)
|
|
|
|
|
|
|
|
o.dump("constants", obj.constants) if obj.respond_to?(:constants)
|
2021-06-19 22:36:40 -04:00
|
|
|
dump_methods(o, klass, obj)
|
2021-03-18 03:28:04 -04:00
|
|
|
o.dump("instance variables", obj.instance_variables)
|
|
|
|
o.dump("class variables", klass.class_variables)
|
|
|
|
o.dump("locals", locals)
|
|
|
|
end
|
|
|
|
|
2021-06-19 22:36:40 -04:00
|
|
|
def dump_methods(o, klass, obj)
|
2021-06-30 08:04:56 -04:00
|
|
|
singleton_class = begin obj.singleton_class; rescue TypeError; nil end
|
2021-06-29 06:16:58 -04:00
|
|
|
maps = class_method_map((singleton_class || klass).ancestors)
|
2021-05-02 04:50:46 -04:00
|
|
|
maps.each do |mod, methods|
|
2021-06-29 06:16:58 -04:00
|
|
|
name = mod == singleton_class ? "#{klass}.methods" : "#{mod}#methods"
|
2021-05-02 04:50:46 -04:00
|
|
|
o.dump(name, methods)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def class_method_map(classes)
|
2021-06-29 08:29:22 -04:00
|
|
|
dumped = Array.new
|
2021-05-02 04:50:46 -04:00
|
|
|
classes.reject { |mod| mod >= Object }.map do |mod|
|
2021-06-29 08:29:22 -04:00
|
|
|
methods = mod.public_instance_methods(false).select do |m|
|
|
|
|
dumped.push(m) unless dumped.include?(m)
|
|
|
|
end
|
2021-05-02 04:50:46 -04:00
|
|
|
[mod, methods]
|
|
|
|
end.reverse
|
|
|
|
end
|
|
|
|
|
2021-03-18 03:28:04 -04:00
|
|
|
class Output
|
2021-03-21 02:15:51 -04:00
|
|
|
MARGIN = " "
|
|
|
|
|
2021-03-18 03:28:04 -04:00
|
|
|
def initialize(grep: nil)
|
|
|
|
@grep = grep
|
2021-03-21 03:01:07 -04:00
|
|
|
@line_width = screen_width - MARGIN.length # right padding
|
2021-03-18 03:28:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def dump(name, strs)
|
|
|
|
strs = strs.grep(@grep) if @grep
|
|
|
|
strs = strs.sort
|
|
|
|
return if strs.empty?
|
|
|
|
|
2021-03-21 02:15:51 -04:00
|
|
|
# Attempt a single line
|
2021-03-18 03:28:04 -04:00
|
|
|
print "#{Color.colorize(name, [:BOLD, :BLUE])}: "
|
2021-03-21 02:15:51 -04:00
|
|
|
if fits_on_line?(strs, cols: strs.size, offset: "#{name}: ".length)
|
|
|
|
puts strs.join(MARGIN)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
puts
|
|
|
|
|
|
|
|
# Dump with the largest # of columns that fits on a line
|
|
|
|
cols = strs.size
|
|
|
|
until fits_on_line?(strs, cols: cols, offset: MARGIN.length) || cols == 1
|
|
|
|
cols -= 1
|
|
|
|
end
|
|
|
|
widths = col_widths(strs, cols: cols)
|
|
|
|
strs.each_slice(cols) do |ss|
|
|
|
|
puts ss.map.with_index { |s, i| "#{MARGIN}%-#{widths[i]}s" % s }.join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fits_on_line?(strs, cols:, offset: 0)
|
|
|
|
width = col_widths(strs, cols: cols).sum + MARGIN.length * (cols - 1)
|
|
|
|
width <= @line_width - offset
|
|
|
|
end
|
|
|
|
|
|
|
|
def col_widths(strs, cols:)
|
|
|
|
cols.times.map do |col|
|
2021-03-21 02:48:10 -04:00
|
|
|
(col...strs.size).step(cols).map do |i|
|
2021-03-21 02:15:51 -04:00
|
|
|
strs[i].length
|
|
|
|
end.max
|
2021-03-18 03:28:04 -04:00
|
|
|
end
|
|
|
|
end
|
2021-03-21 02:15:51 -04:00
|
|
|
|
|
|
|
def screen_width
|
|
|
|
Reline.get_screen_size.last
|
|
|
|
rescue Errno::EINVAL # in `winsize': Invalid argument - <STDIN>
|
2021-03-21 03:01:07 -04:00
|
|
|
80
|
2021-03-21 02:15:51 -04:00
|
|
|
end
|
2021-03-18 03:28:04 -04:00
|
|
|
end
|
|
|
|
private_constant :Output
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# :startdoc:
|