From e36cdc678e798f0ed104a9cde1d7981e94eac945 Mon Sep 17 00:00:00 2001 From: Kyrylo Silin Date: Sun, 1 Dec 2013 02:15:35 +0200 Subject: [PATCH] Refactor the way how `ls` displays local names --- lib/pry/commands/ls/local_names.rb | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 lib/pry/commands/ls/local_names.rb diff --git a/lib/pry/commands/ls/local_names.rb b/lib/pry/commands/ls/local_names.rb new file mode 100644 index 00000000..5cdf8702 --- /dev/null +++ b/lib/pry/commands/ls/local_names.rb @@ -0,0 +1,33 @@ +class Pry + class Command::Ls < Pry::ClassCommand + class LocalNames < Pry::Command::Ls::Formatter + + def initialize(target, has_any_opts, sticky_locals, args) + super(target) + @has_any_opts = has_any_opts + @sticky_locals = sticky_locals + @args = args + end + + def correct_opts? + super || !@has_any_opts && @args.empty? + end + + def output_self + local_vars = grep.regexp[@target.eval('local_variables')] + output_section('locals', format(local_vars)) + end + + def format(locals) + locals.sort_by(&:downcase).map do |name| + if @sticky_locals.include?(name.to_sym) + color(:pry_var, name) + else + color(:local_var, name) + end + end + end + + end + end +end