mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Refactor the way how ls
displays local variables
This commit is contained in:
parent
e36cdc678e
commit
0787e42ed0
1 changed files with 37 additions and 0 deletions
37
lib/pry/commands/ls/local_vars.rb
Normal file
37
lib/pry/commands/ls/local_vars.rb
Normal file
|
@ -0,0 +1,37 @@
|
|||
class Pry
|
||||
class Command::Ls < Pry::ClassCommand
|
||||
class LocalVars < Pry::Command::Ls::Formatter
|
||||
|
||||
def initialize(target, sticky_locals, opts)
|
||||
super(target)
|
||||
@sticky_locals = sticky_locals
|
||||
@default_switch = opts[:locals]
|
||||
end
|
||||
|
||||
def output_self
|
||||
name_value_pairs = @target.eval('local_variables').reject { |e|
|
||||
@sticky_locals.keys.include?(e.to_sym)
|
||||
}.map { |name|
|
||||
[name, (@target.eval(name.to_s))]
|
||||
}
|
||||
format(name_value_pairs).join('')
|
||||
end
|
||||
|
||||
def format(name_value_pairs)
|
||||
name_value_pairs.sort_by { |name, value|
|
||||
value.to_s.size
|
||||
}.reverse.map { |name, value|
|
||||
colorized_assignment_style(name, format_value(value))
|
||||
}
|
||||
end
|
||||
|
||||
def colorized_assignment_style(lhs, rhs, desired_width = 7)
|
||||
colorized_lhs = color(:local_var, lhs)
|
||||
color_escape_padding = colorized_lhs.size - lhs.size
|
||||
pad = desired_width + color_escape_padding
|
||||
"%-#{pad}s = %s" % [color(:local_var, colorized_lhs), rhs]
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue