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, #command_dependencies_met?, #find_command, #heading, #highlight, #not_a_real_file?, #safe_send, #silence_warnings, #stagger_output, #use_ansi_codes?

Methods included from Platform

#jruby?, #jruby_19?, #known_engines, #linux?, #mac_osx?, #mri?, #mri_19?, #mri_2?, #rbx?, #windows?, #windows_ansi?

Methods included from Helpers::CommandHelpers

#absolute_index_number, absolute_index_number, #absolute_index_range, absolute_index_range, #command_error, command_error, #get_method_or_raise, get_method_or_raise, #internal_binding?, internal_binding?, #one_index_number, one_index_number, #one_index_range, one_index_range, #one_index_range_or_number, one_index_range_or_number, #restrict_to_lines, restrict_to_lines, #set_file_and_dir_locals, set_file_and_dir_locals, #temp_file, temp_file, #unindent, 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



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

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

Instance Attribute Details

#input_expressionsObject

Returns the value of attribute input_expressions



4
5
6
# File 'lib/pry/commands/cat/input_expression_formatter.rb', line 4

def input_expressions
  @input_expressions
end

#optsObject

Returns the value of attribute opts



5
6
7
# File 'lib/pry/commands/cat/input_expression_formatter.rb', line 5

def opts
  @opts
end

Instance Method Details

#formatObject

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pry/commands/cat/input_expression_formatter.rb', line 12

def format
  raise CommandError, "No input expressions!" if numbered_input_items.length < 1

  if numbered_input_items.length > 1
    content = ""
    numbered_input_items.each do |i, s|
      content << "#{Helpers::Text.bold(i.to_s)}:\n" << 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)



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

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

#numbered_input_itemsObject (private)



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

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

#selected_input_itemsObject (private)



29
30
31
# File 'lib/pry/commands/cat/input_expression_formatter.rb', line 29

def selected_input_items
  input_expressions[normalized_expression_range] || []
end