pry 0.9.12.2 → 0.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (237) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1141 -0
  3. data/LICENSE +2 -2
  4. data/README.md +466 -0
  5. data/bin/pry +4 -7
  6. data/lib/pry/basic_object.rb +10 -0
  7. data/lib/pry/block_command.rb +22 -0
  8. data/lib/pry/class_command.rb +194 -0
  9. data/lib/pry/cli.rb +97 -92
  10. data/lib/pry/code/code_file.rb +114 -0
  11. data/lib/pry/code/code_range.rb +7 -4
  12. data/lib/pry/code/loc.rb +27 -14
  13. data/lib/pry/code.rb +62 -90
  14. data/lib/pry/code_object.rb +83 -39
  15. data/lib/pry/color_printer.rb +66 -0
  16. data/lib/pry/command.rb +202 -371
  17. data/lib/pry/command_set.rb +151 -133
  18. data/lib/pry/command_state.rb +31 -0
  19. data/lib/pry/commands/amend_line.rb +86 -82
  20. data/lib/pry/commands/bang.rb +18 -14
  21. data/lib/pry/commands/bang_pry.rb +15 -11
  22. data/lib/pry/commands/cat/abstract_formatter.rb +23 -18
  23. data/lib/pry/commands/cat/exception_formatter.rb +85 -73
  24. data/lib/pry/commands/cat/file_formatter.rb +56 -63
  25. data/lib/pry/commands/cat/input_expression_formatter.rb +35 -30
  26. data/lib/pry/commands/cat.rb +64 -47
  27. data/lib/pry/commands/cd.rb +42 -26
  28. data/lib/pry/commands/change_inspector.rb +34 -0
  29. data/lib/pry/commands/change_prompt.rb +51 -0
  30. data/lib/pry/commands/clear_screen.rb +20 -0
  31. data/lib/pry/commands/code_collector.rb +148 -131
  32. data/lib/pry/commands/disable_pry.rb +23 -19
  33. data/lib/pry/commands/easter_eggs.rb +23 -34
  34. data/lib/pry/commands/edit/exception_patcher.rb +21 -17
  35. data/lib/pry/commands/edit/file_and_line_locator.rb +33 -24
  36. data/lib/pry/commands/edit.rb +183 -167
  37. data/lib/pry/commands/exit.rb +40 -35
  38. data/lib/pry/commands/exit_all.rb +24 -20
  39. data/lib/pry/commands/exit_program.rb +20 -17
  40. data/lib/pry/commands/find_method.rb +167 -167
  41. data/lib/pry/commands/fix_indent.rb +16 -12
  42. data/lib/pry/commands/help.rb +140 -133
  43. data/lib/pry/commands/hist.rb +153 -132
  44. data/lib/pry/commands/import_set.rb +20 -15
  45. data/lib/pry/commands/jump_to.rb +25 -21
  46. data/lib/pry/commands/list_inspectors.rb +42 -0
  47. data/lib/pry/commands/ls/constants.rb +75 -0
  48. data/lib/pry/commands/ls/formatter.rb +55 -0
  49. data/lib/pry/commands/ls/globals.rb +50 -0
  50. data/lib/pry/commands/ls/grep.rb +23 -0
  51. data/lib/pry/commands/ls/instance_vars.rb +40 -0
  52. data/lib/pry/commands/ls/interrogatable.rb +24 -0
  53. data/lib/pry/commands/ls/jruby_hacks.rb +55 -0
  54. data/lib/pry/commands/ls/local_names.rb +37 -0
  55. data/lib/pry/commands/ls/local_vars.rb +47 -0
  56. data/lib/pry/commands/ls/ls_entity.rb +65 -0
  57. data/lib/pry/commands/ls/methods.rb +55 -0
  58. data/lib/pry/commands/ls/methods_helper.rb +50 -0
  59. data/lib/pry/commands/ls/self_methods.rb +34 -0
  60. data/lib/pry/commands/ls.rb +100 -303
  61. data/lib/pry/commands/nesting.rb +21 -17
  62. data/lib/pry/commands/play.rb +93 -49
  63. data/lib/pry/commands/pry_backtrace.rb +22 -18
  64. data/lib/pry/commands/pry_version.rb +15 -11
  65. data/lib/pry/commands/raise_up.rb +33 -27
  66. data/lib/pry/commands/reload_code.rb +57 -48
  67. data/lib/pry/commands/reset.rb +16 -12
  68. data/lib/pry/commands/ri.rb +57 -38
  69. data/lib/pry/commands/save_file.rb +45 -43
  70. data/lib/pry/commands/shell_command.rb +66 -34
  71. data/lib/pry/commands/shell_mode.rb +22 -20
  72. data/lib/pry/commands/show_doc.rb +80 -65
  73. data/lib/pry/commands/show_info.rb +193 -159
  74. data/lib/pry/commands/show_input.rb +16 -11
  75. data/lib/pry/commands/show_source.rb +113 -33
  76. data/lib/pry/commands/stat.rb +35 -31
  77. data/lib/pry/commands/switch_to.rb +21 -15
  78. data/lib/pry/commands/toggle_color.rb +21 -13
  79. data/lib/pry/commands/watch_expression/expression.rb +43 -0
  80. data/lib/pry/commands/watch_expression.rb +110 -0
  81. data/lib/pry/commands/whereami.rb +157 -134
  82. data/lib/pry/commands/wtf.rb +78 -40
  83. data/lib/pry/config/attributable.rb +22 -0
  84. data/lib/pry/config/lazy_value.rb +29 -0
  85. data/lib/pry/config/memoized_value.rb +34 -0
  86. data/lib/pry/config/value.rb +24 -0
  87. data/lib/pry/config.rb +290 -220
  88. data/lib/pry/control_d_handler.rb +28 -0
  89. data/lib/pry/core_extensions.rb +50 -27
  90. data/lib/pry/editor.rb +130 -102
  91. data/lib/pry/env.rb +18 -0
  92. data/lib/pry/exception_handler.rb +43 -0
  93. data/lib/pry/exceptions.rb +73 -0
  94. data/lib/pry/forwardable.rb +27 -0
  95. data/lib/pry/helpers/base_helpers.rb +22 -151
  96. data/lib/pry/helpers/command_helpers.rb +55 -63
  97. data/lib/pry/helpers/documentation_helpers.rb +21 -13
  98. data/lib/pry/helpers/options_helpers.rb +15 -8
  99. data/lib/pry/helpers/platform.rb +55 -0
  100. data/lib/pry/helpers/table.rb +44 -32
  101. data/lib/pry/helpers/text.rb +96 -86
  102. data/lib/pry/helpers.rb +3 -0
  103. data/lib/pry/history.rb +101 -70
  104. data/lib/pry/hooks.rb +67 -137
  105. data/lib/pry/indent.rb +79 -73
  106. data/lib/pry/input_completer.rb +283 -0
  107. data/lib/pry/input_lock.rb +129 -0
  108. data/lib/pry/inspector.rb +39 -0
  109. data/lib/pry/last_exception.rb +61 -0
  110. data/lib/pry/method/disowned.rb +19 -5
  111. data/lib/pry/{commands/edit/method_patcher.rb → method/patcher.rb} +51 -42
  112. data/lib/pry/method/weird_method_locator.rb +80 -44
  113. data/lib/pry/method.rb +225 -176
  114. data/lib/pry/object_path.rb +91 -0
  115. data/lib/pry/output.rb +136 -0
  116. data/lib/pry/pager.rb +227 -68
  117. data/lib/pry/prompt.rb +214 -0
  118. data/lib/pry/pry_class.rb +216 -289
  119. data/lib/pry/pry_instance.rb +438 -500
  120. data/lib/pry/repl.rb +256 -0
  121. data/lib/pry/repl_file_loader.rb +34 -35
  122. data/lib/pry/ring.rb +89 -0
  123. data/lib/pry/slop/LICENSE +20 -0
  124. data/lib/pry/slop/commands.rb +190 -0
  125. data/lib/pry/slop/option.rb +210 -0
  126. data/lib/pry/slop.rb +672 -0
  127. data/lib/pry/syntax_highlighter.rb +26 -0
  128. data/lib/pry/system_command_handler.rb +17 -0
  129. data/lib/pry/testable/evalable.rb +24 -0
  130. data/lib/pry/testable/mockable.rb +22 -0
  131. data/lib/pry/testable/pry_tester.rb +88 -0
  132. data/lib/pry/testable/utility.rb +34 -0
  133. data/lib/pry/testable/variables.rb +52 -0
  134. data/lib/pry/testable.rb +68 -0
  135. data/lib/pry/version.rb +3 -1
  136. data/lib/pry/warning.rb +20 -0
  137. data/lib/pry/{module_candidate.rb → wrapped_module/candidate.rb} +36 -43
  138. data/lib/pry/wrapped_module.rb +102 -103
  139. data/lib/pry.rb +135 -261
  140. metadata +94 -283
  141. data/.document +0 -2
  142. data/.gitignore +0 -16
  143. data/.travis.yml +0 -21
  144. data/.yardopts +0 -1
  145. data/CHANGELOG +0 -534
  146. data/CONTRIBUTORS +0 -55
  147. data/Gemfile +0 -9
  148. data/Guardfile +0 -62
  149. data/README.markdown +0 -400
  150. data/Rakefile +0 -140
  151. data/TODO +0 -117
  152. data/lib/pry/commands/disabled_commands.rb +0 -2
  153. data/lib/pry/commands/gem_cd.rb +0 -26
  154. data/lib/pry/commands/gem_install.rb +0 -29
  155. data/lib/pry/commands/gem_list.rb +0 -33
  156. data/lib/pry/commands/gem_open.rb +0 -29
  157. data/lib/pry/commands/gist.rb +0 -102
  158. data/lib/pry/commands/install_command.rb +0 -51
  159. data/lib/pry/commands/simple_prompt.rb +0 -22
  160. data/lib/pry/commands.rb +0 -6
  161. data/lib/pry/completion.rb +0 -304
  162. data/lib/pry/custom_completions.rb +0 -6
  163. data/lib/pry/history_array.rb +0 -116
  164. data/lib/pry/plugins.rb +0 -103
  165. data/lib/pry/rbx_method.rb +0 -13
  166. data/lib/pry/rbx_path.rb +0 -22
  167. data/lib/pry/rubygem.rb +0 -74
  168. data/lib/pry/terminal.rb +0 -78
  169. data/lib/pry/test/helper.rb +0 -185
  170. data/man/pry.1 +0 -195
  171. data/man/pry.1.html +0 -204
  172. data/man/pry.1.ronn +0 -141
  173. data/pry.gemspec +0 -30
  174. data/spec/Procfile +0 -3
  175. data/spec/cli_spec.rb +0 -78
  176. data/spec/code_object_spec.rb +0 -277
  177. data/spec/code_spec.rb +0 -219
  178. data/spec/command_helpers_spec.rb +0 -29
  179. data/spec/command_integration_spec.rb +0 -644
  180. data/spec/command_set_spec.rb +0 -627
  181. data/spec/command_spec.rb +0 -821
  182. data/spec/commands/amend_line_spec.rb +0 -247
  183. data/spec/commands/bang_spec.rb +0 -19
  184. data/spec/commands/cat_spec.rb +0 -164
  185. data/spec/commands/cd_spec.rb +0 -250
  186. data/spec/commands/disable_pry_spec.rb +0 -25
  187. data/spec/commands/edit_spec.rb +0 -727
  188. data/spec/commands/exit_all_spec.rb +0 -34
  189. data/spec/commands/exit_program_spec.rb +0 -19
  190. data/spec/commands/exit_spec.rb +0 -34
  191. data/spec/commands/find_method_spec.rb +0 -70
  192. data/spec/commands/gem_list_spec.rb +0 -26
  193. data/spec/commands/gist_spec.rb +0 -79
  194. data/spec/commands/help_spec.rb +0 -56
  195. data/spec/commands/hist_spec.rb +0 -181
  196. data/spec/commands/jump_to_spec.rb +0 -15
  197. data/spec/commands/ls_spec.rb +0 -181
  198. data/spec/commands/play_spec.rb +0 -140
  199. data/spec/commands/raise_up_spec.rb +0 -56
  200. data/spec/commands/save_file_spec.rb +0 -177
  201. data/spec/commands/show_doc_spec.rb +0 -510
  202. data/spec/commands/show_input_spec.rb +0 -17
  203. data/spec/commands/show_source_spec.rb +0 -782
  204. data/spec/commands/whereami_spec.rb +0 -203
  205. data/spec/completion_spec.rb +0 -239
  206. data/spec/control_d_handler_spec.rb +0 -58
  207. data/spec/documentation_helper_spec.rb +0 -73
  208. data/spec/editor_spec.rb +0 -79
  209. data/spec/exception_whitelist_spec.rb +0 -21
  210. data/spec/fixtures/candidate_helper1.rb +0 -11
  211. data/spec/fixtures/candidate_helper2.rb +0 -8
  212. data/spec/fixtures/example.erb +0 -5
  213. data/spec/fixtures/example_nesting.rb +0 -33
  214. data/spec/fixtures/show_source_doc_examples.rb +0 -15
  215. data/spec/fixtures/testrc +0 -2
  216. data/spec/fixtures/testrcbad +0 -2
  217. data/spec/fixtures/whereami_helper.rb +0 -6
  218. data/spec/helper.rb +0 -34
  219. data/spec/helpers/bacon.rb +0 -86
  220. data/spec/helpers/mock_pry.rb +0 -43
  221. data/spec/helpers/table_spec.rb +0 -105
  222. data/spec/history_array_spec.rb +0 -67
  223. data/spec/hooks_spec.rb +0 -522
  224. data/spec/indent_spec.rb +0 -301
  225. data/spec/input_stack_spec.rb +0 -90
  226. data/spec/method_spec.rb +0 -482
  227. data/spec/prompt_spec.rb +0 -60
  228. data/spec/pry_defaults_spec.rb +0 -419
  229. data/spec/pry_history_spec.rb +0 -99
  230. data/spec/pry_output_spec.rb +0 -95
  231. data/spec/pry_spec.rb +0 -504
  232. data/spec/run_command_spec.rb +0 -25
  233. data/spec/sticky_locals_spec.rb +0 -157
  234. data/spec/syntax_checking_spec.rb +0 -81
  235. data/spec/wrapped_module_spec.rb +0 -261
  236. data/wiki/Customizing-pry.md +0 -397
  237. data/wiki/Home.md +0 -4
