pry 0.11.3 → 0.12.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +157 -1
  3. data/LICENSE +1 -1
  4. data/README.md +13 -30
  5. data/bin/pry +0 -4
  6. data/lib/pry/cli.rb +17 -24
  7. data/lib/pry/code/code_file.rb +5 -4
  8. data/lib/pry/code/code_range.rb +3 -3
  9. data/lib/pry/code/loc.rb +14 -8
  10. data/lib/pry/code.rb +6 -6
  11. data/lib/pry/code_object.rb +4 -4
  12. data/lib/pry/color_printer.rb +1 -0
  13. data/lib/pry/command.rb +36 -29
  14. data/lib/pry/command_set.rb +17 -52
  15. data/lib/pry/commands/amend_line.rb +3 -4
  16. data/lib/pry/commands/bang.rb +1 -1
  17. data/lib/pry/commands/cat/exception_formatter.rb +9 -8
  18. data/lib/pry/commands/cat/input_expression_formatter.rb +1 -1
  19. data/lib/pry/commands/cat.rb +7 -6
  20. data/lib/pry/commands/change_prompt.rb +29 -9
  21. data/lib/pry/commands/clear_screen.rb +14 -0
  22. data/lib/pry/commands/code_collector.rb +9 -9
  23. data/lib/pry/commands/easter_eggs.rb +3 -3
  24. data/lib/pry/commands/edit.rb +8 -7
  25. data/lib/pry/commands/exit.rb +2 -1
  26. data/lib/pry/commands/find_method.rb +11 -13
  27. data/lib/pry/commands/gem_cd.rb +1 -1
  28. data/lib/pry/commands/gem_install.rb +2 -2
  29. data/lib/pry/commands/gem_list.rb +2 -2
  30. data/lib/pry/commands/gem_open.rb +1 -1
  31. data/lib/pry/commands/gem_search.rb +2 -2
  32. data/lib/pry/commands/gem_stats.rb +83 -0
  33. data/lib/pry/commands/gist.rb +7 -6
  34. data/lib/pry/commands/help.rb +3 -3
  35. data/lib/pry/commands/hist.rb +9 -8
  36. data/lib/pry/commands/import_set.rb +2 -1
  37. data/lib/pry/commands/install_command.rb +7 -6
  38. data/lib/pry/commands/list_inspectors.rb +2 -2
  39. data/lib/pry/commands/ls/constants.rb +4 -4
  40. data/lib/pry/commands/ls/formatter.rb +3 -2
  41. data/lib/pry/commands/ls/globals.rb +0 -2
  42. data/lib/pry/commands/ls/grep.rb +0 -2
  43. data/lib/pry/commands/ls/instance_vars.rb +0 -1
  44. data/lib/pry/commands/ls/local_names.rb +0 -2
  45. data/lib/pry/commands/ls/local_vars.rb +0 -2
  46. data/lib/pry/commands/ls/ls_entity.rb +0 -1
  47. data/lib/pry/commands/ls/methods.rb +0 -3
  48. data/lib/pry/commands/ls/methods_helper.rb +1 -1
  49. data/lib/pry/commands/ls/self_methods.rb +0 -1
  50. data/lib/pry/commands/ls.rb +27 -30
  51. data/lib/pry/commands/play.rb +1 -2
  52. data/lib/pry/commands/pry_backtrace.rb +1 -1
  53. data/lib/pry/commands/raise_up.rb +2 -1
  54. data/lib/pry/commands/ri.rb +5 -4
  55. data/lib/pry/commands/shell_command.rb +3 -2
  56. data/lib/pry/commands/shell_mode.rb +6 -6
  57. data/lib/pry/commands/show_doc.rb +5 -7
  58. data/lib/pry/commands/show_info.rb +25 -18
  59. data/lib/pry/commands/show_source.rb +5 -2
  60. data/lib/pry/commands/stat.rb +1 -1
  61. data/lib/pry/commands/watch_expression.rb +9 -7
  62. data/lib/pry/commands/whereami.rb +4 -4
  63. data/lib/pry/commands/wtf.rb +15 -2
  64. data/lib/pry/config/behavior.rb +229 -205
  65. data/lib/pry/config/convenience.rb +24 -21
  66. data/lib/pry/config/default.rb +153 -143
  67. data/lib/pry/config/memoization.rb +41 -37
  68. data/lib/pry/config.rb +33 -9
  69. data/lib/pry/core_extensions.rb +4 -3
  70. data/lib/pry/editor.rb +5 -12
  71. data/lib/pry/exceptions.rb +0 -2
  72. data/lib/pry/helpers/base_helpers.rb +133 -4
  73. data/lib/pry/helpers/command_helpers.rb +5 -4
  74. data/lib/pry/helpers/documentation_helpers.rb +2 -2
  75. data/lib/pry/helpers/options_helpers.rb +5 -5
  76. data/lib/pry/helpers/platform.rb +58 -0
  77. data/lib/pry/helpers/table.rb +20 -15
  78. data/lib/pry/helpers/text.rb +3 -4
  79. data/lib/pry/helpers.rb +1 -0
  80. data/lib/pry/history.rb +21 -8
  81. data/lib/pry/hooks.rb +3 -3
  82. data/lib/pry/indent.rb +15 -17
  83. data/lib/pry/input_completer.rb +12 -7
  84. data/lib/pry/input_lock.rb +0 -2
  85. data/lib/pry/last_exception.rb +1 -1
  86. data/lib/pry/method/disowned.rb +2 -1
  87. data/lib/pry/method/patcher.rb +2 -2
  88. data/lib/pry/method/weird_method_locator.rb +7 -8
  89. data/lib/pry/method.rb +37 -31
  90. data/lib/pry/object_path.rb +5 -4
  91. data/lib/pry/output.rb +3 -2
  92. data/lib/pry/pager.rb +194 -186
  93. data/lib/pry/platform.rb +79 -81
  94. data/lib/pry/plugins.rb +7 -3
  95. data/lib/pry/prompt.rb +144 -25
  96. data/lib/pry/pry_class.rb +53 -29
  97. data/lib/pry/pry_instance.rb +88 -55
  98. data/lib/pry/repl.rb +33 -4
  99. data/lib/pry/repl_file_loader.rb +1 -2
  100. data/lib/pry/ring.rb +84 -0
  101. data/lib/pry/rubygem.rb +6 -6
  102. data/lib/pry/slop/commands.rb +3 -4
  103. data/lib/pry/slop/option.rb +19 -21
  104. data/lib/pry/slop.rb +17 -17
  105. data/lib/pry/terminal.rb +2 -1
  106. data/lib/pry/testable/mockable.rb +2 -2
  107. data/lib/pry/testable/pry_tester.rb +1 -1
  108. data/lib/pry/testable/utility.rb +2 -2
  109. data/lib/pry/testable/variables.rb +1 -1
  110. data/lib/pry/version.rb +1 -1
  111. data/lib/pry/wrapped_module/candidate.rb +2 -2
  112. data/lib/pry/wrapped_module.rb +15 -15
  113. data/lib/pry.rb +17 -47
  114. metadata +8 -20
  115. data/lib/pry/commands/list_prompts.rb +0 -35
  116. data/lib/pry/commands/simple_prompt.rb +0 -22
  117. data/lib/pry/history_array.rb +0 -121
  118. data/lib/pry/rbx_path.rb +0 -22
@@ -1,8 +1,135 @@
1
1
  module Pry::Helpers; end
2
+
3
+ # rubocop:disable Metrics/ModuleLength
2
4
  module Pry::Helpers::BaseHelpers
3
- include Pry::Platform
4
5
  extend self
5
6
 
7
+ @mac_osx_warn = false
8
+ # @deprecated Use {Pry::Helpers::Platform.mac_osx?} instead.
9
+ def mac_osx?
10
+ unless @mac_osx_warn
11
+ loc = caller_locations(1..1).first
12
+ warn(
13
+ "#{loc.path}:#{loc.lineno}: warning: method BaseHelpers##{__method__} " \
14
+ "is deprecated. Use Pry:Helpers::Platform.#{__method__} instead"
15
+ )
16
+ @mac_osx_warn = true
17
+ end
18
+ Pry::Helpers::Platform.mac_osx?
19
+ end
20
+
21
+ @linux_warn = false
22
+ # @deprecated Use {Pry::Helpers::Platform.mac_osx?} instead.
23
+ def linux?
24
+ unless @linux_warn
25
+ loc = caller_locations(1..1).first
26
+ warn(
27
+ "#{loc.path}:#{loc.lineno}: warning: method BaseHelpers##{__method__} " \
28
+ "is deprecated. Use Pry:Helpers::Platform.#{__method__} instead"
29
+ )
30
+ @linux_warn = true
31
+ end
32
+ Pry::Helpers::Platform.linux?
33
+ end
34
+
35
+ @windows_warn = false
36
+ # @deprecated Use {Pry::Helpers::Platform.windows?} instead.
37
+ def windows?
38
+ unless @windows_warn
39
+ loc = caller_locations(1..1).first
40
+ warn(
41
+ "#{loc.path}:#{loc.lineno}: warning: method BaseHelpers##{__method__} " \
42
+ "is deprecated. Use Pry:Helpers::Platform.#{__method__} instead"
43
+ )
44
+ @windows_warn = true
45
+ end
46
+ Pry::Helpers::Platform.windows?
47
+ end
48
+
49
+ @windows_ansi_warn = false
50
+ # @deprecated Use {Pry::Helpers::Platform.windows_ansi?} instead.
51
+ def windows_ansi?
52
+ unless @windows_ansi_warn
53
+ loc = caller_locations(1..1).first
54
+ warn(
55
+ "#{loc.path}:#{loc.lineno}: warning: method BaseHelpers##{__method__} " \
56
+ "is deprecated. Use Pry:Helpers::Platform.#{__method__} instead"
57
+ )
58
+ @windows_ansi_warn = true
59
+ end
60
+ Pry::Helpers::Platform.windows_ansi?
61
+ end
62
+
63
+ @jruby_warn = false
64
+ # @deprecated Use {Pry::Helpers::Platform.jruby?} instead.
65
+ def jruby?
66
+ unless @jruby_warn
67
+ loc = caller_locations(1..1).first
68
+ warn(
69
+ "#{loc.path}:#{loc.lineno}: warning: method BaseHelpers##{__method__} " \
70
+ "is deprecated. Use Pry:Helpers::Platform.#{__method__} instead"
71
+ )
72
+ @jruby_warn = true
73
+ end
74
+ Pry::Helpers::Platform.jruby?
75
+ end
76
+
77
+ @jruby19_warn = false
78
+ # @deprecated Use {Pry::Helpers::Platform.jruby_19?} instead.
79
+ def jruby_19?
80
+ unless @jruby19_warn
81
+ loc = caller_locations(1..1).first
82
+ warn(
83
+ "#{loc.path}:#{loc.lineno}: warning: method BaseHelpers##{__method__} " \
84
+ "is deprecated. Use Pry:Helpers::Platform.#{__method__} instead"
85
+ )
86
+ @jruby19_warn = true
87
+ end
88
+ Pry::Helpers::Platform.jruby_19?
89
+ end
90
+
91
+ @mri_warn = false
92
+ # @deprecated Use {Pry::Helpers::Platform.mri?} instead.
93
+ def mri?
94
+ unless @mri_warn
95
+ loc = caller_locations(1..1).first
96
+ warn(
97
+ "#{loc.path}:#{loc.lineno}: warning: method BaseHelpers##{__method__} " \
98
+ "is deprecated. Use Pry:Helpers::Platform.#{__method__} instead"
99
+ )
100
+ @mri_warn = true
101
+ end
102
+ Pry::Helpers::Platform.mri?
103
+ end
104
+
105
+ @mri19_warn = false
106
+ # @deprecated Use {Pry::Helpers::Platform.mri_19?} instead.
107
+ def mri_19?
108
+ unless @mri19_warn
109
+ loc = caller_locations(1..1).first
110
+ warn(
111
+ "#{loc.path}:#{loc.lineno}: warning: method BaseHelpers##{__method__} " \
112
+ "is deprecated. Use Pry:Helpers::Platform.#{__method__} instead"
113
+ )
114
+ @mri19_warn = true
115
+ end
116
+ Pry::Helpers::Platform.mri_19?
117
+ end
118
+
119
+ @mri2_warn = false
120
+ # @deprecated Use {Pry::Helpers::Platform.mri_2?} instead.
121
+ def mri_2?
122
+ unless @mri2_warn
123
+ loc = caller_locations(1..1).first
124
+ warn(
125
+ "#{loc.path}:#{loc.lineno}: warning: method BaseHelpers##{__method__} " \
126
+ "is deprecated. Use Pry:Helpers::Platform.#{__method__} instead"
127
+ )
128
+ @mri2_warn = true
129
+ end
130
+ Pry::Helpers::Platform.mri_2?
131
+ end
132
+
6
133
  def silence_warnings
