Class: Pry::Command::Ls::LocalVars

Inherits:
Formatter show all
Defined in:
lib/pry/commands/ls/local_vars.rb

Instance Attribute Summary

Attributes inherited from Formatter

#grep, #pry_instance

Instance Method Summary collapse

Methods inherited from Formatter

#color, #correct_opts?, #format_value, #output_section, #write_out

Constructor Details

#initialize(opts, pry_instance) ⇒ LocalVars

Returns a new instance of LocalVars.


7
8
9
10
11
# File 'lib/pry/commands/ls/local_vars.rb', line 7

def initialize(opts, pry_instance)
  super(pry_instance)
  @default_switch = opts[:locals]
  @sticky_locals = pry_instance.sticky_locals
end

Instance Method Details

#colorized_assignment_style(lhs, rhs, desired_width = 7) ⇒ Object (private)


34
35
36
37
38
39
40
41
42
43
# File 'lib/pry/commands/ls/local_vars.rb', line 34

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
  Kernel.format(
    "%-#{pad}<name>s = %<value>s",
    name: color(:local_var, colorized_lhs),
    value: rhs
  )
end

#format(name_value_pairs) ⇒ Object (private)


25
26
27
28
29
30
31
32
# File 'lib/pry/commands/ls/local_vars.rb', line 25

def format(name_value_pairs)
  sorted = name_value_pairs.sort_by do |_name, value|
    value.to_s.size
  end
  sorted.reverse.map do |name, value|
    colorized_assignment_style(name, format_value(value))
  end
end

#output_selfObject


13
14
15
16
17
18
19
20
21
# File 'lib/pry/commands/ls/local_vars.rb', line 13

def output_self
  locals = @target.eval('local_variables').reject do |e|
    @sticky_locals.key?(e.to_sym)
  end
  name_value_pairs = locals.map do |name|
    [name, @target.eval(name.to_s)]
  end
  format(name_value_pairs).join('')
end