pry 0.10.pre.1-i386-mswin32 → 0.10.0.pre3-i386-mswin32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +702 -0
  3. data/LICENSE +2 -2
  4. data/{README.markdown → README.md} +41 -35
  5. data/lib/pry.rb +82 -139
  6. data/lib/pry/cli.rb +77 -30
  7. data/lib/pry/code.rb +122 -183
  8. data/lib/pry/code/code_file.rb +103 -0
  9. data/lib/pry/code/code_range.rb +71 -0
  10. data/lib/pry/code/loc.rb +92 -0
  11. data/lib/pry/code_object.rb +172 -0
  12. data/lib/pry/color_printer.rb +55 -0
  13. data/lib/pry/command.rb +184 -28
  14. data/lib/pry/command_set.rb +113 -59
  15. data/lib/pry/commands.rb +4 -27
  16. data/lib/pry/commands/amend_line.rb +99 -0
  17. data/lib/pry/commands/bang.rb +20 -0
  18. data/lib/pry/commands/bang_pry.rb +17 -0
  19. data/lib/pry/commands/cat.rb +62 -0
  20. data/lib/pry/commands/cat/abstract_formatter.rb +27 -0
  21. data/lib/pry/commands/cat/exception_formatter.rb +77 -0
  22. data/lib/pry/commands/cat/file_formatter.rb +67 -0
  23. data/lib/pry/commands/cat/input_expression_formatter.rb +43 -0
  24. data/lib/pry/commands/cd.rb +41 -0
  25. data/lib/pry/commands/change_inspector.rb +27 -0
  26. data/lib/pry/commands/change_prompt.rb +26 -0
  27. data/lib/pry/commands/code_collector.rb +165 -0
  28. data/lib/pry/commands/disable_pry.rb +27 -0
  29. data/lib/pry/commands/disabled_commands.rb +2 -0
  30. data/lib/pry/commands/easter_eggs.rb +112 -0
  31. data/lib/pry/commands/edit.rb +195 -0
  32. data/lib/pry/commands/edit/exception_patcher.rb +25 -0
  33. data/lib/pry/commands/edit/file_and_line_locator.rb +36 -0
  34. data/lib/pry/commands/exit.rb +42 -0
  35. data/lib/pry/commands/exit_all.rb +29 -0
  36. data/lib/pry/commands/exit_program.rb +23 -0
  37. data/lib/pry/commands/find_method.rb +193 -0
  38. data/lib/pry/commands/fix_indent.rb +19 -0
  39. data/lib/pry/commands/gem_cd.rb +26 -0
  40. data/lib/pry/commands/gem_install.rb +32 -0
  41. data/lib/pry/commands/gem_list.rb +33 -0
  42. data/lib/pry/commands/gem_open.rb +29 -0
  43. data/lib/pry/commands/gist.rb +101 -0
  44. data/lib/pry/commands/help.rb +164 -0
  45. data/lib/pry/commands/hist.rb +180 -0
  46. data/lib/pry/commands/import_set.rb +22 -0
  47. data/lib/pry/commands/install_command.rb +53 -0
  48. data/lib/pry/commands/jump_to.rb +29 -0
  49. data/lib/pry/commands/list_inspectors.rb +35 -0
  50. data/lib/pry/commands/list_prompts.rb +35 -0
  51. data/lib/pry/commands/ls.rb +114 -0
  52. data/lib/pry/commands/ls/constants.rb +47 -0
  53. data/lib/pry/commands/ls/formatter.rb +49 -0
  54. data/lib/pry/commands/ls/globals.rb +48 -0
  55. data/lib/pry/commands/ls/grep.rb +21 -0
  56. data/lib/pry/commands/ls/instance_vars.rb +39 -0
  57. data/lib/pry/commands/ls/interrogatable.rb +18 -0
  58. data/lib/pry/commands/ls/jruby_hacks.rb +49 -0
  59. data/lib/pry/commands/ls/local_names.rb +35 -0
  60. data/lib/pry/commands/ls/local_vars.rb +39 -0
  61. data/lib/pry/commands/ls/ls_entity.rb +70 -0
  62. data/lib/pry/commands/ls/methods.rb +57 -0
  63. data/lib/pry/commands/ls/methods_helper.rb +46 -0
  64. data/lib/pry/commands/ls/self_methods.rb +32 -0
  65. data/lib/pry/commands/nesting.rb +25 -0
  66. data/lib/pry/commands/play.rb +103 -0
  67. data/lib/pry/commands/pry_backtrace.rb +25 -0
  68. data/lib/pry/commands/pry_version.rb +17 -0
  69. data/lib/pry/commands/raise_up.rb +32 -0
  70. data/lib/pry/commands/reload_code.rb +62 -0
  71. data/lib/pry/commands/reset.rb +18 -0
  72. data/lib/pry/commands/ri.rb +60 -0
  73. data/lib/pry/commands/save_file.rb +61 -0
  74. data/lib/pry/commands/shell_command.rb +48 -0
  75. data/lib/pry/commands/shell_mode.rb +25 -0
  76. data/lib/pry/commands/show_doc.rb +83 -0
  77. data/lib/pry/commands/show_info.rb +195 -0
  78. data/lib/pry/commands/show_input.rb +17 -0
  79. data/lib/pry/commands/show_source.rb +50 -0
  80. data/lib/pry/commands/simple_prompt.rb +22 -0
  81. data/lib/pry/commands/stat.rb +40 -0
  82. data/lib/pry/commands/switch_to.rb +23 -0
  83. data/lib/pry/commands/toggle_color.rb +24 -0
  84. data/lib/pry/commands/watch_expression.rb +105 -0
  85. data/lib/pry/commands/watch_expression/expression.rb +38 -0
  86. data/lib/pry/commands/whereami.rb +190 -0
  87. data/lib/pry/commands/wtf.rb +57 -0
  88. data/lib/pry/config.rb +20 -229
  89. data/lib/pry/config/behavior.rb +139 -0
  90. data/lib/pry/config/convenience.rb +26 -0
  91. data/lib/pry/config/default.rb +165 -0
  92. data/lib/pry/core_extensions.rb +59 -38
  93. data/lib/pry/editor.rb +133 -0
  94. data/lib/pry/exceptions.rb +77 -0
  95. data/lib/pry/helpers.rb +1 -0
  96. data/lib/pry/helpers/base_helpers.rb +40 -154
  97. data/lib/pry/helpers/command_helpers.rb +19 -130
  98. data/lib/pry/helpers/documentation_helpers.rb +21 -11
  99. data/lib/pry/helpers/table.rb +109 -0
  100. data/lib/pry/helpers/text.rb +8 -9
  101. data/lib/pry/history.rb +61 -45
  102. data/lib/pry/history_array.rb +11 -1
  103. data/lib/pry/hooks.rb +10 -32
  104. data/lib/pry/indent.rb +110 -38
  105. data/lib/pry/input_completer.rb +242 -0
  106. data/lib/pry/input_lock.rb +132 -0
  107. data/lib/pry/inspector.rb +27 -0
  108. data/lib/pry/last_exception.rb +61 -0
  109. data/lib/pry/method.rb +199 -200
  110. data/lib/pry/method/disowned.rb +53 -0
  111. data/lib/pry/method/patcher.rb +125 -0
  112. data/lib/pry/method/weird_method_locator.rb +186 -0
  113. data/lib/pry/module_candidate.rb +39 -33
  114. data/lib/pry/object_path.rb +82 -0
  115. data/lib/pry/output.rb +50 -0
  116. data/lib/pry/pager.rb +234 -0
  117. data/lib/pry/plugins.rb +4 -3
  118. data/lib/pry/prompt.rb +26 -0
  119. data/lib/pry/pry_class.rb +199 -227
  120. data/lib/pry/pry_instance.rb +344 -403
  121. data/lib/pry/rbx_path.rb +1 -1
  122. data/lib/pry/repl.rb +202 -0
  123. data/lib/pry/repl_file_loader.rb +20 -26
  124. data/lib/pry/rubygem.rb +82 -0
  125. data/lib/pry/terminal.rb +79 -0
  126. data/lib/pry/test/helper.rb +170 -0
  127. data/lib/pry/version.rb +1 -1
  128. data/lib/pry/wrapped_module.rb +133 -48
  129. metadata +132 -197
  130. data/.document +0 -2
  131. data/.gemtest +0 -0
  132. data/.gitignore +0 -16
  133. data/.travis.yml +0 -17
  134. data/.yardopts +0 -1
  135. data/CHANGELOG +0 -387
  136. data/CONTRIBUTORS +0 -36
  137. data/Gemfile +0 -2
  138. data/Rakefile +0 -137
  139. data/TODO +0 -117
  140. data/examples/example_basic.rb +0 -15
  141. data/examples/example_command_override.rb +0 -32
  142. data/examples/example_commands.rb +0 -36
  143. data/examples/example_hooks.rb +0 -9
  144. data/examples/example_image_edit.rb +0 -67
  145. data/examples/example_input.rb +0 -7
  146. data/examples/example_input2.rb +0 -29
  147. data/examples/example_output.rb +0 -11
  148. data/examples/example_print.rb +0 -6
  149. data/examples/example_prompt.rb +0 -9
  150. data/examples/helper.rb +0 -6
  151. data/lib/pry/completion.rb +0 -221
  152. data/lib/pry/custom_completions.rb +0 -6
  153. data/lib/pry/default_commands/cd.rb +0 -81
  154. data/lib/pry/default_commands/commands.rb +0 -62
  155. data/lib/pry/default_commands/context.rb +0 -98
  156. data/lib/pry/default_commands/easter_eggs.rb +0 -95
  157. data/lib/pry/default_commands/editing.rb +0 -420
  158. data/lib/pry/default_commands/find_method.rb +0 -169
  159. data/lib/pry/default_commands/gems.rb +0 -84
  160. data/lib/pry/default_commands/gist.rb +0 -187
  161. data/lib/pry/default_commands/help.rb +0 -127
  162. data/lib/pry/default_commands/hist.rb +0 -120
  163. data/lib/pry/default_commands/input_and_output.rb +0 -306
  164. data/lib/pry/default_commands/introspection.rb +0 -410
  165. data/lib/pry/default_commands/ls.rb +0 -272
  166. data/lib/pry/default_commands/misc.rb +0 -38
  167. data/lib/pry/default_commands/navigating_pry.rb +0 -110
  168. data/lib/pry/default_commands/whereami.rb +0 -92
  169. data/lib/pry/extended_commands/experimental.rb +0 -7
  170. data/lib/pry/rbx_method.rb +0 -13
  171. data/man/pry.1 +0 -195
  172. data/man/pry.1.html +0 -204
  173. data/man/pry.1.ronn +0 -141
  174. data/pry.gemspec +0 -46
  175. data/test/candidate_helper1.rb +0 -11
  176. data/test/candidate_helper2.rb +0 -8
  177. data/test/helper.rb +0 -223
  178. data/test/test_cli.rb +0 -78
  179. data/test/test_code.rb +0 -201
  180. data/test/test_command.rb +0 -712
  181. data/test/test_command_helpers.rb +0 -9
  182. data/test/test_command_integration.rb +0 -668
  183. data/test/test_command_set.rb +0 -610
  184. data/test/test_completion.rb +0 -62
  185. data/test/test_control_d_handler.rb +0 -45
  186. data/test/test_default_commands/example.erb +0 -5
  187. data/test/test_default_commands/test_cd.rb +0 -318
  188. data/test/test_default_commands/test_context.rb +0 -280
  189. data/test/test_default_commands/test_documentation.rb +0 -314
  190. data/test/test_default_commands/test_find_method.rb +0 -50
  191. data/test/test_default_commands/test_gems.rb +0 -18
  192. data/test/test_default_commands/test_help.rb +0 -57
  193. data/test/test_default_commands/test_input.rb +0 -428
  194. data/test/test_default_commands/test_introspection.rb +0 -511
  195. data/test/test_default_commands/test_ls.rb +0 -151
  196. data/test/test_default_commands/test_shell.rb +0 -343
  197. data/test/test_default_commands/test_show_source.rb +0 -432
  198. data/test/test_exception_whitelist.rb +0 -21
  199. data/test/test_history_array.rb +0 -65
  200. data/test/test_hooks.rb +0 -521
  201. data/test/test_indent.rb +0 -277
  202. data/test/test_input_stack.rb +0 -86
  203. data/test/test_method.rb +0 -401
  204. data/test/test_pry.rb +0 -463
  205. data/test/test_pry_defaults.rb +0 -419
  206. data/test/test_pry_history.rb +0 -84
  207. data/test/test_pry_output.rb +0 -41
  208. data/test/test_sticky_locals.rb +0 -155
  209. data/test/test_syntax_checking.rb +0 -65
  210. data/test/test_wrapped_module.rb +0 -174
  211. data/test/testrc +0 -2
  212. data/test/testrcbad +0 -2
  213. data/wiki/Customizing-pry.md +0 -397
  214. data/wiki/Home.md +0 -4
@@ -4,14 +4,17 @@ class Pry
4
4
  # This class contains methods useful for extracting
5
5
  # documentation from methods and classes.
6
6
  module DocumentationHelpers
7
- def process_rdoc(comment, code_type)
7
+
8
+ module_function
9
+
10
+ def process_rdoc(comment)
8
11
  comment = comment.dup
9
- comment.gsub(/<code>(?:\s*\n)?(.*?)\s*<\/code>/m) { Pry.color ? CodeRay.scan($1, code_type).term : $1 }.
10
- gsub(/<em>(?:\s*\n)?(.*?)\s*<\/em>/m) { Pry.color ? "\e[1m#{$1}\e[0m": $1 }.
11
- gsub(/<i>(?:\s*\n)?(.*?)\s*<\/i>/m) { Pry.color ? "\e[1m#{$1}\e[0m" : $1 }.
12
- gsub(/\B\+(\w*?)\+\B/) { Pry.color ? "\e[32m#{$1}\e[0m": $1 }.
13
- gsub(/((?:^[ \t]+.+(?:\n+|\Z))+)/) { Pry.color ? CodeRay.scan($1, code_type).term : $1 }.
14
- gsub(/`(?:\s*\n)?([^\e]*?)\s*`/) { "`#{Pry.color ? CodeRay.scan($1, code_type).term : $1}`" }
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(/\B\+(\w+?)\+\B/) { "\e[32m#{$1}\e[0m" }.
16
+ gsub(/((?:^[ \t]+.+(?:\n+|\Z))+)/) { CodeRay.scan($1, :ruby).term }.
17
+ gsub(/`(?:\s*\n)?([^\e]*?)\s*`/) { "`#{CodeRay.scan($1, :ruby).term}`" }
15
18
  end
16
19
 
17
20
  def process_yardoc_tag(comment, tag)
@@ -33,11 +36,11 @@ class Pry
33
36
  yard_tags = ["param", "return", "option", "yield", "attr", "attr_reader", "attr_writer",
34
37
  "deprecate", "example", "raise"]
35
38
  (yard_tags - ["example"]).inject(comment) { |a, v| process_yardoc_tag(a, v) }.
36
- gsub(/^@(#{yard_tags.join("|")})/) { Pry.color ? "\e[33m#{$1}\e[0m": $1 }
39
+ gsub(/^@(#{yard_tags.join("|")})/) { "\e[33m#{$1}\e[0m" }
37
40
  end
38
41
 
39
- def process_comment_markup(comment, code_type)
40
- process_yardoc process_rdoc(comment, code_type)
42
+ def process_comment_markup(comment)
43
+ process_yardoc process_rdoc(comment)
41
44
  end
42
45
 
43
46
  # @param [String] code
@@ -46,10 +49,17 @@ class Pry
46
49
  code.sub(/\A\s*\/\*.*?\*\/\s*/m, '')
47
50
  end
48
51
 
52
+ # Given a string that makes up a comment in a source-code file parse out the content
53
+ # that the user is intended to read. (i.e. without leading indentation, #-characters
54
+ # or shebangs)
55
+ #
49
56
  # @param [String] comment
50
57
  # @return [String]
51
- def strip_leading_hash_and_whitespace_from_ruby_comments(comment)
58
+ def get_comment_content(comment)
52
59
  comment = comment.dup
60
+ # Remove #!/usr/bin/ruby
61
+ comment.gsub!(/\A\#!.*$/, '')
62
+ # Remove leading empty comment lines
53
63
  comment.gsub!(/\A\#+?$/, '')
54
64
  comment.gsub!(/^\s*#/, '')
55
65
  strip_leading_whitespace(comment)
@@ -0,0 +1,109 @@
1
+ class Pry
2
+ module Helpers
3
+ def self.tablify_or_one_line(heading, things)
4
+ plain_heading = Pry::Helpers::Text.strip_color(heading)
5
+ attempt = Table.new(things, :column_count => things.size)
6
+ if attempt.fits_on_line?(Terminal.width! - plain_heading.size - 2)
7
+ "#{heading}: #{attempt}\n"
8
+ else
9
+ "#{heading}: \n#{tablify_to_screen_width(things, :indent => ' ')}\n"
10
+ end
11
+ end
12
+
13
+ def self.tablify_to_screen_width(things, options = {})
14
+ things = things.compact
15
+ if indent = options[:indent]
16
+ usable_width = Terminal.width! - indent.size
17
+ tablify(things, usable_width).to_s.gsub(/^/, indent)
18
+ else
19
+ tablify(things, Terminal.width!).to_s
20
+ end
21
+ end
22
+
23
+ def self.tablify(things, line_length)
24
+ table = Table.new(things, :column_count => things.size)
25
+ table.column_count -= 1 until 1 == table.column_count or
26
+ table.fits_on_line?(line_length)
27
+ table
28
+ end
29
+
30
+ class Table
31
+ attr_reader :items, :column_count
32
+ def initialize items, args = {}
33
+ @column_count = args[:column_count]
34
+ self.items = items
35
+ end
36
+
37
+ def to_s
38
+ rows_to_s.join("\n")
39
+ end
40
+
41
+ def rows_to_s style = :color_on
42
+ widths = columns.map{|e| _max_width(e)}
43
+ @rows_without_colors.map do |r|
44
+ padded = []
45
+ r.each_with_index do |e,i|
46
+ next unless e
47
+ item = e.ljust(widths[i])
48
+ item.sub! e, _recall_color_for(e) if :color_on == style
49
+ padded << item
50
+ end
51
+ padded.join(Pry.config.ls.separator)
52
+ end
53
+ end
54
+
55
+ def items= items
56
+ @items = items
57
+ _rebuild_colorless_cache
58
+ _recolumn
59
+ items
60
+ end
61
+
62
+ def column_count= n
63
+ @column_count = n
64
+ _recolumn
65
+ end
66
+
67
+ def fits_on_line? line_length
68
+ _max_width(rows_to_s :no_color) <= line_length
69
+ end
70
+
71
+ def columns
72
+ @rows_without_colors.transpose
73
+ end
74
+
75
+ def ==(other); items == other.to_a end
76
+ def to_a; items.to_a end
77
+
78
+ private
79
+ def _max_width(things)
80
+ things.compact.map(&:size).max || 0
81
+ end
82
+
83
+ def _rebuild_colorless_cache
84
+ @colorless_cache = {}
85
+ @plain_items = []
86
+ items.map do |e|
87
+ plain = Pry::Helpers::Text.strip_color(e)
88
+ @colorless_cache[plain] = e
89
+ @plain_items << plain
90
+ end
91
+ end
92
+
93
+ def _recolumn
94
+ @rows_without_colors = []
95
+ return if items.size.zero?
96
+ row_count = (items.size.to_f/column_count).ceil
97
+ 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]}
100
+ end
101
+ end
102
+
103
+ def _recall_color_for thing
104
+ @colorless_cache[thing]
105
+ end
106
+ end
107
+
108
+ end
109
+ end
@@ -21,11 +21,11 @@ class Pry
21
21
 
22
22
  COLORS.each_pair do |color, value|
23
23
  define_method color do |text|
24
- Pry.color ? "\033[0;#{30+value}m#{text}\033[0m" : text.to_s
24
+ "\033[0;#{30+value}m#{text}\033[0m"
25
25
  end
26
26
 
27
27
  define_method "bright_#{color}" do |text|
28
- Pry.color ? "\033[1;#{30+value}m#{text}\033[0m" : text.to_s
28
+ "\033[1;#{30+value}m#{text}\033[0m"
29
29
  end
30
30
  end
31
31
 
@@ -33,17 +33,16 @@ class Pry
33
33
  #
34
34
  # @param [String, #to_s] text
35
35
  # @return [String] _text_ stripped of any color codes.
36
- def strip_color text
36
+ def strip_color(text)
37
37
  text.to_s.gsub(/\e\[.*?(\d)+m/ , '')
38
38
  end
39
39
 
40
40
  # Returns _text_ as bold text for use on a terminal.
41
- # _Pry.color_ must be true for this method to perform any transformations.
42
41
  #
43
42
  # @param [String, #to_s] text
44
43
  # @return [String] _text_
45
- def bold text
46
- Pry.color ? "\e[1m#{text}\e[0m" : text.to_s
44
+ def bold(text)
45
+ "\e[1m#{text}\e[0m"
47
46
  end
48
47
 
49
48
  # Returns `text` in the default foreground colour.
@@ -56,10 +55,10 @@ class Pry
56
55
  end
57
56
  alias_method :bright_default, :bold
58
57
 
59
- # Executes the block with `Pry.color` set to false.
58
+ # Executes the block with `Pry.config.color` set to false.
60
59
  # @yield
61
60
  # @return [void]
62
- def no_color &block
61
+ def no_color(&block)
63
62
  boolean = Pry.config.color
64
63
  Pry.config.color = false
65
64
  yield
@@ -70,7 +69,7 @@ class Pry
70
69
  # Executes the block with `Pry.config.pager` set to false.
71
70
  # @yield
72
71
  # @return [void]
73
- def no_pager &block
72
+ def no_pager(&block)
74
73
  boolean = Pry.config.pager
75
74
  Pry.config.pager = false
76
75
  yield
@@ -1,21 +1,33 @@
1
1
  class Pry
2
- # The History class is responsible for maintaining the user's input history, both
3
- # internally and within Readline.
2
+ # The History class is responsible for maintaining the user's input history,
3
+ # both internally and within Readline.
4
4
  class History
5
5
  attr_accessor :loader, :saver, :pusher, :clearer
6
6
 
7
- def initialize
7
+ # @return [Fixnum] Number of lines in history when Pry first loaded.
8
+ attr_reader :original_lines
9
+
10
+ def initialize(options={})
8
11
  @history = []
9
- @saved_lines = 0
12
+ @original_lines = 0
13
+ @file_path = options[:file_path]
10
14
  restore_default_behavior
11
15
  end
12
16
 
13
17
  # Assign the default methods for loading, saving, pushing, and clearing.
14
18
  def restore_default_behavior
15
- @loader = method(:read_from_file)
16
- @saver = method(:write_to_file)
17
- @pusher = method(:push_to_readline)
18
- @clearer = method(:clear_readline)
19
+ Pry.config.input # force Readline to load if applicable
20
+
21
+ @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
19
31
  end
20
32
 
21
33
  # Load the input history using `History.loader`.
@@ -24,17 +36,8 @@ class Pry
24
36
  @loader.call do |line|
25
37
  @pusher.call(line.chomp)
26
38
  @history << line.chomp
39
+ @original_lines += 1
27
40
  end
28
- @saved_lines = @history.length
29
- end
30
-
31
- # Write this session's history using `History.saver`.
32
- # @return [Integer] The number of lines saved
33
- def save
34
- history_to_save = @history[@saved_lines..-1]
35
- @saver.call(history_to_save)
36
- @saved_lines = @history.length
37
- history_to_save.length
38
41
  end
39
42
 
40
43
  # Add a line to the input history, ignoring blank and duplicate lines.
@@ -44,18 +47,27 @@ class Pry
44
47
  unless line.empty? || (@history.last && line == @history.last)
45
48
  @pusher.call(line)
46
49
  @history << line
50
+ @saver.call(line) if Pry.config.history.should_save
47
51
  end
48
52
  line
49
53
  end
50
54
  alias << push
51
55
 
52
- # Clear all history. Anything the user entered before this point won't be
53
- # saved, but anything they put in afterwards will still be appended to the
54
- # history file on exit.
56
+ # Clear this session's history. This won't affect the contents of the
57
+ # history file.
55
58
  def clear
56
59
  @clearer.call
57
60
  @history = []
58
- @saved_lines = 0
61
+ end
62
+
63
+ # @return [Fixnum] The number of lines in history.
64
+ def history_line_count
65
+ @history.count
66
+ end
67
+
68
+ # @return [Fixnum] The number of lines in history from just this session.
69
+ def session_line_count
70
+ @history.count - @original_lines
59
71
  end
60
72
 
61
73
  # Return an Array containing all stored history.
@@ -66,33 +78,16 @@ class Pry
66
78
  end
67
79
 
68
80
  private
81
+
69
82
  # The default loader. Yields lines from `Pry.history.config.file`.
70
83
  def read_from_file
71
- begin
72
- history_file = File.expand_path(Pry.config.history.file)
73
- if File.exists?(history_file)
74
- File.foreach(history_file) { |line| yield(line) }
75
- end
76
- rescue => error
77
- unless error.message.empty?
78
- warn "History file not loaded, received an error: #{error.message}"
79
- end
80
- end
81
- end
84
+ filename = File.expand_path(Pry.config.history.file)
82
85
 
83
- # The default saver. Appends the given lines to `Pry.history.config.file`.
84
- # @param [Array<String>] lines
85
- def write_to_file(lines)
86
- history_file = File.expand_path(Pry.config.history.file)
87
-
88
- begin
89
- File.open(history_file, 'a') do |f|
90
- lines.each { |ln| f.puts ln }
91
- end
92
- rescue Errno::EACCES
93
- # We should probably create an option Pry.show_warnings?!?!?!
94
- warn 'Unable to write to your history file, history not saved'
86
+ if File.exists?(filename)
87
+ File.foreach(filename) { |line| yield(line) }
95
88
  end
89
+ rescue => error
90
+ warn "History file not loaded: #{error.message}"
96
91
  end
97
92
 
98
93
  # The default pusher. Appends the given line to Readline::HISTORY.
@@ -105,5 +100,26 @@ class Pry
105
100
  def clear_readline
106
101
  Readline::HISTORY.shift until Readline::HISTORY.empty?
107
102
  end
103
+
104
+ # The default saver. Appends the given line to `Pry.history.config.file`.
105
+ def save_to_file(line)
106
+ history_file.puts line if history_file
107
+ end
108
+
109
+ # The history file, opened for appending.
110
+ def history_file
111
+ if defined?(@history_file)
112
+ @history_file
113
+ else
114
+ @history_file = File.open(file_path, 'a', 0600).tap { |f| f.sync = true }
115
+ end
116
+ rescue Errno::EACCES
117
+ warn 'History not saved; unable to open your history file for writing.'
118
+ @history_file = false
119
+ end
120
+
121
+ def file_path
122
+ @file_path || Pry.config.history.file
123
+ end
108
124
  end
109
125
  end
@@ -1,6 +1,6 @@
1
1
  class Pry
2
2
  # A history array is an array to which you can only add elements. Older
3
- # entries are removed progressively, so that the aray never contains more than
3
+ # entries are removed progressively, so that the array never contains more than
4
4
  # N elements.
5
5
  #
6
6
  # History arrays are used by Pry to store the output of the last commands.
@@ -89,6 +89,16 @@ class Pry
89
89
  ((@count - size)...@count).map { |n| @hash[n] }
90
90
  end
91
91
 
92
+ # Returns [Hash] copy of the internal @hash history
93
+ def to_h
94
+ @hash.dup
95
+ end
96
+
97
+ def pop!
98
+ @hash.delete @count - 1
99
+ @count -= 1
100
+ end
101
+
92
102
  def inspect
93
103
  "#<#{self.class} size=#{size} first=#{@count - size} max_size=#{max_size}>"
94
104
  end
@@ -18,11 +18,11 @@ class Pry
18
18
  # @param [Hash] hash The hash to convert to `Pry::Hooks`.
19
19
  # @return [Pry::Hooks] The resulting `Pry::Hooks` instance.
20
20
  def self.from_hash(hash)
21
+ return hash if hash.instance_of?(self)
21
22
  instance = new
22
23
  hash.each do |k, v|
23
24
  instance.add_hook(k, nil, v)
24
25
  end
25
-
26
26
  instance
27
27
  end
28
28
 
@@ -49,22 +49,6 @@ class Pry
49
49
  @errors ||= []
50
50
  end
51
51
 
52
- # FIXME:
53
- # This is a hack to alert people of the new API.
54
- def [](event_name)
55
- warn "`Pry.hooks[]` is deprecated! Please use the new `Pry::Hooks` API! http://rubydoc.info/github/pry/pry/master/Pry/Hooks"
56
-
57
- get_hook(event_name, nil)
58
- end
59
-
60
- # FIXME:
61
- # This is a hack to alert people of the new API.
62
- def []=(event_name, callable)
63
- warn "`Pry.hooks[]=` is deprecated! Please use the new `Pry::Hooks` API! http://rubydoc.info/github/pry/pry/master/Pry/Hooks"
64
-
65
- add_hook(event_name, nil, callable)
66
- end
67
-
68
52
  # Destructively merge the contents of two `Pry:Hooks` instances.
69
53
  # @param [Pry::Hooks] other The `Pry::Hooks` instance to merge
70
54
  # @return [Pry:Hooks] Returns the receiver.
@@ -146,18 +130,14 @@ class Pry
146
130
  def exec_hook(event_name, *args, &block)
147
131
  @hooks[event_name] ||= []
148
132
 
149
- # silence warnings to get rid of 1.8's "warning: multiple values
150
- # for a block parameter" warnings
151
- Pry::Helpers::BaseHelpers.silence_warnings do
152
- @hooks[event_name].map do |hook_name, callable|
153
- begin
154
- callable.call(*args, &block)
155
- rescue RescuableException => e
156
- errors << e
157
- e
158
- end
159
- end.last
160
- end
133
+ @hooks[event_name].map do |hook_name, callable|
134
+ begin
135
+ callable.call(*args, &block)
136
+ rescue RescuableException => e
137
+ errors << e
138
+ e
139
+ end
140
+ end.last
161
141
  end
162
142
 
163
143
  # Return the number of hook functions registered for the `event_name` event.
@@ -244,9 +224,7 @@ class Pry
244
224
  # @param [Symbol] hook_name Name of the hook.
245
225
  # @return [Boolean] Whether the hook by the name `hook_name`
246
226
  def hook_exists?(event_name, hook_name)
247
- !!@hooks[event_name].find { |name, _| name == hook_name }
227
+ !!(@hooks[event_name] && @hooks[event_name].find { |name, _| name == hook_name })
248
228
  end
249
- private :hook_exists?
250
-
251
229
  end
252
230
  end