7
134
  old_verbose = $VERBOSE
8
135
  $VERBOSE = nil
@@ -35,20 +162,21 @@ module Pry::Helpers::BaseHelpers
35
162
 
36
163
  def command_dependencies_met?(options)
37
164
  return true if !options[:requires_gem]
165
+
38
166
  Array(options[:requires_gem]).all? do |g|
39
167
  Pry::Rubygem.installed?(g)
40
168
  end
41
169
  end
42
170
 
43
171
  def use_ansi_codes?
44
- windows_ansi? || ENV['TERM'] && ENV['TERM'] != "dumb"
172
+ Pry::Helpers::Platform.windows_ansi? || ENV['TERM'] && ENV['TERM'] != "dumb"
45
173
  end
46
174
 
47
175
  def colorize_code(code)
48
176
  CodeRay.scan(code, :ruby).term
49
177
  end
50
178
 
51
- def highlight(string, regexp, highlight_color=:bright_yellow)
179
+ def highlight(string, regexp, highlight_color = :bright_yellow)
52
180
  string.gsub(regexp) { |match| "<#{highlight_color}>#{match}</#{highlight_color}>" }
53
181
  end
54
182
 
@@ -61,7 +189,7 @@ module Pry::Helpers::BaseHelpers
61
189
  # Send the given text through the best available pager (if Pry.config.pager is
62
190
  # enabled). Infers where to send the output if used as a mixin.
63
191
  # DEPRECATED.
64
- def stagger_output(text, out = nil)
192
+ def stagger_output(text, _out = nil)
65
193
  if defined?(_pry_) && _pry_
66
194
  _pry_.pager.page text
67
195
  else
@@ -69,3 +197,4 @@ module Pry::Helpers::BaseHelpers
69
197
  end
70
198
  end
71
199
  end
200
+ # rubocop:enable Metrics/ModuleLength
@@ -8,7 +8,7 @@ class Pry
8
8
 
9
9
  # Open a temp file and yield it to the block, closing it after
10
10
  # @return [String] The path of the temp file
11
- def temp_file(ext='.rb')
11
+ def temp_file(ext = '.rb')
12
12
  file = Tempfile.new(['pry', ext])
13
13
  yield file
14
14
  ensure
@@ -21,7 +21,7 @@ class Pry
21
21
  ["__binding__", "__pry__", "class_eval"].include?(m)
22
22
  end
23
23
 
24
- def get_method_or_raise(name, target, opts={}, omit_help=false)
24
+ def get_method_or_raise(name, target, opts = {}, omit_help = false)
25
25
  meth = Pry::Method.from_str(name, target, opts)
26
26
 
27
27
  if name && !meth
@@ -44,7 +44,7 @@ class Pry
44
44
  meth
45
45
  end
46
46
 
47
- def command_error(message, omit_help, klass=CommandError)
47
+ def command_error(message, omit_help, klass = CommandError)
48
48
  message += " Type `#{command_name} --help` for help." unless omit_help
49
49
  raise klass, message
50
50
  end
@@ -142,8 +142,9 @@ class Pry
142
142
  Range.new(a, b)
143
143
  end
144
144
 
145
- def set_file_and_dir_locals(file_name, _pry_=_pry_(), target=target())
145
+ def set_file_and_dir_locals(file_name, _pry_ = _pry_(), target = target())
146
146
  return if !target or !file_name
147
+
147
148
  _pry_.last_file = File.expand_path(file_name)
148
149
  _pry_.inject_local("_file_", _pry_.last_file, target)
149
150
 
@@ -13,8 +13,8 @@ class Pry
13
13
  gsub(/<em>(?:\s*\n)?(.*?)\s*<\/em>/m) { "\e[1m#{$1}\e[0m" }.
14
14
  gsub(/<i>(?:\s*\n)?(.*?)\s*<\/i>/m) { "\e[1m#{$1}\e[0m" }.
15
15
  gsub(/<tt>(?:\s*\n)?(.*?)\s*<\/tt>/m) { CodeRay.scan($1, :ruby).term }.
16
- gsub(/\B\+(\w+?)\+\B/) { "\e[32m#{$1}\e[0m" }.
17
- gsub(/((?:^[ \t]+.+(?:\n+|\Z))+)/) { CodeRay.scan($1, :ruby).term }.
16
+ gsub(/\B\+(\w+?)\+\B/) { "\e[32m#{$1}\e[0m" }.
17
+ gsub(/((?:^[ \t]+.+(?:\n+|\Z))+)/) { CodeRay.scan($1, :ruby).term }.
18
18
  gsub(/`(?:\s*\n)?([^\e]*?)\s*`/) { "`#{CodeRay.scan($1, :ruby).term}`" }
19
19
  end
20
20
 
@@ -8,8 +8,8 @@ class Pry
8
8
  @method_target = target
9
9
  opt.on :M, "instance-methods", "Operate on instance methods."
10
10
  opt.on :m, :methods, "Operate on methods."
11
- opt.on :s, :super, "Select the 'super' method. Can be repeated to traverse the ancestors.", :as => :count
12
- opt.on :c, :context, "Select object context to run under.", :argument => true do |context|
11
+ opt.on :s, :super, "Select the 'super' method. Can be repeated to traverse the ancestors.", as: :count
12
+ opt.on :c, :context, "Select object context to run under.", argument: true do |context|
13
13
  @method_target = Pry.binding_for(target.eval(context))
14
14
  end
15
15
  end
@@ -17,9 +17,9 @@ class Pry
17
17
  # Get the method object parsed by the slop instance
18
18
  def method_object
19
19
  @method_object ||= get_method_or_raise(args.empty? ? nil : args.join(" "), @method_target,
20
- :super => opts[:super],
21
- :instance => opts.present?(:'instance-methods') && !opts.present?(:'methods'),
22
- :methods => opts.present?(:'methods') && !opts.present?(:'instance-methods')
20
+ super: opts[:super],
21
+ instance: opts.present?(:'instance-methods') && !opts.present?(:'methods'),
22
+ methods: opts.present?(:'methods') && !opts.present?(:'instance-methods')
23
23
  )
24
24
  end
25
25
  end
@@ -0,0 +1,58 @@
1
+ class Pry
2
+ module Helpers
3
+ # Contains methods for querying the platform that Pry is running on
4
+ # @api public
5
+ # @since v0.12.0
6
+ # rubocop:disable Style/DoubleNegation
7
+ module Platform
8
+ # @return [Boolean]
9
+ def self.mac_osx?
10
+ !!(RbConfig::CONFIG['host_os'] =~ /\Adarwin/i)
11
+ end
12
+
13
+ # @return [Boolean]
14
+ def self.linux?
15
+ !!(RbConfig::CONFIG['host_os'] =~ /linux/i)
16
+ end
17
+
18
+ # @return [Boolean] true when Pry is running on Windows with ANSI support,
19
+ # false otherwise
20
+ def self.windows?
21
+ !!(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/)
22
+ end
23
+
24
+ # @return [Boolean]
25
+ def self.windows_ansi?
26
+ return false unless windows?
27
+
28
+ !!(defined?(Win32::Console) || ENV['ANSICON'] || mri_2?)
29
+ end
30
+
31
+ # @return [Boolean]
32
+ def self.jruby?
33
+ RbConfig::CONFIG['ruby_install_name'] == 'jruby'
34
+ end
35
+
36
+ # @return [Boolean]
37
+ def self.jruby_19?
38
+ jruby? && RbConfig::CONFIG['ruby_version'] == '1.9'
39
+ end
40
+
41
+ # @return [Boolean]
42
+ def self.mri?
43
+ RbConfig::CONFIG['ruby_install_name'] == 'ruby'
44
+ end
45
+
46
+ # @return [Boolean]
47
+ def self.mri_19?
48
+ !!(mri? && RUBY_VERSION.start_with?('1.9'))
49
+ end
50
+
51
+ # @return [Boolean]
52
+ def self.mri_2?
53
+ !!(mri? && RUBY_VERSION.start_with?('2'))
54
+ end
55
+ end
56
+ # rubocop:enable Style/DoubleNegation
57
+ end
58
+ end
@@ -1,27 +1,28 @@
1
1
  class Pry
2
2
  module Helpers
3
- def self.tablify_or_one_line(heading, things)
3
+ def self.tablify_or_one_line(heading, things, config = Pry.config)
4
4
  plain_heading = Pry::Helpers::Text.strip_color(heading)
5
- attempt = Table.new(things, :column_count => things.size)
5
+ attempt = Table.new(things, column_count: things.size)
6
6
  if attempt.fits_on_line?(Terminal.width! - plain_heading.size - 2)
7
7
  "#{heading}: #{attempt}\n"
8
8
  else
9
- "#{heading}: \n#{tablify_to_screen_width(things, :indent => ' ')}\n"
9
+ "#{heading}: \n#{tablify_to_screen_width(things, { indent: ' ' }, config)}\n"
10
10
  end
11
11
  end
12
12
 
13
- def self.tablify_to_screen_width(things, options = {})
13
+ def self.tablify_to_screen_width(things, options, config = Pry.config)
14
+ options ||= {}
14
15
  things = things.compact
15
- if indent = options[:indent]
16
+ if (indent = options[:indent])
16
17
  usable_width = Terminal.width! - indent.size
17
- tablify(things, usable_width).to_s.gsub(/^/, indent)
18
+ tablify(things, usable_width, config).to_s.gsub(/^/, indent)
18
19
  else
19
- tablify(things, Terminal.width!).to_s
20
+ tablify(things, Terminal.width!, config).to_s
20
21
  end
21
22
  end
22
23
 
23
- def self.tablify(things, line_length)
24
- table = Table.new(things, :column_count => things.size)
24
+ def self.tablify(things, line_length, config = Pry.config)
25
+ table = Table.new(things, { column_count: things.size }, config)
25
26
  table.column_count -= 1 until 1 == table.column_count or
26
27
  table.fits_on_line?(line_length)
27
28
  table
@@ -29,8 +30,9 @@ class Pry
29
30
 
30
31
  class Table
31
32
  attr_reader :items, :column_count
32
- def initialize items, args = {}
33
+ def initialize(items, args, config = Pry.config)
33
34
  @column_count = args[:column_count]
35
+ @config = config
34
36
  self.items = items
35
37
  end
36
38
 
@@ -39,16 +41,17 @@ class Pry
39
41
  end
40
42
 
41
43
  def rows_to_s style = :color_on
42
- widths = columns.map{|e| _max_width(e)}
44
+ widths = columns.map { |e| _max_width(e) }
43
45
  @rows_without_colors.map do |r|
44
46
  padded = []
45
47
  r.each_with_index do |e,i|
46
48
  next unless e
49
+
47
50
  item = e.ljust(widths[i])
48
51
  item.sub! e, _recall_color_for(e) if :color_on == style
49
52
  padded << item
50
53
  end
51
- padded.join(Pry.config.ls.separator)
54
+ padded.join(@config.ls.separator)
52
55
  end
53
56
  end
54
57
 
@@ -73,6 +76,7 @@ class Pry
73
76
  end
74
77
 
75
78
  def ==(other); items == other.to_a end
79
+
76
80
  def to_a; items.to_a end
77
81
 
78
82
  private
@@ -93,10 +97,11 @@ class Pry
93
97
  def _recolumn
94
98
  @rows_without_colors = []
95
99
  return if items.size.zero?
96
- row_count = (items.size.to_f/column_count).ceil
100
+
101
+ row_count = (items.size.to_f / column_count).ceil
97
102
  row_count.times do |i|
98
- row_indices = (0...column_count).map{|e| row_count*e+i}
99
- @rows_without_colors << row_indices.map{|e| @plain_items[e]}
103
+ row_indices = (0...column_count).map { |e| row_count * e + i }
104
+ @rows_without_colors << row_indices.map { |e| @plain_items[e] }
100
105
  end
101
106
  end
102
107
 
@@ -18,11 +18,11 @@ class Pry
18
18
 
19
19
  COLORS.each_pair do |color, value|
20
20
  define_method color do |text|
21
- "\033[0;#{30+value}m#{text}\033[0m"
21
+ "\033[0;#{30 + value}m#{text}\033[0m"
22
22
  end
23
23
 
24
24
  define_method "bright_#{color}" do |text|
25
- "\033[1;#{30+value}m#{text}\033[0m"
25
+ "\033[1;#{30 + value}m#{text}\033[0m"
26
26
  end
27
27
 
28
28
  COLORS.each_pair do |bg_color, bg_value|
@@ -60,7 +60,6 @@ class Pry
60
60
  def default(text)
61
61
  text.to_s
62
62
  end
63
- alias_method :bright_default, :bold
64
63
 
65
64
  #
66
65
  # @yield
@@ -95,7 +94,7 @@ class Pry
95
94
  # @param [#each_line] text
96
95
  # @param [Fixnum] offset
97
96
  # @return [String]
98
- def with_line_numbers(text, offset, color=:blue)
97
+ def with_line_numbers(text, offset, color = :blue)
99
98
  lines = text.each_line.to_a
100
99
  max_width = (offset + lines.count).to_s.length
101
100
  lines.each_with_index.map do |line, index|
data/lib/pry/helpers.rb CHANGED
@@ -3,3 +3,4 @@ require "pry/helpers/options_helpers"
3
3
  require "pry/helpers/command_helpers"
4
4
  require "pry/helpers/text"
5
5
  require "pry/helpers/table"
6
+ require "pry/helpers/platform"
data/lib/pry/history.rb CHANGED
@@ -7,7 +7,7 @@ class Pry
7
7
  # @return [Fixnum] Number of lines in history when Pry first loaded.
8
8
  attr_reader :original_lines
9
9
 
10
- def initialize(options={})
10
+ def initialize(options = {})
11
11
  @history = []
12
12
  @original_lines = 0
13
13
  @file_path = options[:file_path]
@@ -25,8 +25,8 @@ class Pry
25
25
  @pusher = method(:push_to_readline)
26
26
  @clearer = method(:clear_readline)
27
27
  else
28
- @pusher = proc { }
29
- @clearer = proc { }
28
+ @pusher = proc {}
29
+ @clearer = proc {}
30
30
  end
31
31
  end
32
32
 
@@ -34,6 +34,8 @@ class Pry
34
34
  # @return [Integer] The number of lines loaded
35
35
  def load
36
36
  @loader.call do |line|
37
+ next if invalid_readline_line?(line)
38
+
37
39
  @pusher.call(line.chomp)
38
40
  @history << line.chomp
39
41
  @original_lines += 1
@@ -44,7 +46,9 @@ class Pry
44
46
  # @param [String] line
45
47
  # @return [String] The same line that was passed in
46
48
  def push(line)
47
- unless line.empty? || (@history.last && line == @history.last)
49
+ empty_or_invalid_line = line.empty? || invalid_readline_line?(line)
50
+
51
+ unless empty_or_invalid_line || (@history.last && line == @history.last)
48
52
  @pusher.call(line)
49
53
  @history << line
50
54
  if !should_ignore?(line) && Pry.config.history.should_save
@@ -107,8 +111,8 @@ class Pry
107
111
  if File.exist?(path)
108
112
  File.foreach(path) { |line| yield(line) }
109
113
  end
110
- rescue => error
111
- warn "History file not loaded: #{error.message}"
114
+ rescue SystemCallError => error
115
+ warn "Unable to read history file: #{error.message}"
112
116
  end
113
117
 
114
118
  # The default pusher. Appends the given line to Readline::HISTORY.
@@ -132,17 +136,26 @@ class Pry
132
136
  if defined?(@history_file)
133
137
  @history_file
134
138
  else
139
+ unless File.exist?(history_file_path)
140
+ FileUtils.mkdir_p(File.dirname(history_file_path))
141
+ end
135
142
  @history_file = File.open(history_file_path, 'a', 0600).tap do |file|
136
143
  file.sync = true
137
144
  end
138
145
  end
139
- rescue Errno::EACCES
140
- warn 'History not saved; unable to open your history file for writing.'
146
+ rescue SystemCallError => error
147
+ warn "Unable to write history file: #{error.message}"
141
148
  @history_file = false
142
149
  end
143
150
 
144
151
  def history_file_path
145
152
  File.expand_path(@file_path || Pry.config.history.file)
146
153
  end
154
+
155
+ def invalid_readline_line?(line)
156
+ # `Readline::HISTORY << line` raises an `ArgumentError` if `line`
157
+ # includes a null byte
158
+ line.include?("\0")
159
+ end
147
160
  end
148
161
  end
data/lib/pry/hooks.rb CHANGED
@@ -15,7 +15,7 @@ class Pry
15
15
  end
16
16
 
17
17
  # Ensure that duplicates have their @hooks object.
18
- def initialize_copy(orig)
18
+ def initialize_copy(_orig)
19
19
  hooks_dup = @hooks.dup
20
20
  @hooks.each do |k, v|
21
21
  hooks_dup[k] = v.dup
@@ -32,7 +32,7 @@ class Pry
32
32
  #
33
33
  # @param [Pry::Hooks] other The `Pry::Hooks` instance to merge
34
34
  # @return [Pry:Hooks] The receiver.
35
- # @see {#merge}
35
+ # @see #merge
36
36
  def merge!(other)
37
37
  @hooks.merge!(other.dup.hooks) do |key, array, other_array|
38
38
  temp_hash, output = {}, []
@@ -65,7 +65,7 @@ class Pry
65
65
  # @param [#call] callable The callable.
66
66
  # @yield The block to use as the callable (if no `callable` provided).
67
67
  # @return [Pry:Hooks] The receiver.
68
- def add_hook(event_name, hook_name, callable=nil, &block)
68
+ def add_hook(event_name, hook_name, callable = nil, &block)
69
69
  event_name = event_name.to_s
70
70
 
71
71
  # do not allow duplicates, but allow multiple `nil` hooks
data/lib/pry/indent.rb CHANGED
@@ -143,10 +143,9 @@ class Pry
143
143
  prefix = indent_level
144
144
 
145
145
  input.lines.each do |line|
146
-
147
146
  if in_string?
148
147
  tokens = tokenize("#{open_delimiters_line}\n#{line}")
149
- tokens = tokens.drop_while{ |token, type| !(String === token && token.include?("\n")) }
148
+ tokens = tokens.drop_while { |token, type| !(String === token && token.include?("\n")) }
150
149
  previously_in_string = true
151
150
  else
152
151
  tokens = tokenize(line)
@@ -155,7 +154,7 @@ class Pry
155
154
 
156
155
  before, after = indentation_delta(tokens)
157
156
 
158
- before.times{ prefix.sub! SPACES, '' }
157
+ before.times { prefix.sub! SPACES, '' }
159
158
  new_prefix = prefix + SPACES * after
160
159
 
161
160
  line = prefix + line.lstrip unless previously_in_string
@@ -215,7 +214,7 @@ class Pry
215
214
  # If the list of tokens contains a matching closing token the line should
216
215
  # not be indented (and thus we should return true).
217
216
  tokens.each do |token, kind|
218
- is_singleline_if = (SINGLELINE_TOKENS.include?(token)) && end_of_statement?(last_token, last_kind)
217
+ is_singleline_if = (SINGLELINE_TOKENS.include?(token)) && end_of_statement?(last_token, last_kind)
219
218
  is_optional_do = (token == "do" && seen_for_at.include?(add_after - 1))
220
219
 
221
220
  last_token, last_kind = token, kind unless kind == :space
@@ -358,7 +357,7 @@ class Pry
358
357
  #
359
358
  # @param [String] token a token from Coderay
360
359
  # @param [Symbol] kind the kind of that token
361
- def track_module_nesting_end(token, kind=:keyword)
360
+ def track_module_nesting_end(token, kind = :keyword)
362
361
  if kind == :keyword && (token == "class" || token == "module")
363
362
  @module_nesting.pop
364
363
  end
@@ -382,25 +381,24 @@ class Pry
382
381
  # the correct indentation. Mostly useful for fixing 'end'.
383
382
  #
384
383
  # @param [String] prompt The user's prompt
385
- # @param [String] code The code the user just typed in.
386
- # @param [Fixnum] overhang (0) The number of chars to erase afterwards (i.e.,
387
- # the difference in length between the old line and the new one).
388
- # @return [String]
389
- def correct_indentation(prompt, code, overhang=0)
384
+ # @param [String] code The code the user just typed in
385
+ # @param [Integer] overhang The number of characters to erase afterwards (the
386
+ # the difference in length between the old line and the new one)
387
+ #
388
+ # @return [String] correctly indented line
389
+ def correct_indentation(prompt, code, overhang = 0)
390
390
  prompt = prompt.delete("\001\002")
391
391
  line_to_measure = Pry::Helpers::Text.strip_color(prompt) << code
392
392
  whitespace = ' ' * overhang
393
393
 
394
- _, cols = Terminal.screen_size
395
-
396
- cols = cols.to_i
397
- lines = (cols != 0 ? (line_to_measure.length / cols + 1) : 1).to_i
394
+ cols = Terminal.width!
395
+ lines = cols == 0 ? 1 : (line_to_measure.length / cols + 1).to_i
398
396
 
399
- if Pry::Helpers::BaseHelpers.windows_ansi?
400
- move_up = "\e[#{lines}F"
397
+ if Helpers::Platform.windows_ansi?
398
+ move_up = "\e[#{lines}F"
401
399
  move_down = "\e[#{lines}E"
402
400
  else
403
- move_up = "\e[#{lines}A\e[0G"
401
+ move_up = "\e[#{lines}A\e[0G"
404
402
  move_down = "\e[#{lines}B\e[0G"
405
403
  end
406
404