@@ -1,164 +1,171 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
- class Command::Help < Pry::ClassCommand
3
- match 'help'
4
- group 'Help'
5
- description 'Show a list of commands or information about a specific command'
6
-
7
- banner <<-'BANNER'
8
- Usage: help [COMMAND]
9
-
10
- With no arguments, help lists all the available commands in the current
11
- command-set along with their description. When given a command name as an
12
- argument, shows the help for that command.
13
- BANNER
14
-
15
- # We only want to show commands that have descriptions, so that the
16
- # easter eggs don't show up.
17
- def visible_commands
18
- visible = {}
19
- commands.each do |key, command|
20
- visible[key] = command if command.description && !command.description.empty?
4
+ class Command
5
+ class Help < Pry::ClassCommand
6
+ match 'help'
7
+ group 'Help'
8
+ description 'Show a list of commands or information about a specific command.'
9
+
10
+ banner <<-'BANNER'
11
+ Usage: help [COMMAND]
12
+
13
+ With no arguments, help lists all the available commands along with their
14
+ descriptions. When given a command name as an argument, shows the help
15
+ for that command.
16
+ BANNER
17
+
18
+ # We only want to show commands that have descriptions, so that the
19
+ # easter eggs don't show up.
20
+ def visible_commands
21
+ visible = {}
22
+ commands.each do |key, command|
23
+ visible[key] = command if command.description && !command.description.empty?
24
+ end
25
+ visible
21
26
  end
22
- visible
23
- end
24
27
 
25
- # Get a hash of available commands grouped by the "group" name.
26
- def command_groups
27
- visible_commands.values.group_by(&:group)
28
- end
28
+ # Get a hash of available commands grouped by the "group" name.
29
+ def command_groups
30
+ visible_commands.values.group_by(&:group)
31
+ end
29
32
 
30
- def process
31
- if args.empty?
32
- display_index(command_groups)
33
- else
34
- display_search(args.first)
33
+ def process
34
+ if args.empty?
35
+ display_index(command_groups)
36
+ else
37
+ display_search(args.first)
38
+ end
35
39
  end
36
- end
37
40
 
38
- # Display the index view, with headings and short descriptions per command.
39
- #
40
- # @param Hash[String => Array[Commands]]
41
- def display_index(groups)
42
- help_text = []
41
+ # Display the index view, with headings and short descriptions per command.
42
+ #
43
+ # @param [Hash<String, Array<Commands>>] groups
44
+ def display_index(groups)
45
+ help_text = []
43
46
 
44
- sorted_group_names(groups).each do |group_name|
45
- commands = sorted_commands(groups[group_name])
47
+ sorted_group_names(groups).each do |group_name|
48
+ commands = sorted_commands(groups[group_name])
46
49
 
47
- if commands.any?
48
- help_text << help_text_for_commands(group_name, commands)
50
+ help_text << help_text_for_commands(group_name, commands) if commands.any?
49
51
  end
52
+
53
+ pry_instance.pager.page help_text.join("\n\n")
50
54
  end
51
55
 
52
- stagger_output(help_text.join("\n\n"))
53
- end
56
+ # Given a group name and an array of commands,
57
+ # return the help string for those commands.
58
+ #
59
+ # @param [String] name The group name.
60
+ # @param [Array<Pry::Command>] commands
61
+ # @return [String] The generated help string.
62
+ def help_text_for_commands(name, commands)
63
+ "#{bold(name.capitalize)}\n" + commands.map do |command|
64
+ " #{command.options[:listing].to_s.ljust(18)} " \
65
+ "#{command.description.capitalize}"
66
+ end.join("\n")
67
+ end
54
68
 
55
- # Given a group name and an array of commands,
56
- # return the help string for those commands.
57
- #
58
- # @param [String] name The group name.
59
- # @param [Array<Pry::Command>]] commands
60
- # @return [String] The generated help string.
61
- def help_text_for_commands(name, commands)
62
- "#{text.bold(name)}\n" + commands.map do |command|
63
- " #{command.options[:listing].to_s.ljust(18)} #{command.description}"
64
- end.join("\n")
65
- end
69
+ # @param [Hash] groups
70
+ # @return [Array<String>] An array of sorted group names.
71
+ def sorted_group_names(groups)
72
+ groups.keys.sort_by(&method(:group_sort_key))
73
+ end
66
74
 
67
- # @param [Hash] groups
68
- # @return [Array<String>] An array of sorted group names.
69
- def sorted_group_names(groups)
70
- groups.keys.sort_by(&method(:group_sort_key))
71
- end
75
+ # Sort an array of commands by their `listing` name.
76
+ #
77
+ # @param [Array<Pry::Command>] commands The commands to sort
78
+ # @return [Array<Pry::Command>] commands sorted by listing name.
79
+ def sorted_commands(commands)
80
+ commands.sort_by { |command| command.options[:listing].to_s }
81
+ end
72
82
 
73
- # Sort an array of commands by their `listing` name.
74
- #
75
- # @param [Array<Pry::Command>] commands The commands to sort
76
- # @return [Array<Pry::Command>] commands sorted by listing name.
77
- def sorted_commands(commands)
78
- commands.sort_by{ |command| command.options[:listing].to_s }
79
- end
83
+ # Display help for an individual command or group.
84
+ #
85
+ # @param [String] search The string to search for.
86
+ def display_search(search)
87
+ if (command = command_set.find_command_for_help(search))
88
+ display_command(command)
89
+ else
90
+ display_filtered_search_results(search)
91
+ end
92
+ end
80
93
 
81
- # Display help for an individual command or group.
82
- #
83
- # @param [String] search The string to search for.
84
- def display_search(search)
85
- if command = command_set.find_command_for_help(search)
86
- display_command(command)
87
- else
88
- display_filtered_search_results(search)
94
+ # Display help for a searched item, filtered first by group
95
+ # and if that fails, filtered by command name.
96
+ #
97
+ # @param [String] search The string to search for.
98
+ def display_filtered_search_results(search)
99
+ groups = search_hash(search, command_groups)
100
+
101
+ if !groups.empty?
102
+ display_index(groups)
103
+ else
104
+ display_filtered_commands(search)
105
+ end
89
106
  end
90
- end
91
107
 
92
- # Display help for a searched item, filtered first by group
93
- # and if that fails, filtered by command name.
94
- #
95
- # @param [String] search The string to search for.
96
- def display_filtered_search_results(search)
97
- groups = search_hash(search, command_groups)
98
-
99
- if groups.size > 0
100
- display_index(groups)
101
- else
102
- display_filtered_commands(search)
108
+ # Display help for a searched item, filtered by group
109
+ #
110
+ # @param [String] search The string to search for.
111
+ def display_filtered_commands(search)
112
+ filtered = search_hash(search, visible_commands)
113
+ raise CommandError, "No help found for '#{args.first}'" if filtered.empty?
114
+
115
+ if filtered.size == 1
116
+ display_command(filtered.values.first)
117
+ else
118
+ display_index("'#{search}' commands" => filtered.values)
119
+ end
103
120
  end
104
- end
105
121
 
106
- # Display help for a searched item, filtered by group
107
- #
108
- # @param [String] search The string to search for.
109
- def display_filtered_commands(search)
110
- filtered = search_hash(search, visible_commands)
111
- raise CommandError, "No help found for '#{args.first}'" if filtered.empty?
112
-
113
- if filtered.size == 1
114
- display_command(filtered.values.first)
115
- else
116
- display_index({"'#{search}' commands" => filtered.values})
122
+ # Display help for an individual command.
123
+ #
124
+ # @param [Pry::Command] command
125
+ def display_command(command)
126
+ pry_instance.pager.page command.new.help
117
127
  end
118
- end
119
128
 
120
- # Display help for an individual command.
121
- #
122
- # @param [Pry::Command] command
123
- def display_command(command)
124
- stagger_output command.new.help
125
- end
129
+ # Find a subset of a hash that matches the user's search term.
130
+ #
131
+ # If there's an exact match a Hash of one element will be returned,
132
+ # otherwise a sub-Hash with every key that matches the search will
133
+ # be returned.
134
+ #
135
+ # @param [String] search the search term
136
+ # @param [Hash] hash the hash to search
137
+ def search_hash(search, hash)
138
+ matching = {}
139
+
140
+ hash.each_pair do |key, value|
141
+ next unless key.is_a?(String)
142
+ return { key => value } if normalize(key) == normalize(search)
143
+ next unless normalize(key).start_with?(normalize(search))
126
144
 
127
- # Find a subset of a hash that matches the user's search term.
128
- #
129
- # If there's an exact match a Hash of one element will be returned,
130
- # otherwise a sub-Hash with every key that matches the search will
131
- # be returned.
132
- #
133
- # @param [String] the search term
134
- # @param [Hash] the hash to search
135
- def search_hash(search, hash)
136
- matching = {}
137
-
138
- hash.each_pair do |key, value|
139
- next unless key.is_a?(String)
140
- if normalize(key) == normalize(search)
141
- return {key => value}
142
- elsif normalize(key).start_with?(normalize(search))
143
145
  matching[key] = value
144
146
  end
147
+
148
+ matching
145
149
  end
146
150
 
147
- matching
148
- end
151
+ # Clean search terms to make it easier to search group names
152
+ #
153
+ # @param [String] key
154
+ # @return [String]
155
+ def normalize(key)
156
+ key.downcase.gsub(/pry\W+/, '')
157
+ end
149
158
 
150
- # Clean search terms to make it easier to search group names
151
- #
152
- # @param String
153
- # @return String
154
- def normalize(key)
155
- key.downcase.gsub(/pry\W+/, '')
159
+ def group_sort_key(group_name)
160
+ [
161
+ %w[
162
+ Help Context Editing Introspection Input_and_output Navigating_pry
163
+ Gems Basic Commands
164
+ ].index(group_name.tr(' ', '_')) || 99, group_name
165
+ ]
166
+ end
156
167
  end
157
168
 
158
- def group_sort_key(group_name)
159
- [%w(Help Context Editing Introspection Input_and_output Navigating_pry Gems Basic Commands).index(group_name.gsub(' ', '_')) || 99, group_name]
160
- end
169
+ Pry::Commands.add_command(Pry::Command::Help)
161
170
  end
162
-
163
- Pry::Commands.add_command(Pry::Command::Help)
164
171
  end
@@ -1,161 +1,182 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
- class Command::Hist < Pry::ClassCommand
3
- match 'hist'
4
- group 'Editing'
5
- description 'Show and replay Readline history.'
6
-
7
- banner <<-'BANNER'
8
- Usage: hist [--head|--tail]
9
- hist --head N
10
- hist --tail N
11
- hist --show START..END
12
- hist --grep PATTERN
13
- hist --clear
14
- hist --replay START..END
15
- hist --save [START..END] FILE
16
- Aliases: history
17
-
18
- Show and replay Readline history.
19
- BANNER
20
-
21
- def options(opt)
22
- opt.on :H, :head, "Display the first N items", :optional_argument => true, :as => Integer
23
- opt.on :T, :tail, "Display the last N items", :optional_argument => true, :as => Integer
24
- opt.on :s, :show, "Show the given range of lines", :optional_argument => true, :as => Range
25
- opt.on :G, :grep, "Show lines matching the given pattern", :argument => true, :as => String
26
- opt.on :c, :clear , "Clear the current session's history"
27
- opt.on :r, :replay, "Replay a line or range of lines", :argument => true, :as => Range
28
- opt.on :save, "Save history to a file", :argument => true, :as => Range
29
- opt.on :e, :'exclude-pry', "Exclude Pry commands from the history"
30
- opt.on :n, :'no-numbers', "Omit line numbers"
31
- opt.on :f, :flood, "Do not use a pager to view text longer than one screen"
32
- end
4
+ class Command
5
+ class Hist < Pry::ClassCommand
6
+ match 'hist'
7
+ group 'Editing'
8
+ description 'Show and replay Readline history.'
9
+
10
+ banner <<-'BANNER'
11
+ Usage: hist [--head|--tail]
12
+ hist --all
13
+ hist --head N
14
+ hist --tail N
15
+ hist --show START..END
16
+ hist --grep PATTERN
17
+ hist --clear
18
+ hist --replay START..END
19
+ hist --save [START..END] FILE
20
+ Aliases: history
21
+
22
+ Show and replay Readline history.
23
+ BANNER
24
+
25
+ def options(opt)
26
+ opt.on :a, :all, "Display all history"
27
+ opt.on :H, :head, "Display the first N items",
28
+ optional_argument: true, as: Integer
29
+ opt.on :T, :tail, "Display the last N items",
30
+ optional_argument: true, as: Integer
31
+ opt.on :s, :show, "Show the given range of lines",
32
+ optional_argument: true, as: Range
33
+ opt.on :G, :grep, "Show lines matching the given pattern",
34
+ argument: true, as: String
35
+ opt.on :c, :clear, "Clear the current session's history"
36
+ opt.on :r, :replay, "Replay a line or range of lines",
37
+ argument: true, as: Range
38
+ opt.on :save, "Save history to a file", argument: true, as: Range
39
+ opt.on :e, :'exclude-pry', "Exclude Pry commands from the history"
40
+ opt.on :n, :'no-numbers', "Omit line numbers"
41
+ end
33
42
 
34
- def process
35
- @history = Pry::Code(Pry.history.to_a)
43
+ def process
44
+ @history = find_history
36
45
 
37
- if opts.present?(:show)
38
- @history = @history.between(opts[:show])
39
- end
46
+ @history = @history.between(opts[:show]) if opts.present?(:show)
40
47
 
41
- if opts.present?(:grep)
42
- @history = @history.grep(opts[:grep])
43
- end
48
+ @history = @history.grep(opts[:grep]) if opts.present?(:grep)
44
49
 
45
- @history = case
46
- when opts.present?(:head)
47
- @history.take_lines(1, opts[:head] || 10)
48
- when opts.present?(:tail)
49
- @history.take_lines(-(opts[:tail] || 10), opts[:tail] || 10)
50
- when opts.present?(:show)
51
- @history.between(opts[:show])
52
- else
53
- @history
50
+ @history =
51
+ if opts.present?(:head)
52
+ @history.take_lines(1, opts[:head] || 10)
53
+ elsif opts.present?(:tail)
54
+ @history.take_lines(-(opts[:tail] || 10), opts[:tail] || 10)
55
+ else
56
+ @history
57
+ end
58
+
59
+ if opts.present?(:'exclude-pry')
60
+ @history = @history.reject do |loc|
61
+ command_set.valid_command?(loc.line)
62
+ end
54
63
  end
55
64
 
56
- if opts.present?(:'exclude-pry')
57
- @history = @history.select { |l, ln| !command_set.valid_command?(l) }
65
+ if opts.present?(:save)
66
+ process_save
67
+ elsif opts.present?(:clear)
68
+ process_clear
69
+ elsif opts.present?(:replay)
70
+ process_replay
71
+ else
72
+ process_display
73
+ end
58
74
  end
59
75
 
60
- if opts.present?(:save)
61
- process_save
62
- elsif opts.present?(:clear)
63
- process_clear
64
- elsif opts.present?(:replay)
65
- process_replay
66
- else
67
- process_display
68
- end
69
- end
76
+ private
70
77
 
71
- private
78
+ def process_display
79
+ @history = @history.with_line_numbers unless opts.present?(:'no-numbers')
72
80
 
73
- def process_display
74
- unless opts.present?(:'no-numbers')
75
- @history = @history.with_line_numbers
81
+ pry_instance.pager.open do |pager|
82
+ @history.print_to_output(pager, true)
83
+ end
76
84
  end
77
85
 
78
- render_output(@history, opts)
79
- end
86
+ def process_save
87
+ case opts[:save]
88
+ when Range
89
+ @history = @history.between(opts[:save])
80
90
 
81
- def process_save
82
- case opts[:save]
83
- when Range
84
- @history = @history.between(opts[:save])
91
+ raise CommandError, "Must provide a file name." unless args.first
85
92
 
86
- unless args.first
87
- raise CommandError, "Must provide a file name."
93
+ file_name = File.expand_path(args.first)
94
+ when String
95
+ file_name = File.expand_path(opts[:save])
88
96
  end
89
97
 
90
- file_name = File.expand_path(args.first)
91
- when String
92
- file_name = File.expand_path(opts[:save])
93
- end
98
+ output.puts "Saving history in #{file_name}..."
94
99
 
95
- output.puts "Saving history in #{file_name}..."
100
+ File.open(file_name, 'w') { |f| f.write(@history.raw) }
96
101
 
97
- File.open(file_name, 'w') { |f| f.write(@history.raw) }
98
-
99
- output.puts "History saved."
100
- end
102
+ output.puts "History saved."
103
+ end
101
104
 
102
- def process_clear
103
- Pry.history.clear
104
- output.puts "History cleared."
105
- end
105
+ def process_clear
106
+ Pry.history.clear
107
+ output.puts "History cleared."
108
+ end
106
109
 
107
- def process_replay
108
- @history = @history.between(opts[:r])
109
- replay_sequence = @history.raw
110
+ def process_replay
111
+ @history = @history.between(opts[:r])
112
+ replay_sequence = @history.raw
110
113
 
111
- # If we met follow-up "hist" call, check for the "--replay" option
112
- # presence. If "hist" command is called with other options, proceed
113
- # further.
114
- check_for_juxtaposed_replay(replay_sequence)
114
+ # If we met follow-up "hist" call, check for the "--replay" option
115
+ # presence. If "hist" command is called with other options, proceed
116
+ # further.
117
+ check_for_juxtaposed_replay(replay_sequence)
115
118
 
116
- _pry_.input_stack.push _pry_.input
117
- _pry_.input = StringIO.new(replay_sequence)
118
- # eval_string << "#{@history.raw}\n"
119
- # run "show-input" unless _pry_.complete_expression?(eval_string)
120
- end
119
+ replay_sequence.lines.each do |line|
120
+ pry_instance.eval line, generated: true
121
+ end
122
+ end
121
123
 
122
- # Checks +replay_sequence+ for the presence of neighboring replay calls.
123
- # @example
124
- # [1] pry(main)> hist --show 46894
125
- # 46894: hist --replay 46675..46677
126
- # [2] pry(main)> hist --show 46675..46677
127
- # 46675: 1+1
128
- # 46676: a = 100
129
- # 46677: hist --tail
130
- # [3] pry(main)> hist --replay 46894
131
- # Error: Replay index 46894 points out to another replay call: `hist -r 46675..46677`
132
- # [4] pry(main)>
133
- #
134
- # @raise [Pry::CommandError] If +replay_sequence+ contains another
135
- # "hist --replay" call
136
- # @param [String] replay_sequence The sequence of commands to be replayed
137
- # (per saltum)
138
- # @return [Boolean] `false` if +replay_sequence+ does not contain another
139
- # "hist --replay" call
140
- def check_for_juxtaposed_replay(replay_sequence)
141
- if replay_sequence =~ /\Ahist(?:ory)?\b/
142
- # Create *fresh* instance of Options for parsing of "hist" command.
143
- _slop = self.slop
144
- _slop.parse replay_sequence.split(' ')[1..-1]
145
-
146
- if _slop.present?(:r)
147
- replay_sequence = replay_sequence.split("\n").join('; ')
148
- index = opts[:r]
149
- index = index.min if index.min == index.max || index.max.nil?
150
-
151
- raise CommandError, "Replay index #{ index } points out to another replay call: `#{ replay_sequence }`"
124
+ # Checks +replay_sequence+ for the presence of neighboring replay calls.
125
+ # @example
126
+ # [1] pry(main)> hist --show 46894
127
+ # 46894: hist --replay 46675..46677
128
+ # [2] pry(main)> hist --show 46675..46677
129
+ # 46675: 1+1
130
+ # 46676: a = 100
131
+ # 46677: hist --tail
132
+ # [3] pry(main)> hist --replay 46894
133
+ # Error: Replay index 46894 points out to another replay call:
134
+ # `hist -r 46675..46677`
135
+ # [4] pry(main)>
136
+ #
137
+ # @raise [Pry::CommandError] If +replay_sequence+ contains another
138
+ # "hist --replay" call
139
+ # @param [String] replay_sequence The sequence of commands to be replayed
140
+ # (per saltum)
141
+ # @return [Boolean] `false` if +replay_sequence+ does not contain another
142
+ # "hist --replay" call
143
+ def check_for_juxtaposed_replay(replay_sequence)
144
+ if replay_sequence =~ /\Ahist(?:ory)?\b/
145
+ # Create *fresh* instance of Options for parsing of "hist" command.
146
+ slop_instance = slop
147
+ slop_instance.parse(replay_sequence.split(' ')[1..-1])
148
+
149
+ if slop_instance.present?(:r)
150
+ replay_sequence = replay_sequence.split("\n").join('; ')
151
+ index = opts[:r]
152
+ index = index.min if index.min == index.max || index.max.nil?
153
+
154
+ raise CommandError,
155
+ "Replay index #{index} points out to another replay call: " \
156
+ "`#{replay_sequence}`"
157
+ end
158
+ else
159
+ false
152
160
  end
153
- else
154
- false
161
+ end
162
+
163
+ # Finds history depending on the given switch.
164
+ #
165
+ # @return [Pry::Code] if it finds `--all` (or `-a`) switch, returns all
166
+ # entries in history. Without the switch returns only the entries from the
167
+ # current Pry session.
168
+ def find_history
169
+ h = if opts.present?(:all)
170
+ Pry.history.to_a
171
+ else
172
+ Pry.history.to_a.last(Pry.history.session_line_count)
173
+ end
174
+
175
+ Pry::Code(Pry.history.filter(h[0..-2]))
155
176
  end
156
177
  end
157
- end
158
178
 
159
- Pry::Commands.add_command(Pry::Command::Hist)
160
- Pry::Commands.alias_command 'history', 'hist'
179
+ Pry::Commands.add_command(Pry::Command::Hist)
180
+ Pry::Commands.alias_command 'history', 'hist'
181
+ end
161
182
  end