Module: Pry::Helpers::CommandHelpers

Includes:
OptionsHelpers
Included in:
CodeObject, Command, Command::Cat::AbstractFormatter, Command::CodeCollector, Editor
Defined in:
lib/pry/helpers/command_helpers.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OptionsHelpers

method_object, #method_object, method_options, #method_options

Class Method Details

.absolute_index_number(line_number, array_length) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/pry/helpers/command_helpers.rb', line 131

def absolute_index_number(line_number, array_length)
  if line_number >= 0
    line_number
  else
    [array_length + line_number, 0].max
  end
end

.absolute_index_range(range_or_number, array_length) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/pry/helpers/command_helpers.rb', line 139

def absolute_index_range(range_or_number, array_length)
  case range_or_number
  when Range
    a = absolute_index_number(range_or_number.begin, array_length)
    b = absolute_index_number(range_or_number.end, array_length)
  else
    a = b = absolute_index_number(range_or_number, array_length)
  end

  Range.new(a, b)
end

.command_error(message, omit_help, klass = CommandError) ⇒ Object

Raises:

  • (klass)


55
56
57
58
# File 'lib/pry/helpers/command_helpers.rb', line 55

def command_error(message, omit_help, klass=CommandError)
  message += " Type `#{command_name} --help` for help." unless omit_help
  raise klass, message
end

