mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Differentiate output more
This commit is contained in:
parent
119d4a32e6
commit
0216c9b93c
2 changed files with 63 additions and 15 deletions
|
@ -1,5 +1,6 @@
|
|||
# (C) John Mair (banisterfiend) 2011
|
||||
# MIT License
|
||||
#
|
||||
|
||||
require 'pp'
|
||||
require 'pry/helpers/base_helpers'
|
||||
|
|
|
@ -4,6 +4,21 @@ class Pry
|
|||
Ls = Pry::CommandSet.new do
|
||||
|
||||
helpers do
|
||||
|
||||
# http://ruby.runpaint.org/globals, and running "puts global_variables.inspect".
|
||||
BUILTIN_GLOBALS = %w($" $$ $* $, $-0 $-F $-I $-K $-W $-a $-d $-i $-l $-p $-v $-w $. $/ $\\
|
||||
$: $; $< $= $> $0 $ARGV $CONSOLE $DEBUG $DEFAULT_INPUT $DEFAULT_OUTPUT
|
||||
$FIELD_SEPARATOR $FILENAME $FS $IGNORECASE $INPUT_LINE_NUMBER
|
||||
$INPUT_RECORD_SEPARATOR $KCODE $LOADED_FEATURES $LOAD_PATH $NR $OFS
|
||||
$ORS $OUTPUT_FIELD_SEPARATOR $OUTPUT_RECORD_SEPARATOR $PID $PROCESS_ID
|
||||
$PROGRAM_NAME $RS $VERBOSE $deferr $defout $stderr $stdin $stdout)
|
||||
|
||||
# $SAFE and $? are thread-local, the exception stuff only works in a rescue clause,
|
||||
# everything else is basically a local variable with a $ in its name.
|
||||
PSEUDO_GLOBALS = %w($! $' $& $` $@ $? $+ $_ $~ $1 $2 $3 $4 $5 $6 $7 $8 $9
|
||||
$CHILD_STATUS $SAFE $ERROR_INFO $ERROR_POSITION $LAST_MATCH_INFO
|
||||
$LAST_PAREN_MATCH $LAST_READ_LINE $MATCH $POSTMATCH $PREMATCH)
|
||||
|
||||
# Get all the methods that we'll want to output
|
||||
def all_methods(obj, opts)
|
||||
opts.M? ? Pry::Method.all_from_class(obj) : Pry::Method.all_from_obj(obj)
|
||||
|
@ -56,9 +71,9 @@ class Pry
|
|||
if method.name == 'method_missing'
|
||||
text.red('method_missing') # This should stand out!
|
||||
elsif method.visibility == :private
|
||||
text.blue(method.name) # TODO: make colours configurable
|
||||
text.green(method.name) # TODO: make colours configurable
|
||||
elsif method.visibility == :protected
|
||||
text.purple(method.name)
|
||||
text.yellow(method.name)
|
||||
else
|
||||
method.name
|
||||
end
|
||||
|
@ -84,21 +99,53 @@ class Pry
|
|||
end.compact.join(" ")
|
||||
end
|
||||
|
||||
def format_globals(globals, quiet)
|
||||
globals.sort_by(&:downcase).map do |name|
|
||||
if PSEUDO_GLOBALS.include?(name)
|
||||
text.cyan(name) unless quiet
|
||||
elsif BUILTIN_GLOBALS.include?(name)
|
||||
text.cyan(name) unless quiet
|
||||
else
|
||||
name
|
||||
end
|
||||
end.compact.join(" ")
|
||||
end
|
||||
|
||||
def format_locals(locals)
|
||||
locals.sort_by(&:downcase).map do |name|
|
||||
if _pry_.special_locals.include?(name.to_sym)
|
||||
text.red(name)
|
||||
else
|
||||
name
|
||||
end
|
||||
end.join(" ")
|
||||
end
|
||||
|
||||
# Add a new section to the output. Outputs nothing if the section would be empty.
|
||||
def output_section(heading, body)
|
||||
output.puts "#{text.bold(heading)}: #{body}" if body.strip != ""
|
||||
output.puts "#{text.bold(text.grey(heading))}: #{body}" if body.strip != ""
|
||||
end
|
||||
|
||||
def ls_color_map
|
||||
{
|
||||
"local variables" => Pry.config.ls.local_var_color,
|
||||
"instance variables" => Pry.config.ls.instance_var_color,
|
||||
"class variables" => Pry.config.ls.class_var_color,
|
||||
"global variables" => Pry.config.ls.global_var_color,
|
||||
"public methods" => Pry.config.ls.method_color,
|
||||
"private methods" => Pry.config.ls.method_color,
|
||||
"protected methods" => Pry.config.ls.method_color,
|
||||
"constants" => Pry.config.ls.constant_color
|
||||
"local variables" => Pry.config.ls.local_var_color, #black
|
||||
"pry variables" => Pry.config.pry_var_color, #red
|
||||
|
||||
"instance variables" => Pry.config.ls.instance_var_color, #blue
|
||||
"class variables" => Pry.config.ls.class_var_color, #bright_blue
|
||||
|
||||
"global variables" => Pry.config.ls.global_var_color, #black
|
||||
"pseudo-global variables" => Pry.config.ls.pseudo_global_var_color, #cyan
|
||||
"builtin global variables" => Pry.config.ls.builtin_global_var_color, #cyan
|
||||
|
||||
"public methods" => Pry.config.ls.public_color, #black
|
||||
"private methods" => Pry.config.ls.private_color, #green
|
||||
"protected methods" => Pry.config.ls.protected_color, #yellow
|
||||
"method_missing" => Pry.config.ls.method_missing_color, #red
|
||||
|
||||
"class constants" => Pry.config.ls.constant_color, #blue
|
||||
"exception constants" => Pry.config.ls.class_color, #magenta
|
||||
"other constants" => Pry.config.ls.exception_color #black
|
||||
}
|
||||
end
|
||||
end
|
||||
|
@ -147,7 +194,7 @@ class Pry
|
|||
|
||||
obj = args.empty? ? target_self : target.eval(args.join(" "))
|
||||
show_methods = opts.m? || opts.M? || opts.p? || !has_opts
|
||||
show_constants = opts.c? || (!has_opts && Module === obj || TOPLEVEL_BINDING.eval('self') == obj)
|
||||
show_constants = opts.c? || (!has_opts && (Module === obj || TOPLEVEL_BINDING.eval('self') == obj))
|
||||
show_ivars = opts.i? || !has_opts
|
||||
show_locals = opts.l? || (!has_opts && args.empty?)
|
||||
|
||||
|
@ -158,14 +205,14 @@ class Pry
|
|||
raise Pry::CommandError, "-c only makes sense with a Module or a Class" if opts.c? && !args.empty? && !(Module === obj)
|
||||
|
||||
if opts.g?
|
||||
output_variables("global variables", target.eval("global_variables"))
|
||||
output_section("global variables", format_globals(target.eval("global_variables"), opts.q?))
|
||||
end
|
||||
|
||||
if show_constants
|
||||
mod = Module === obj ? obj : Object
|
||||
constants = mod.constants
|
||||
constants -= (mod.ancestors - [mod]).map(&:constants).flatten unless opts.v?
|
||||
output_section("Constants", format_constants(mod, constants))
|
||||
output_section("constants", format_constants(mod, constants))
|
||||
end
|
||||
|
||||
if show_methods
|
||||
|
@ -184,7 +231,7 @@ class Pry
|
|||
end
|
||||
|
||||
if show_locals
|
||||
output_variables("local variables", target.eval("local_variables"))
|
||||
output_section("locals", format_locals(target.eval("local_variables")))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue