pry 0.12.2 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +110 -1
  3. data/LICENSE +1 -1
  4. data/README.md +331 -269
  5. data/bin/pry +5 -0
  6. data/lib/pry.rb +133 -119
  7. data/lib/pry/basic_object.rb +8 -4
  8. data/lib/pry/block_command.rb +22 -0
  9. data/lib/pry/class_command.rb +194 -0
  10. data/lib/pry/cli.rb +40 -31
  11. data/lib/pry/code.rb +39 -27
  12. data/lib/pry/code/code_file.rb +28 -24
  13. data/lib/pry/code/code_range.rb +4 -2
  14. data/lib/pry/code/loc.rb +15 -8
  15. data/lib/pry/code_object.rb +40 -38
  16. data/lib/pry/color_printer.rb +47 -46
  17. data/lib/pry/command.rb +166 -369
  18. data/lib/pry/command_set.rb +76 -73
  19. data/lib/pry/command_state.rb +31 -0
  20. data/lib/pry/commands/amend_line.rb +86 -81
  21. data/lib/pry/commands/bang.rb +18 -14
  22. data/lib/pry/commands/bang_pry.rb +15 -11
  23. data/lib/pry/commands/cat.rb +61 -54
  24. data/lib/pry/commands/cat/abstract_formatter.rb +23 -18
  25. data/lib/pry/commands/cat/exception_formatter.rb +71 -60
  26. data/lib/pry/commands/cat/file_formatter.rb +55 -49
  27. data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
  28. data/lib/pry/commands/cd.rb +40 -35
  29. data/lib/pry/commands/change_inspector.rb +29 -22
  30. data/lib/pry/commands/change_prompt.rb +44 -39
  31. data/lib/pry/commands/clear_screen.rb +16 -10
  32. data/lib/pry/commands/code_collector.rb +148 -133
  33. data/lib/pry/commands/disable_pry.rb +23 -19
  34. data/lib/pry/commands/easter_eggs.rb +19 -30
  35. data/lib/pry/commands/edit.rb +184 -161
  36. data/lib/pry/commands/edit/exception_patcher.rb +21 -17
  37. data/lib/pry/commands/edit/file_and_line_locator.rb +34 -23
  38. data/lib/pry/commands/exit.rb +39 -35
  39. data/lib/pry/commands/exit_all.rb +24 -20
  40. data/lib/pry/commands/exit_program.rb +20 -16
  41. data/lib/pry/commands/find_method.rb +168 -160
  42. data/lib/pry/commands/fix_indent.rb +16 -12
  43. data/lib/pry/commands/help.rb +140 -133
  44. data/lib/pry/commands/hist.rb +151 -150
  45. data/lib/pry/commands/import_set.rb +20 -16
  46. data/lib/pry/commands/jump_to.rb +25 -21
  47. data/lib/pry/commands/list_inspectors.rb +35 -28
  48. data/lib/pry/commands/ls.rb +124 -102
  49. data/lib/pry/commands/ls/constants.rb +59 -42
  50. data/lib/pry/commands/ls/formatter.rb +50 -46
  51. data/lib/pry/commands/ls/globals.rb +38 -34
  52. data/lib/pry/commands/ls/grep.rb +17 -13
  53. data/lib/pry/commands/ls/instance_vars.rb +29 -27
  54. data/lib/pry/commands/ls/interrogatable.rb +18 -12
  55. data/lib/pry/commands/ls/jruby_hacks.rb +47 -41
  56. data/lib/pry/commands/ls/local_names.rb +26 -22
  57. data/lib/pry/commands/ls/local_vars.rb +38 -28
  58. data/lib/pry/commands/ls/ls_entity.rb +47 -51
  59. data/lib/pry/commands/ls/methods.rb +44 -43
  60. data/lib/pry/commands/ls/methods_helper.rb +46 -42
  61. data/lib/pry/commands/ls/self_methods.rb +23 -22
  62. data/lib/pry/commands/nesting.rb +21 -17
  63. data/lib/pry/commands/play.rb +93 -82
  64. data/lib/pry/commands/pry_backtrace.rb +24 -17
  65. data/lib/pry/commands/pry_version.rb +15 -11
  66. data/lib/pry/commands/raise_up.rb +27 -22
  67. data/lib/pry/commands/reload_code.rb +60 -48
  68. data/lib/pry/commands/reset.rb +16 -12
  69. data/lib/pry/commands/ri.rb +55 -45
  70. data/lib/pry/commands/save_file.rb +45 -43
  71. data/lib/pry/commands/shell_command.rb +51 -51
  72. data/lib/pry/commands/shell_mode.rb +21 -17
  73. data/lib/pry/commands/show_doc.rb +81 -68
  74. data/lib/pry/commands/show_info.rb +189 -171
  75. data/lib/pry/commands/show_input.rb +16 -11
  76. data/lib/pry/commands/show_source.rb +109 -45
  77. data/lib/pry/commands/stat.rb +35 -31
  78. data/lib/pry/commands/switch_to.rb +21 -15
  79. data/lib/pry/commands/toggle_color.rb +20 -16
  80. data/lib/pry/commands/watch_expression.rb +89 -86
  81. data/lib/pry/commands/watch_expression/expression.rb +32 -27
  82. data/lib/pry/commands/whereami.rb +156 -148
  83. data/lib/pry/commands/wtf.rb +75 -50
  84. data/lib/pry/config.rb +311 -25
  85. data/lib/pry/config/attributable.rb +22 -0
  86. data/lib/pry/config/lazy_value.rb +29 -0
  87. data/lib/pry/config/memoized_value.rb +34 -0
  88. data/lib/pry/config/value.rb +24 -0
  89. data/lib/pry/control_d_handler.rb +28 -0
  90. data/lib/pry/core_extensions.rb +9 -7
  91. data/lib/pry/editor.rb +48 -21
  92. data/lib/pry/env.rb +18 -0
  93. data/lib/pry/exception_handler.rb +43 -0
  94. data/lib/pry/exceptions.rb +13 -16
  95. data/lib/pry/forwardable.rb +5 -1
  96. data/lib/pry/helpers.rb +2 -0
  97. data/lib/pry/helpers/base_helpers.rb +68 -197
  98. data/lib/pry/helpers/command_helpers.rb +50 -61
  99. data/lib/pry/helpers/documentation_helpers.rb +20 -13
  100. data/lib/pry/helpers/options_helpers.rb +14 -7
  101. data/lib/pry/helpers/platform.rb +7 -5
  102. data/lib/pry/helpers/table.rb +33 -26
  103. data/lib/pry/helpers/text.rb +17 -14
  104. data/lib/pry/history.rb +48 -56
  105. data/lib/pry/hooks.rb +21 -12
  106. data/lib/pry/indent.rb +54 -50
  107. data/lib/pry/input_completer.rb +248 -230
  108. data/lib/pry/input_lock.rb +8 -9
  109. data/lib/pry/inspector.rb +36 -24
  110. data/lib/pry/last_exception.rb +45 -45
  111. data/lib/pry/method.rb +141 -94
  112. data/lib/pry/method/disowned.rb +16 -4
  113. data/lib/pry/method/patcher.rb +12 -3
  114. data/lib/pry/method/weird_method_locator.rb +68 -44
  115. data/lib/pry/object_path.rb +33 -25
  116. data/lib/pry/output.rb +121 -35
  117. data/lib/pry/pager.rb +41 -42
  118. data/lib/pry/plugins.rb +25 -8
  119. data/lib/pry/prompt.rb +123 -54
  120. data/lib/pry/pry_class.rb +61 -98
  121. data/lib/pry/pry_instance.rb +217 -215
  122. data/lib/pry/repl.rb +18 -22
  123. data/lib/pry/repl_file_loader.rb +27 -21
  124. data/lib/pry/ring.rb +11 -6
  125. data/lib/pry/slop.rb +574 -563
  126. data/lib/pry/slop/commands.rb +164 -169
  127. data/lib/pry/slop/option.rb +172 -168
  128. data/lib/pry/syntax_highlighter.rb +26 -0
  129. data/lib/pry/system_command_handler.rb +17 -0
  130. data/lib/pry/testable.rb +59 -61
  131. data/lib/pry/testable/evalable.rb +21 -12
  132. data/lib/pry/testable/mockable.rb +18 -10
  133. data/lib/pry/testable/pry_tester.rb +71 -56
  134. data/lib/pry/testable/utility.rb +29 -21
  135. data/lib/pry/testable/variables.rb +49 -43
  136. data/lib/pry/version.rb +3 -1
  137. data/lib/pry/warning.rb +27 -0
  138. data/lib/pry/wrapped_module.rb +51 -42
  139. data/lib/pry/wrapped_module/candidate.rb +21 -14
  140. metadata +31 -30
  141. data/lib/pry/commands.rb +0 -6
  142. data/lib/pry/commands/disabled_commands.rb +0 -2
  143. data/lib/pry/commands/gem_cd.rb +0 -26
  144. data/lib/pry/commands/gem_install.rb +0 -32
  145. data/lib/pry/commands/gem_list.rb +0 -33
  146. data/lib/pry/commands/gem_open.rb +0 -29
  147. data/lib/pry/commands/gem_readme.rb +0 -25
  148. data/lib/pry/commands/gem_search.rb +0 -40
  149. data/lib/pry/commands/gem_stats.rb +0 -83
  150. data/lib/pry/commands/gist.rb +0 -102
  151. data/lib/pry/commands/install_command.rb +0 -54
  152. data/lib/pry/config/behavior.rb +0 -255
  153. data/lib/pry/config/convenience.rb +0 -28
  154. data/lib/pry/config/default.rb +0 -159
  155. data/lib/pry/config/memoization.rb +0 -48
  156. data/lib/pry/platform.rb +0 -91
  157. data/lib/pry/rubygem.rb +0 -84
  158. data/lib/pry/terminal.rb +0 -91
