2013-12-01 18:47:50 +02:00
|
|
|
require 'pry/commands/ls/ls_entity'
|
2011-05-01 22:25:37 +12:00
|
|
|
class Pry
|
2019-02-27 02:22:32 +02:00
|
|
|
class Command
|
|
|
|
class Ls < Pry::ClassCommand
|
|
|
|
DEFAULT_OPTIONS = {
|
|
|
|
heading_color: :bright_blue,
|
|
|
|
public_method_color: :default,
|
|
|
|
private_method_color: :blue,
|
|
|
|
protected_method_color: :blue,
|
|
|
|
method_missing_color: :bright_red,
|
|
|
|
local_var_color: :yellow,
|
|
|
|
pry_var_color: :default, # e.g. _, _pry_, _file_
|
|
|
|
instance_var_color: :blue, # e.g. @foo
|
|
|
|
class_var_color: :bright_blue, # e.g. @@foo
|
|
|
|
global_var_color: :default, # e.g. $CODERAY_DEBUG, $eventmachine_library
|
|
|
|
builtin_global_color: :cyan, # e.g. $stdin, $-w, $PID
|
|
|
|
pseudo_global_color: :cyan, # e.g. $~, $1..$9, $LAST_MATCH_INFO
|
|
|
|
constant_color: :default, # e.g. VERSION, ARGF
|
|
|
|
class_constant_color: :blue, # e.g. Object, Kernel
|
|
|
|
exception_constant_color: :magenta, # e.g. Exception, RuntimeError
|
|
|
|
unloaded_constant_color: :yellow, # Any constant that is still in .autoload? state
|
|
|
|
separator: " ",
|
|
|
|
ceiling: [Object, Module, Class]
|
2019-03-02 11:59:33 +02:00
|
|
|
}.freeze
|
2014-03-25 17:54:14 +01:00
|
|
|
|
2019-02-27 02:22:32 +02:00
|
|
|
match 'ls'
|
|
|
|
group 'Context'
|
|
|
|
description 'Show the list of vars and methods in the current scope.'
|
|
|
|
command_options shellwords: false, interpolate: false
|
2011-05-01 22:25:37 +12:00
|
|
|
|
2019-02-27 02:22:32 +02:00
|
|
|
banner <<-'BANNER'
|
|
|
|
Usage: ls [-m|-M|-p|-pM] [-q|-v] [-c|-i] [Object]
|
|
|
|
ls [-g] [-l]
|
2013-02-16 18:09:09 +02:00
|
|
|
|
2019-02-27 02:22:32 +02:00
|
|
|
ls shows you which methods, constants and variables are accessible to Pry. By
|
|
|
|
default it shows you the local variables defined in the current shell, and any
|
|
|
|
public methods or instance variables defined on the current object.
|
2012-02-23 16:28:00 +13:00
|
|
|
|
2019-02-27 02:22:32 +02:00
|
|
|
The colours used are configurable using Pry.config.ls.*_color, and the separator
|
|
|
|
is Pry.config.ls.separator.
|
2011-12-31 00:55:38 +00:00
|
|
|
|
2019-02-27 02:22:32 +02:00
|
|
|
Pry.config.ls.ceiling is used to hide methods defined higher up in the
|
|
|
|
inheritance chain, this is by default set to [Object, Module, Class] so that
|
|
|
|
methods defined on all Objects are omitted. The -v flag can be used to ignore
|
|
|
|
this setting and show all methods, while the -q can be used to set the ceiling
|
|
|
|
much lower and show only methods defined on the object or its direct class.
|
2013-03-15 12:05:50 +02:00
|
|
|
|
2019-02-27 02:22:32 +02:00
|
|
|
Also check out `find-method` command (run `help find-method`).
|
|
|
|
BANNER
|
2011-12-31 00:55:38 +00:00
|
|
|
|
2019-02-27 02:22:32 +02:00
|
|
|
def options(opt)
|
|
|
|
opt.on :m, :methods, "Show public methods defined on the Object"
|
|
|
|
opt.on :M, "instance-methods", "Show public methods defined in a Module or Class"
|
|
|
|
opt.on :p, :ppp, "Show public, protected (in yellow) and private (in green) methods"
|
|
|
|
opt.on :q, :quiet, "Show only methods defined on object.singleton_class and object.class"
|
|
|
|
opt.on :v, :verbose, "Show methods and constants on all super-classes (ignores Pry.config.ls.ceiling)"
|
|
|
|
opt.on :g, :globals, "Show global variables, including those builtin to Ruby (in cyan)"
|
|
|
|
opt.on :l, :locals, "Show hash of local vars, sorted by descending size"
|
|
|
|
opt.on :c, :constants, "Show constants, highlighting classes (in blue), and exceptions (in purple).\n" \
|
|
|
|
"#{' ' * 32}Constants that are pending autoload? are also shown (in yellow)"
|
|
|
|
opt.on :i, :ivars, "Show instance variables (in blue) and class variables (in bright blue)"
|
|
|
|
opt.on :G, :grep, "Filter output by regular expression", argument: true
|
2019-03-02 02:03:35 +02:00
|
|
|
opt.on :d, :dconstants, "Show deprecated constants" if Object.respond_to?(:deprecate_constant)
|
|
|
|
opt.on :J, "all-java", "Show all the aliases for methods from java (default is to show only prettiest)" if Helpers::Platform.jruby?
|
2017-06-04 23:31:14 +01:00
|
|
|
end
|
2011-12-31 00:55:38 +00:00
|
|
|
|
2019-02-27 02:22:32 +02:00
|
|
|
# Exclude -q, -v and --grep because they,
|
|
|
|
# don't specify what the user wants to see.
|
|
|
|
def no_user_opts?
|
|
|
|
!(opts[:methods] || opts['instance-methods'] || opts[:ppp] ||
|
|
|
|
opts[:globals] || opts[:locals] || opts[:constants] || opts[:ivars])
|
|
|
|
end
|
2013-12-01 18:47:50 +02:00
|
|
|
|
2019-02-27 02:22:32 +02:00
|
|
|
def process
|
|
|
|
@interrogatee = args.empty? ? target_self : target.eval(args.join(' '))
|
|
|
|
raise_errors_if_arguments_are_weird
|
|
|
|
ls_entity = LsEntity.new(
|
|
|
|
interrogatee: @interrogatee,
|
|
|
|
no_user_opts: no_user_opts?,
|
|
|
|
opts: opts,
|
|
|
|
args: args,
|
|
|
|
_pry_: _pry_
|
|
|
|
)
|
2012-04-01 19:25:11 -07:00
|
|
|
|
2019-02-27 02:22:32 +02:00
|
|
|
_pry_.pager.page ls_entity.entities_table
|
|
|
|
end
|
2012-04-01 19:25:11 -07:00
|
|
|
|
2019-02-27 02:22:32 +02:00
|
|
|
private
|
2011-10-08 00:53:27 -07:00
|
|
|
|
2019-02-27 02:22:32 +02:00
|
|
|
def error_list
|
|
|
|
any_args = args.any?
|
|
|
|
non_mod_interrogatee = !(Module === @interrogatee)
|
|
|
|
[
|
|
|
|
['-l does not make sense with a specified Object', :locals, any_args],
|
|
|
|
['-g does not make sense with a specified Object', :globals, any_args],
|
|
|
|
['-q does not make sense with -v', :quiet, opts.present?(:verbose)],
|
|
|
|
['-M only makes sense with a Module or a Class', 'instance-methods', non_mod_interrogatee],
|
|
|
|
['-c only makes sense with a Module or a Class', :constants, any_args && non_mod_interrogatee]
|
|
|
|
]
|
|
|
|
end
|
2013-12-01 16:34:08 +02:00
|
|
|
|
2019-02-27 02:22:32 +02:00
|
|
|
def raise_errors_if_arguments_are_weird
|
|
|
|
error_list.each do |message, option, invalid_expr|
|
|
|
|
raise Pry::CommandError, message if opts.present?(option) && invalid_expr
|
|
|
|
end
|
2013-01-12 23:45:10 +00:00
|
|
|
end
|
2013-01-12 15:41:44 +00:00
|
|
|
end
|
2012-12-25 23:35:17 +02:00
|
|
|
|
2019-02-27 02:22:32 +02:00
|
|
|
Pry::Commands.add_command(Pry::Command::Ls)
|
|
|
|
end
|
2011-05-19 16:53:44 +01:00
|
|
|
end
|