summaryrefslogtreecommitdiff
path: root/lib/irb
diff options
context:
space:
mode:
authorTakashi Kokubun <takashikkbn@gmail.com>2021-03-18 00:28:04 -0700
committergit <svn-admin@ruby-lang.org>2021-03-21 12:59:51 +0900
commit5f72962a0955d62dfbac2f7553b725b9d1e9e60d (patch)
treed73f865d0ec71e2038463e4e6d19e814f8789f22 /lib/irb
parentd36ac283d188ba6d923c905a85341761fa1305c3 (diff)
[ruby/irb] Implement ls command
https://github.com/ruby/irb/commit/19b6c20604
Diffstat (limited to 'lib/irb')
-rw-r--r--lib/irb/cmd/ls.rb48
-rw-r--r--lib/irb/cmd/nop.rb14
-rw-r--r--lib/irb/extend-command.rb12
3 files changed, 67 insertions, 7 deletions
diff --git a/lib/irb/cmd/ls.rb b/lib/irb/cmd/ls.rb
new file mode 100644
index 0000000000..c39937d4db
--- /dev/null
+++ b/lib/irb/cmd/ls.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+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)
+ o.dump("#{klass}.methods", obj.singleton_methods(false))
+ o.dump("#{klass}#methods", klass.public_instance_methods(false))
+ o.dump("instance variables", obj.instance_variables)
+ o.dump("class variables", klass.class_variables)
+ o.dump("locals", locals)
+ end
+
+ class Output
+ def initialize(grep: nil)
+ @grep = grep
+ end
+
+ def dump(name, strs)
+ strs = strs.grep(@grep) if @grep
+ strs = strs.sort
+ return if strs.empty?
+
+ print "#{Color.colorize(name, [:BOLD, :BLUE])}: "
+ if strs.size > 7
+ len = [strs.map(&:length).max, 16].min
+ puts; strs.each_slice(7) { |ss| puts " #{ss.map { |s| "%-#{len}s" % s }.join(" ")}" }
+ else
+ puts strs.join(" ")
+ end
+ end
+ end
+ private_constant :Output
+ end
+ end
+end
+# :startdoc:
diff --git a/lib/irb/cmd/nop.rb b/lib/irb/cmd/nop.rb
index fa3c011b5f..d6f7a611a6 100644
--- a/lib/irb/cmd/nop.rb
+++ b/lib/irb/cmd/nop.rb
@@ -14,10 +14,16 @@ module IRB
module ExtendCommand
class Nop
-
- def self.execute(conf, *opts, &block)
- command = new(conf)
- command.execute(*opts, &block)
+ if RUBY_ENGINE == "ruby" && RUBY_VERSION >= "2.7.0"
+ def self.execute(conf, *opts, **kwargs, &block)
+ command = new(conf)
+ command.execute(*opts, **kwargs, &block)
+ end
+ else
+ def self.execute(conf, *opts, &block)
+ command = new(conf)
+ command.execute(*opts, &block)
+ end
end
def initialize(conf)
diff --git a/lib/irb/extend-command.rb b/lib/irb/extend-command.rb
index af076a6154..e7b046ad4e 100644
--- a/lib/irb/extend-command.rb
+++ b/lib/irb/extend-command.rb
@@ -126,6 +126,11 @@ module IRB # :nodoc:
],
[
+ :irb_ls, :Ls, "irb/cmd/ls",
+ [:ls, NO_OVERRIDE],
+ ],
+
+ [
:irb_measure, :Measure, "irb/cmd/measure",
[:measure, NO_OVERRIDE],
],
@@ -169,12 +174,13 @@ module IRB # :nodoc:
end
if load_file
+ kwargs = ", **kwargs" if RUBY_ENGINE == "ruby" && RUBY_VERSION >= "2.7.0"
line = __LINE__; eval %[
- def #{cmd_name}(*opts, &b)
+ def #{cmd_name}(*opts#{kwargs}, &b)
require "#{load_file}"
arity = ExtendCommand::#{cmd_class}.instance_method(:execute).arity
args = (1..(arity < 0 ? ~arity : arity)).map {|i| "arg" + i.to_s }
- args << "*opts" if arity < 0
+ args << "*opts#{kwargs}" if arity < 0
args << "&block"
args = args.join(", ")
line = __LINE__; eval %[
@@ -185,7 +191,7 @@ module IRB # :nodoc:
end
end
], nil, __FILE__, line
- __send__ :#{cmd_name}_, *opts, &b
+ __send__ :#{cmd_name}_, *opts#{kwargs}, &b
end
], nil, __FILE__, line
else