@@ -1,21 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
4
  module Helpers
3
-
4
5
  # This class contains methods useful for extracting
5
6
  # documentation from methods and classes.
6
7
  module DocumentationHelpers
8
+ YARD_TAGS = %w[
9
+ param return option yield attr attr_reader attr_writer deprecate example
10
+ raise
11
+ ].freeze
7
12
 
8
13
  module_function
9
14
 
10
15
  def process_rdoc(comment)
11
16
  comment = comment.dup
12
- comment.gsub(/<code>(?:\s*\n)?(.*?)\s*<\/code>/m) { CodeRay.scan($1, :ruby).term }.
13
- gsub(/<em>(?:\s*\n)?(.*?)\s*<\/em>/m) { "\e[1m#{$1}\e[0m" }.
14
- gsub(/<i>(?:\s*\n)?(.*?)\s*<\/i>/m) { "\e[1m#{$1}\e[0m" }.
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 }.
18
- gsub(/`(?:\s*\n)?([^\e]*?)\s*`/) { "`#{CodeRay.scan($1, :ruby).term}`" }
17
+ last_match_ruby = proc do
18
+ SyntaxHighlighter.highlight(Regexp.last_match(1))
19
+ end
20
+ comment.gsub(%r{<code>(?:\s*\n)?(.*?)\s*</code>}m, &last_match_ruby)
21
+ .gsub(%r{<em>(?:\s*\n)?(.*?)\s*</em>}m) { "\e[1m#{Regexp.last_match(1)}\e[0m" }
22
+ .gsub(%r{<i>(?:\s*\n)?(.*?)\s*</i>}m) { "\e[1m#{Regexp.last_match(1)}\e[0m" }
23
+ .gsub(%r{<tt>(?:\s*\n)?(.*?)\s*</tt>}m, &last_match_ruby)
24
+ .gsub(/\B\+(\w+?)\+\B/) { "\e[32m#{Regexp.last_match(1)}\e[0m" }
25
+ .gsub(/((?:^[ \t]+.+(?:\n+|\Z))+)/, &last_match_ruby)
26
+ .gsub(/`(?:\s*\n)?([^\e]*?)\s*`/) { "`#{last_match_ruby.call}`" }
19
27
  end
20
28
 
21
29
  def process_yardoc_tag(comment, tag)
@@ -34,10 +42,9 @@ class Pry
34
42
  end
35
43
 
36
44
  def process_yardoc(comment)
37
- yard_tags = ["param", "return", "option", "yield", "attr", "attr_reader", "attr_writer",
38
- "deprecate", "example", "raise"]
39
- (yard_tags - ["example"]).inject(comment) { |a, v| process_yardoc_tag(a, v) }.
40
- gsub(/^@(#{yard_tags.join("|")})/) { "\e[33m#{$1}\e[0m" }
45
+ (YARD_TAGS - %w[example])
46
+ .inject(comment) { |a, v| process_yardoc_tag(a, v) }
47
+ .gsub(/^@(#{YARD_TAGS.join("|")})/) { "\e[33m#{Regexp.last_match(1)}\e[0m" }
41
48
  end
42
49
 
43
50
  def process_comment_markup(comment)
@@ -47,7 +54,7 @@ class Pry
47
54
  # @param [String] code
48
55
  # @return [String]
49
56
  def strip_comments_from_c_code(code)
50
- code.sub(/\A\s*\/\*.*?\*\/\s*/m, '')
57
+ code.sub(%r{\A\s*/\*.*?\*/\s*}m, '')
51
58
  end
52
59
 
53
60
  # Given a string that makes up a comment in a source-code file parse out the content
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
4
  module Helpers
3
5
  module OptionsHelpers
@@ -8,19 +10,24 @@ class Pry
8
10
  @method_target = target
9
11
  opt.on :M, "instance-methods", "Operate on instance methods."
10
12
  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|
13
+ opt.on :s, :super, "Select the 'super' method. Can be repeated to " \
14
+ "traverse the ancestors.", as: :count
15
+ opt.on(
16
+ :c, :context, "Select object context to run under.", argument: true
17
+ ) do |context|
13
18
  @method_target = Pry.binding_for(target.eval(context))
14
19
  end
15
20
  end
16
21
 
17
22
  # Get the method object parsed by the slop instance
18
23
  def method_object
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')
23
- )
24
+ @method_object ||= get_method_or_raise(
25
+ args.empty? ? nil : args.join(" "),
26
+ @method_target,
27
+ super: opts[:super],
28
+ instance: opts.present?(:'instance-methods') && !opts.present?(:methods),
29
+ methods: opts.present?(:methods) && !opts.present?(:'instance-methods')
30
+ )
24
31
  end
25
32
  end
26
33
  end
@@ -1,9 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rbconfig'
4
+
1
5
  class Pry
2
6
  module Helpers
3
7
  # Contains methods for querying the platform that Pry is running on
4
8
  # @api public
5
9
  # @since v0.12.0
6
- # rubocop:disable Style/DoubleNegation
7
10
  module Platform
8
11
  # @return [Boolean]
9
12
  def self.mac_osx?
@@ -25,7 +28,7 @@ class Pry
25
28
  def self.windows_ansi?
26
29
  return false unless windows?
27
30
 
28
- !!(defined?(Win32::Console) || ENV['ANSICON'] || mri_2?)
31
+ !!(defined?(Win32::Console) || Pry::Env['ANSICON'] || mri_2?)
29
32
  end
30
33
 
31
34
  # @return [Boolean]
@@ -45,14 +48,13 @@ class Pry
45
48
 
46
49
  # @return [Boolean]
47
50
  def self.mri_19?
48
- !!(mri? && RUBY_VERSION.start_with?('1.9'))
51
+ mri? && RUBY_VERSION.start_with?('1.9')
49
52
  end
50
53
 
51
54
  # @return [Boolean]
52
55
  def self.mri_2?
53
- !!(mri? && RUBY_VERSION.start_with?('2'))
56
+ mri? && RUBY_VERSION.start_with?('2')
54
57
  end
55
58
  end
56
- # rubocop:enable Style/DoubleNegation
57
59
  end
58
60
  end
@@ -1,38 +1,42 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
4
  module Helpers
3
- def self.tablify_or_one_line(heading, things, config = Pry.config)
5
+ def self.tablify_or_one_line(heading, things, pry_instance = Pry.new)
4
6
  plain_heading = Pry::Helpers::Text.strip_color(heading)
5
7
  attempt = Table.new(things, column_count: things.size)
6
- if attempt.fits_on_line?(Terminal.width! - plain_heading.size - 2)
8
+ if attempt.fits_on_line?(pry_instance.output.width - plain_heading.size - 2)
7
9
  "#{heading}: #{attempt}\n"
8
10
  else
9
- "#{heading}: \n#{tablify_to_screen_width(things, { indent: ' ' }, config)}\n"
11
+ content = tablify_to_screen_width(things, { indent: ' ' }, pry_instance)
12
+ "#{heading}: \n#{content}\n"
10
13
  end
11
14
  end
12
15
 
13
- def self.tablify_to_screen_width(things, options, config = Pry.config)
16
+ def self.tablify_to_screen_width(things, options, pry_instance = Pry.new)
14
17
  options ||= {}
15
18
  things = things.compact
16
19
  if (indent = options[:indent])
17
- usable_width = Terminal.width! - indent.size
18
- tablify(things, usable_width, config).to_s.gsub(/^/, indent)
20
+ usable_width = pry_instance.output.width - indent.size
21
+ tablify(things, usable_width, pry_instance).to_s.gsub(/^/, indent)
19
22
  else
20
- tablify(things, Terminal.width!, config).to_s
23
+ tablify(things, pry_instance.output.width, pry_instance).to_s
21
24
  end
22
25
  end
23
26
 
24
- def self.tablify(things, line_length, config = Pry.config)
25
- table = Table.new(things, { column_count: things.size }, config)
26
- table.column_count -= 1 until 1 == table.column_count or
27
- table.fits_on_line?(line_length)
27
+ def self.tablify(things, line_length, pry_instance = Pry.new)
28
+ table = Table.new(things, { column_count: things.size }, pry_instance)
29
+ until (table.column_count == 1) || table.fits_on_line?(line_length)
30
+ table.column_count -= 1
31
+ end
28
32
  table
29
33
  end
30
34
 
31
35
  class Table
32
36
  attr_reader :items, :column_count
33
- def initialize(items, args, config = Pry.config)
37
+ def initialize(items, args, pry_instance = Pry.new)
34
38
  @column_count = args[:column_count]
35
- @config = config
39
+ @config = pry_instance.config
36
40
  self.items = items
37
41
  end
38
42
 
@@ -40,46 +44,50 @@ class Pry
40
44
  rows_to_s.join("\n")
41
45
  end
42
46
 
43
- def rows_to_s style = :color_on
47
+ def rows_to_s(style = :color_on)
44
48
  widths = columns.map { |e| _max_width(e) }
45
49
  @rows_without_colors.map do |r|
46
50
  padded = []
47
- r.each_with_index do |e,i|
51
+ r.each_with_index do |e, i|
48
52
  next unless e
49
53
 
50
54
  item = e.ljust(widths[i])
51
- item.sub! e, _recall_color_for(e) if :color_on == style
55
+ item.sub! e, _recall_color_for(e) if style == :color_on
52
56
  padded << item
53
57
  end
54
58
  padded.join(@config.ls.separator)
55
59
  end
56
60
  end
57
61
 
58
- def items= items
62
+ def items=(items)
59
63
  @items = items
60
64
  _rebuild_colorless_cache
61
65
  _recolumn
62
- items
63
66
  end
64
67
 
65
- def column_count= n
66
- @column_count = n
68
+ def column_count=(count)
69
+ @column_count = count
67
70
  _recolumn
68
71
  end
69
72
 
70
- def fits_on_line? line_length
71
- _max_width(rows_to_s :no_color) <= line_length
73
+ def fits_on_line?(line_length)
74
+ _max_width(rows_to_s(:no_color)) <= line_length
72
75
  end
73
76
 
74
77
  def columns
75
78
  @rows_without_colors.transpose
76
79
  end
77
80
 
78
- def ==(other); items == other.to_a end
81
+ def ==(other)
82
+ items == other.to_a
83
+ end
79
84
 
80
- def to_a; items.to_a end
85
+ def to_a
86
+ items.to_a
87
+ end
81
88
 
82
89
  private
90
+
83
91
  def _max_width(things)
84
92
  things.compact.map(&:size).max || 0
85
93
  end
@@ -105,10 +113,9 @@ class Pry
105
113
  end
106
114
  end
107
115
 
108
- def _recall_color_for thing
116
+ def _recall_color_for(thing)
109
117
  @colorless_cache[thing]
110
118
  end
111
119
  end
112
-
113
120
  end
114
121
  end
@@ -1,20 +1,23 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
4
  module Helpers
3
- # The methods defined on {Text} are available to custom commands via {Pry::Command#text}.
5
+ # The methods defined on {Text} are available to custom commands via
6
+ # {Pry::Command#text}.
4
7
  module Text
5
8
  extend self
6
- COLORS =
7
- {
8
- "black" => 0,
9
- "red" => 1,
10
- "green" => 2,
11
- "yellow" => 3,
12
- "blue" => 4,
13
- "purple" => 5,
9
+
10
+ COLORS = {
11
+ "black" => 0,
12
+ "red" => 1,
13
+ "green" => 2,
14
+ "yellow" => 3,
15
+ "blue" => 4,
16
+ "purple" => 5,
14
17
  "magenta" => 5,
15
- "cyan" => 6,
16
- "white" => 7
17
- }
18
+ "cyan" => 6,
19
+ "white" => 7
20
+ }.freeze
18
21
 
19
22
  COLORS.each_pair do |color, value|
20
23
  define_method color do |text|
@@ -41,7 +44,7 @@ class Pry
41
44
  # @param [String, #to_s] text
42
45
  # @return [String] _text_ stripped of any color codes.
43
46
  def strip_color(text)
44
- text.to_s.gsub(/(\001)?\e\[.*?(\d)+m(\002)?/ , '')
47
+ text.to_s.gsub(/(\001)?\e\[.*?(\d)+m(\002)?/, '')
45
48
  end
46
49
 
47
50
  # Returns _text_ as bold text for use on a terminal.
@@ -99,7 +102,7 @@ class Pry
99
102
  max_width = (offset + lines.count).to_s.length
100
103
  lines.each_with_index.map do |line, index|
101
104
  adjusted_index = (index + offset).to_s.rjust(max_width)
102
- "#{self.send(color, adjusted_index)}: #{line}"
105
+ "#{send(color, adjusted_index)}: #{line}"
103
106
  end.join
104
107
  end
105
108
 
@@ -1,33 +1,38 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
4
  # The History class is responsible for maintaining the user's input history,
3
5
  # both internally and within Readline.
4
6
  class History
5
- attr_accessor :loader, :saver, :pusher, :clearer
7
+ def self.default_file
8
+ history_file =
9
+ if (xdg_home = Pry::Env['XDG_DATA_HOME'])
10
+ # See XDG Base Directory Specification at
11
+ # https://standards.freedesktop.org/basedir-spec/basedir-spec-0.8.html
12
+ xdg_home + '/pry/pry_history'
13
+ elsif File.exist?(File.expand_path('~/.pry_history'))
14
+ '~/.pry_history'
15
+ else
16
+ '~/.local/share/pry/pry_history'
17
+ end
18
+ File.expand_path(history_file)
19
+ end
20
+
21
+ attr_accessor :loader, :saver
6
22
 
7
23
  # @return [Fixnum] Number of lines in history when Pry first loaded.
8
24
  attr_reader :original_lines
9
25
 
26
+ # @return [Integer] total number of lines, including original lines
27
+ attr_reader :history_line_count
28
+
10
29
  def initialize(options = {})
11
- @history = []
12
- @original_lines = 0
30
+ @history = options[:history] || []
31
+ @history_line_count = @history.count
13
32
  @file_path = options[:file_path]
14
- restore_default_behavior
15
- end
16
-
17
- # Assign the default methods for loading, saving, pushing, and clearing.
18
- def restore_default_behavior
19
- Pry.config.input # force Readline to load if applicable
20
-
33
+ @original_lines = 0
21
34
  @loader = method(:read_from_file)
22
- @saver = method(:save_to_file)
23
-
24
- if defined?(Readline)
25
- @pusher = method(:push_to_readline)
26
- @clearer = method(:clear_readline)
27
- else
28
- @pusher = proc {}
29
- @clearer = proc {}
30
- end
35
+ @saver = method(:save_to_file)
31
36
  end
32
37
 
33
38
  # Load the input history using `History.loader`.
@@ -36,9 +41,9 @@ class Pry
36
41
  @loader.call do |line|
37
42
  next if invalid_readline_line?(line)
38
43
 
39
- @pusher.call(line.chomp)
40
44
  @history << line.chomp
41
45
  @original_lines += 1
46
+ @history_line_count += 1
42
47
  end
43
48
  end
44
49
 
@@ -46,15 +51,20 @@ class Pry
46
51
  # @param [String] line
47
52
  # @return [String] The same line that was passed in
48
53
  def push(line)
49
- empty_or_invalid_line = line.empty? || invalid_readline_line?(line)
54
+ return line if line.empty? || invalid_readline_line?(line)
50
55
 
51
- unless empty_or_invalid_line || (@history.last && line == @history.last)
52
- @pusher.call(line)
53
- @history << line
54
- if !should_ignore?(line) && Pry.config.history.should_save
55
- @saver.call(line)
56
- end
56
+ begin
57
+ last_line = @history[-1]
58
+ rescue IndexError
59
+ last_line = nil
57
60
  end
61
+
62
+ return line if line == last_line
63
+
64
+ @history << line
65
+ @history_line_count += 1
66
+ @saver.call(line) if !should_ignore?(line) && Pry.config.history_save
67
+
58
68
  line
59
69
  end
60
70
  alias << push
@@ -62,26 +72,21 @@ class Pry
62
72
  # Clear this session's history. This won't affect the contents of the
63
73
  # history file.
64
74
  def clear
65
- @clearer.call
75
+ @history.clear
76
+ @history_line_count = 0
66
77
  @original_lines = 0
67
- @history = []
68
- end
69
-
70
- # @return [Fixnum] The number of lines in history.
71
- def history_line_count
72
- @history.count
73
78
  end
74
79
 
75
80
  # @return [Fixnum] The number of lines in history from just this session.
76
81
  def session_line_count
77
- @history.count - @original_lines
82
+ @history_line_count - @original_lines
78
83
  end
79
84
 
80
85
  # Return an Array containing all stored history.
81
86
  # @return [Array<String>] An Array containing all lines of history loaded
82
87
  # or entered by the user in the current session.
83
88
  def to_a
84
- @history.dup
89
+ @history.to_a
85
90
  end
86
91
 
87
92
  # Filter the history with the histignore options
@@ -94,39 +99,26 @@ class Pry
94
99
  private
95
100
 
96
101
  # Check if the line match any option in the histignore
97
- # [Pry.config.history.histignore]
102
+ # [Pry.config.history_ignorelist]
98
103
  # @return [Boolean] a boolean that notifies if the line was found in the
99
104
  # histignore array.
100
105
  def should_ignore?(line)
101
- hist_ignore = Pry.config.history.histignore
106
+ hist_ignore = Pry.config.history_ignorelist
102
107
  return false if hist_ignore.nil? || hist_ignore.empty?
103
108
 
104
109
  hist_ignore.any? { |p| line.to_s.match(p) }
105
110
  end
106
111
 
107
- # The default loader. Yields lines from `Pry.history.config.file`.
112
+ # The default loader. Yields lines from `Pry.config.history_file`.
108
113
  def read_from_file
109
114
  path = history_file_path
110
115
 
111
- if File.exist?(path)
112
- File.foreach(path) { |line| yield(line) }
113
- end
116
+ File.foreach(path) { |line| yield(line) } if File.exist?(path)
114
117
  rescue SystemCallError => error
115
118
  warn "Unable to read history file: #{error.message}"
116
119
  end
117
120
 
118
- # The default pusher. Appends the given line to Readline::HISTORY.
119
- # @param [String] line
120
- def push_to_readline(line)
121
- Readline::HISTORY << line
122
- end
123
-
124
- # The default clearer. Clears Readline::HISTORY.
125
- def clear_readline
126
- Readline::HISTORY.shift until Readline::HISTORY.empty?
127
- end
128
-
129
- # The default saver. Appends the given line to `Pry.history.config.file`.
121
+ # The default saver. Appends the given line to `Pry.config.history_file`.
130
122
  def save_to_file(line)
131
123
  history_file.puts line if history_file
132
124
  end
@@ -139,7 +131,7 @@ class Pry
139
131
  unless File.exist?(history_file_path)
140
132
  FileUtils.mkdir_p(File.dirname(history_file_path))
141
133
  end
142
- @history_file = File.open(history_file_path, 'a', 0600).tap do |file|
134
+ @history_file = File.open(history_file_path, 'a', 0o600).tap do |file|
143
135
  file.sync = true
144
136
  end
145
137
  end
@@ -149,7 +141,7 @@ class Pry
149
141
  end
150
142
 
151
143
  def history_file_path
152
- File.expand_path(@file_path || Pry.config.history.file)
144
+ File.expand_path(@file_path || Pry.config.history_file)
153
145
  end
154
146
 
155
147
  def invalid_readline_line?(line)