Class: Pry::Command::Cat::InputExpressionFormatter

Inherits:
AbstractFormatter show all
Defined in:
lib/pry/commands/cat/input_expression_formatter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractFormatter

#between_lines, #code_type, #decorate, #use_line_numbers?

Methods included from Helpers::BaseHelpers

#colorize_code, #find_command, #heading, #highlight, #not_a_real_file?, #safe_send, #silence_warnings, #stagger_output, #use_ansi_codes?

Methods included from Helpers::CommandHelpers

#absolute_index_number, #absolute_index_range, #get_method_or_raise, #internal_binding?, #one_index_number, #one_index_range, #one_index_range_or_number, #restrict_to_lines, #set_file_and_dir_locals, #temp_file, #unindent

Methods included from Helpers::OptionsHelpers

method_object, #method_object, method_options, #method_options

Constructor Details

#initialize(input_expressions, opts) ⇒ InputExpressionFormatter

Returns a new instance of InputExpressionFormatter.


10
11
12
13
# File 'lib/pry/commands/cat/input_expression_formatter.rb', line 10

def initialize(input_expressions, opts)
  @input_expressions = input_expressions
  @opts = opts
end

Instance Attribute Details

#input_expressionsObject

Returns the value of attribute input_expressions


7
8
9
# File 'lib/pry/commands/cat/input_expression_formatter.rb', line 7

def input_expressions
  @input_expressions
end

#optsObject

Returns the value of attribute opts


8
9
10
# File 'lib/pry/commands/cat/input_expression_formatter.rb', line 8

def opts
  @opts
end

Instance Method Details

#formatObject

Raises:


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pry/commands/cat/input_expression_formatter.rb', line 15

def format
  raise CommandError, "No input expressions!" if numbered_input_items.empty?

  if numbered_input_items.length > 1
    content = ''
    numbered_input_items.each do |i, s|
      content += "#{Helpers::Text.bold(i.to_s)}:\n"
      content += decorate(Pry::Code(s).with_indentation(2)).to_s
    end

    content
  else
    decorate(Pry::Code(selected_input_items.first))
  end
end

#normalized_expression_rangeObject (private)


42
43
44
# File 'lib/pry/commands/cat/input_expression_formatter.rb', line 42

def normalized_expression_range
  absolute_index_range(opts[:i], input_expressions.count)
end

#numbered_input_itemsObject (private)


37
38
39
40
# File 'lib/pry/commands/cat/input_expression_formatter.rb', line 37

def numbered_input_items
  @numbered_input_items ||= normalized_expression_range.zip(selected_input_items)
    .reject { |_, s| s.nil? || s == "" }
end

#selected_input_itemsObject (private)


33
34
35
# File 'lib/pry/commands/cat/input_expression_formatter.rb', line 33

def selected_input_items
  input_expressions[normalized_expression_range] || []
end