.get_method_or_raise(name, target, opts = {}, omit_help = false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pry/helpers/command_helpers.rb', line 32

def get_method_or_raise(name, target, opts={}, omit_help=false)
  meth = Pry::Method.from_str(name, target, opts)

  if name && !meth
    command_error("The method '#{name}' could not be found.", omit_help, MethodNotFound)
  end

  (opts[:super] || 0).times do
    if meth.super
      meth = meth.super
    else
      command_error("'#{meth.name_with_owner}' has no super method.", omit_help, MethodNotFound)
    end
  end

  if !meth || (!name && internal_binding?(target))
    command_error("No method name given, and context is not a method.", omit_help, MethodNotFound)
  end

  set_file_and_dir_locals(meth.source_file)
  meth
end

.internal_binding?(target) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/pry/helpers/command_helpers.rb', line 26

def internal_binding?(target)
  m = target.eval("::Kernel.__method__").to_s
  # class_eval is here because of http://jira.codehaus.org/browse/JRUBY-6753
  ["__binding__", "__pry__", "class_eval"].include?(m)
end

.one_index_number(line_number) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/pry/helpers/command_helpers.rb', line 109

def one_index_number(line_number)
  if line_number > 0
    line_number - 1
  else
    line_number
  end
end

.one_index_range(range) ⇒ Object

convert a 1-index range to a 0-indexed one



118
119
120
# File 'lib/pry/helpers/command_helpers.rb', line 118

def one_index_range(range)
  Range.new(one_index_number(range.begin), one_index_number(range.end))
end

.one_index_range_or_number(range_or_number) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/pry/helpers/command_helpers.rb', line 122

def one_index_range_or_number(range_or_number)
  case range_or_number
  when Range
    one_index_range(range_or_number)
  else
    one_index_number(range_or_number)
  end
end

.render_output(str, opts = {}) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/pry/helpers/command_helpers.rb', line 18

def render_output(str, opts={})
  if opts[:flood]
    output.puts str
  else
    stagger_output str
  end
end

.restrict_to_lines(content, lines) ⇒ String

Restrict a string to the given range of lines (1-indexed)

Parameters:

  • content (String)

    The string.

  • lines (Range, Fixnum)

    The line(s) to restrict it to.

Returns:

  • (String)

    The resulting string.



104
105
106
107
# File 'lib/pry/helpers/command_helpers.rb', line 104

def restrict_to_lines(content, lines)
  line_range = one_index_range_or_number(lines)
  Array(content.lines.to_a[line_range]).join
end

.temp_file(ext = '.rb') ⇒ String

Open a temp file and yield it to the block, closing it after

Returns:

  • (String)

    The path of the temp file



11
12
13
14
15
16
# File 'lib/pry/helpers/command_helpers.rb', line 11

def temp_file(ext='.rb')
  file = Tempfile.new(['pry', ext])
  yield file
ensure
  file.close(true) if file
end

.unindent(text, left_padding = 0) ⇒ String

Remove any common leading whitespace from every line in text.

This can be used to make a HEREDOC line up with the left margin, without sacrificing the indentation level of the source code.

e.g. opt.banner unindent <<-USAGE Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "Ut enim ad minim veniam." USAGE

Heavily based on textwrap.dedent from Python, which is: Copyright (C) 1999-2001 Gregory P. Ward. Copyright (C) 2002, 2003 Python Software Foundation. Written by Greg Ward gward@python.net

Licensed under http://docs.python.org/license.html From http://hg.python.org/cpython/file/6b9f0a6efaeb/Lib/textwrap.py

Parameters:

  • text (String)

    The text from which to remove indentation

Returns:

  • (String)

    The text with indentation stripped.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pry/helpers/command_helpers.rb', line 82

def unindent(text, left_padding = 0)
  # Empty blank lines
  text = text.sub(/^[ \t]+$/, '')

  # Find the longest common whitespace to all indented lines
  margin = text.scan(/^[ \t]*(?=[^ \t\n])/).inject do |current_margin, next_indent|
    if next_indent.start_with?(current_margin)
      current_margin
    elsif current_margin.start_with?(next_indent)
      next_indent
    else
      ""
    end
  end

  text.gsub(/^#{margin}/, ' ' * left_padding)
end

Instance Method Details

#absolute_index_number(line_number, array_length) ⇒ Object (private)



131
132
133
134
135
136
137
# File 'lib/pry/helpers/command_helpers.rb', line 131

def absolute_index_number(line_number, array_length)
  if line_number >= 0
    line_number
  else
    [array_length + line_number, 0].max
  end
end

#absolute_index_range(range_or_number, array_length) ⇒ Object (private)



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/pry/helpers/command_helpers.rb', line 139

def absolute_index_range(range_or_number, array_length)
  case range_or_number
  when Range
    a = absolute_index_number(range_or_number.begin, array_length)
    b = absolute_index_number(range_or_number.end, array_length)
  else
    a = b = absolute_index_number(range_or_number, array_length)
  end

  Range.new(a, b)
end

#command_error(message, omit_help, klass = CommandError) ⇒ Object (private)



55
56
57
58
# File 'lib/pry/helpers/command_helpers.rb', line 55

def command_error(message, omit_help, klass=CommandError)
  message += " Type `#{command_name} --help` for help." unless omit_help
  raise klass, message
end

#get_method_or_raise(name, target, opts = {}, omit_help = false) ⇒ Object (private)



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pry/helpers/command_helpers.rb', line 32

def get_method_or_raise(name, target, opts={}, omit_help=false)
  meth = Pry::Method.from_str(name, target, opts)

  if name && !meth
    command_error("The method '#{name}' could not be found.", omit_help, MethodNotFound)
  end

  (opts[:super] || 0).times do
    if meth.super
      meth = meth.super
    else
      command_error("'#{meth.name_with_owner}' has no super method.", omit_help, MethodNotFound)
    end
  end

  if !meth || (!name && internal_binding?(target))
    command_error("No method name given, and context is not a method.", omit_help, MethodNotFound)
  end

  set_file_and_dir_locals(meth.source_file)
  meth
end

#internal_binding?(target) ⇒ Object (private)



26
27
28
29
30
# File 'lib/pry/helpers/command_helpers.rb', line 26

def internal_binding?(target)
  m = target.eval("::Kernel.__method__").to_s
  # class_eval is here because of http://jira.codehaus.org/browse/JRUBY-6753
  ["__binding__", "__pry__", "class_eval"].include?(m)
end

#one_index_number(line_number) ⇒ Object (private)



109
110
111
112
113
114
115
# File 'lib/pry/helpers/command_helpers.rb', line 109

def one_index_number(line_number)
  if line_number > 0
    line_number - 1
  else
    line_number
  end
end

#one_index_range(range) ⇒ Object (private)

convert a 1-index range to a 0-indexed one



118
119
120
# File 'lib/pry/helpers/command_helpers.rb', line 118

def one_index_range(range)
  Range.new(one_index_number(range.begin), one_index_number(range.end))
end

#one_index_range_or_number(range_or_number) ⇒ Object (private)



122
123
124
125
126
127
128
129
# File 'lib/pry/helpers/command_helpers.rb', line 122

def one_index_range_or_number(range_or_number)
  case range_or_number
  when Range
    one_index_range(range_or_number)
  else
    one_index_number(range_or_number)
  end
end

#render_output(str, opts = {}) ⇒ Object (private)



18
19
20
21
22
23
24
# File 'lib/pry/helpers/command_helpers.rb', line 18

def render_output(str, opts={})
  if opts[:flood]
    output.puts str
  else
    stagger_output str
  end
end

#restrict_to_lines(content, lines) ⇒ String (private)

Restrict a string to the given range of lines (1-indexed)

Parameters:

  • content (String)

    The string.

  • lines (Range, Fixnum)

    The line(s) to restrict it to.

Returns:

  • (String)

    The resulting string.



104
105
106
107
# File 'lib/pry/helpers/command_helpers.rb', line 104

def restrict_to_lines(content, lines)
  line_range = one_index_range_or_number(lines)
  Array(content.lines.to_a[line_range]).join
end

#temp_file(ext = '.rb') ⇒ String (private)

Open a temp file and yield it to the block, closing it after

Returns:

  • (String)

    The path of the temp file



11
12
13
14
15
16
# File 'lib/pry/helpers/command_helpers.rb', line 11

def temp_file(ext='.rb')
  file = Tempfile.new(['pry', ext])
  yield file
ensure
  file.close(true) if file
end

#unindent(text, left_padding = 0) ⇒ String (private)

Remove any common leading whitespace from every line in text.

This can be used to make a HEREDOC line up with the left margin, without sacrificing the indentation level of the source code.

e.g. opt.banner unindent <<-USAGE Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "Ut enim ad minim veniam." USAGE

Heavily based on textwrap.dedent from Python, which is: Copyright (C) 1999-2001 Gregory P. Ward. Copyright (C) 2002, 2003 Python Software Foundation. Written by Greg Ward gward@python.net

Licensed under http://docs.python.org/license.html From http://hg.python.org/cpython/file/6b9f0a6efaeb/Lib/textwrap.py

Parameters:

  • text (String)

    The text from which to remove indentation

Returns:

  • (String)

    The text with indentation stripped.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/pry/helpers/command_helpers.rb', line 82

def unindent(text, left_padding = 0)
  # Empty blank lines
  text = text.sub(/^[ \t]+$/, '')

  # Find the longest common whitespace to all indented lines
  margin = text.scan(/^[ \t]*(?=[^ \t\n])/).inject do |current_margin, next_indent|
    if next_indent.start_with?(current_margin)
      current_margin
    elsif current_margin.start_with?(next_indent)
      next_indent
    else
      ""
    end
  end

  text.gsub(/^#{margin}/, ' ' * left_padding)
end