1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00
pry--pry/lib/pry/commands/ls/globals.rb
Kyrylo Silin f67830d9a5 Refactor the way how ls displays global variables
This is just a facility, "ls.rb" still uses the old code for the time
being. I'm going to assemble everything when I prepare all the code.
2013-11-30 14:26:28 +02:00

46 lines
1.7 KiB
Ruby

class Pry
class Command::Ls < Pry::ClassCommand
class Globals < Pry::Command::Ls::Formatter
# 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)
attr_accessor :grep
def initialize(target, globals_switch = false)
super(target)
@switch = globals_switch
end
def output_self
output_section('global variables', grep.regexp[format(@target.eval('global_variables'))])
end
def format(globals)
globals.sort_by(&:downcase).map do |name|
if PSEUDO_GLOBALS.include?(name)
color(:pseudo_global, name)
elsif BUILTIN_GLOBALS.include?(name)
color(:builtin_global, name)
else
color(:global_var, name)
end
end
end
end
end
end