pry 0.10.pre.1-java → 0.10.0.pre2-java

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 +126 -182
  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
@@ -0,0 +1,29 @@
1
+ class Pry
2
+ class Command::GemOpen < Pry::ClassCommand
3
+ match 'gem-open'
4
+ group 'Gems'
5
+ description 'Opens the working directory of the gem in your editor.'
6
+ command_options :argument_required => true
7
+
8
+ banner <<-'BANNER'
9
+ Usage: gem-open GEM_NAME
10
+
11
+ Change the current working directory to that in which the given gem is
12
+ installed, and then opens your text editor.
13
+
14
+ gem-open pry-exception_explorer
15
+ BANNER
16
+
17
+ def process(gem)
18
+ Dir.chdir(Rubygem.spec(gem).full_gem_path) do
19
+ Pry::Editor.invoke_editor(".", 0, false)
20
+ end
21
+ end
22
+
23
+ def complete(str)
24
+ Rubygem.complete(str)
25
+ end
26
+ end
27
+
28
+ Pry::Commands.add_command(Pry::Command::GemOpen)
29
+ end
@@ -0,0 +1,101 @@
1
+ class Pry
2
+ class Command::Gist < Pry::ClassCommand
3
+ match 'gist'
4
+ group 'Misc'
5
+ description 'Upload code, docs, history to https://gist.github.com/.'
6
+ command_options :requires_gem => "gist"
7
+
8
+ banner <<-'BANNER'
9
+ Usage: gist [OPTIONS] [--help]
10
+
11
+ The gist command enables you to gist code from files and methods to github.
12
+
13
+ gist -i 20 --lines 1..3
14
+ gist Pry#repl --lines 1..-1
15
+ gist Rakefile --lines 5
16
+ BANNER
17
+
18
+ def setup
19
+ require 'gist'
20
+ end
21
+
22
+ def options(opt)
23
+ CodeCollector.inject_options(opt)
24
+ opt.on :login, "Authenticate the gist gem with GitHub"
25
+ opt.on :p, :public, "Create a public gist (default: false)", :default => false
26
+ opt.on :clip, "Copy the selected content to clipboard instead, do NOT gist it", :default => false
27
+ end
28
+
29
+ def process
30
+ return ::Gist.login! if opts.present?(:login)
31
+ cc = CodeCollector.new(args, opts, _pry_)
32
+
33
+ if cc.content =~ /\A\s*\z/
34
+ raise CommandError, "Found no code to gist."
35
+ end
36
+
37
+ if opts.present?(:clip)
38
+ clipboard_content(cc.content)
39
+ else
40
+ # we're overriding the default behavior of the 'in' option (as
41
+ # defined on CodeCollector) with our local behaviour.
42
+ content = opts.present?(:in) ? input_content : cc.content
43
+ gist_content content, cc.file
44
+ end
45
+ end
46
+
47
+ def clipboard_content(content)
48
+ ::Gist.copy(content)
49
+ output.puts "Copied content to clipboard!"
50
+ end
51
+
52
+ def input_content
53
+ content = ""
54
+ CodeCollector.input_expression_ranges.each do |range|
55
+ input_expressions = _pry_.input_array[range] || []
56
+ Array(input_expressions).each_with_index do |code, index|
57
+ corrected_index = index + range.first
58
+ if code && code != ""
59
+ content << code
60
+ if code !~ /;\Z/
61
+ content << "#{comment_expression_result_for_gist(_pry_.config.gist.inspecter.call(_pry_.output_array[corrected_index]))}"
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ content
68
+ end
69
+
70
+ def comment_expression_result_for_gist(result)
71
+ content = ""
72
+ result.lines.each_with_index do |line, index|
73
+ if index == 0
74
+ content << "# => #{line}"
75
+ else
76
+ content << "# #{line}"
77
+ end
78
+ end
79
+
80
+ content
81
+ end
82
+
83
+ def gist_content(content, filename)
84
+ response = ::Gist.gist(content, :filename => filename || "pry_gist.rb", :public => !!opts[:p])
85
+ if response
86
+ url = response['html_url']
87
+ message = "Gist created at URL #{url}"
88
+ begin
89
+ ::Gist.copy(url)
90
+ message << ", which is now in the clipboard."
91
+ rescue ::Gist::ClipboardError
92
+ end
93
+
94
+ output.puts message
95
+ end
96
+ end
97
+ end
98
+
99
+ Pry::Commands.add_command(Pry::Command::Gist)
100
+ Pry::Commands.alias_command 'clipit', 'gist --clip'
101
+ end
@@ -0,0 +1,164 @@
1
+ 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 along with their
11
+ descriptions. When given a command name as an argument, shows the help
12
+ 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?
21
+ end
22
+ visible
23
+ end
24
+
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
29
+
30
+ def process
31
+ if args.empty?
32
+ display_index(command_groups)
33
+ else
34
+ display_search(args.first)
35
+ end
36
+ end
37
+
38
+ # Display the index view, with headings and short descriptions per command.
39
+ #
40
+ # @param [Hash<String, Array<Commands>>] groups
41
+ def display_index(groups)
42
+ help_text = []
43
+
44
+ sorted_group_names(groups).each do |group_name|
45
+ commands = sorted_commands(groups[group_name])
46
+
47
+ if commands.any?
48
+ help_text << help_text_for_commands(group_name, commands)
49
+ end
50
+ end
51
+
52
+ _pry_.pager.page help_text.join("\n\n")
53
+ end
54
+
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.capitalize)}\n" << commands.map do |command|
63
+ " #{command.options[:listing].to_s.ljust(18)} #{command.description.capitalize}"
64
+ end.join("\n")
65
+ end
66
+
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
72
+
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
80
+
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)
89
+ end
90
+ end
91
+
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)
103
+ end
104
+ end
105
+
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})
117
+ end
118
+ end
119
+
120
+ # Display help for an individual command.
121
+ #
122
+ # @param [Pry::Command] command
123
+ def display_command(command)
124
+ _pry_.pager.page command.new.help
125
+ end
126
+
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] search the search term
134
+ # @param [Hash] 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
+ matching[key] = value
144
+ end
145
+ end
146
+
147
+ matching
148
+ end
149
+
150
+ # Clean search terms to make it easier to search group names
151
+ #
152
+ # @param [String] key
153
+ # @return [String]
154
+ def normalize(key)
155
+ key.downcase.gsub(/pry\W+/, '')
156
+ end
157
+
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
161
+ end
162
+
163
+ Pry::Commands.add_command(Pry::Command::Help)
164
+ end
@@ -0,0 +1,180 @@
1
+ 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 --all
10
+ hist --head N
11
+ hist --tail N
12
+ hist --show START..END
13
+ hist --grep PATTERN
14
+ hist --clear
15
+ hist --replay START..END
16
+ hist --save [START..END] FILE
17
+ Aliases: history
18
+
19
+ Show and replay Readline history.
20
+ BANNER
21
+
22
+ def options(opt)
23
+ opt.on :a, :all, "Display all history"
24
+ opt.on :H, :head, "Display the first N items", :optional_argument => true, :as => Integer
25
+ opt.on :T, :tail, "Display the last N items", :optional_argument => true, :as => Integer
26
+ opt.on :s, :show, "Show the given range of lines", :optional_argument => true, :as => Range
27
+ opt.on :G, :grep, "Show lines matching the given pattern", :argument => true, :as => String
28
+ opt.on :c, :clear , "Clear the current session's history"
29
+ opt.on :r, :replay, "Replay a line or range of lines", :argument => true, :as => Range
30
+ opt.on :save, "Save history to a file", :argument => true, :as => Range
31
+ opt.on :e, :'exclude-pry', "Exclude Pry commands from the history"
32
+ opt.on :n, :'no-numbers', "Omit line numbers"
33
+ end
34
+
35
+ def process
36
+ @history = find_history
37
+
38
+ if opts.present?(:show)
39
+ @history = @history.between(opts[:show])
40
+ end
41
+
42
+ if opts.present?(:grep)
43
+ @history = @history.grep(opts[:grep])
44
+ end
45
+
46
+ @history = case
47
+ when opts.present?(:head)
48
+ @history.take_lines(1, opts[:head] || 10)
49
+ when opts.present?(:tail)
50
+ @history.take_lines(-(opts[:tail] || 10), opts[:tail] || 10)
51
+ when opts.present?(:show)
52
+ @history.between(opts[:show])
53
+ else
54
+ @history
55
+ end
56
+
57
+ if opts.present?(:'exclude-pry')
58
+ @history = @history.select do |loc|
59
+ !command_set.valid_command?(loc.line)
60
+ end
61
+ end
62
+
63
+ if opts.present?(:save)
64
+ process_save
65
+ elsif opts.present?(:clear)
66
+ process_clear
67
+ elsif opts.present?(:replay)
68
+ process_replay
69
+ else
70
+ process_display
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ def process_display
77
+ unless opts.present?(:'no-numbers')
78
+ @history = @history.with_line_numbers
79
+ end
80
+
81
+ _pry_.pager.open do |pager|
82
+ @history.print_to_output(pager, true)
83
+ end
84
+ end
85
+
86
+ def process_save
87
+ case opts[:save]
88
+ when Range
89
+ @history = @history.between(opts[:save])
90
+
91
+ unless args.first
92
+ raise CommandError, "Must provide a file name."
93
+ end
94
+
95
+ file_name = File.expand_path(args.first)
96
+ when String
97
+ file_name = File.expand_path(opts[:save])
98
+ end
99
+
100
+ output.puts "Saving history in #{file_name}..."
101
+
102
+ File.open(file_name, 'w') { |f| f.write(@history.raw) }
103
+
104
+ output.puts "History saved."
105
+ end
106
+
107
+ def process_clear
108
+ Pry.history.clear
109
+ output.puts "History cleared."
110
+ end
111
+
112
+ def process_replay
113
+ @history = @history.between(opts[:r])
114
+ replay_sequence = @history.raw
115
+
116
+ # If we met follow-up "hist" call, check for the "--replay" option
117
+ # presence. If "hist" command is called with other options, proceed
118
+ # further.
119
+ check_for_juxtaposed_replay(replay_sequence)
120
+
121
+ replay_sequence.lines.each do |line|
122
+ _pry_.eval line, :generated => true
123
+ end
124
+ end
125
+
126
+ # Checks +replay_sequence+ for the presence of neighboring replay calls.
127
+ # @example
128
+ # [1] pry(main)> hist --show 46894
129
+ # 46894: hist --replay 46675..46677
130
+ # [2] pry(main)> hist --show 46675..46677
131
+ # 46675: 1+1
132
+ # 46676: a = 100
133
+ # 46677: hist --tail
134
+ # [3] pry(main)> hist --replay 46894
135
+ # Error: Replay index 46894 points out to another replay call: `hist -r 46675..46677`
136
+ # [4] pry(main)>
137
+ #
138
+ # @raise [Pry::CommandError] If +replay_sequence+ contains another
139
+ # "hist --replay" call
140
+ # @param [String] replay_sequence The sequence of commands to be replayed
141
+ # (per saltum)
142
+ # @return [Boolean] `false` if +replay_sequence+ does not contain another
143
+ # "hist --replay" call
144
+ def check_for_juxtaposed_replay(replay_sequence)
145
+ if replay_sequence =~ /\Ahist(?:ory)?\b/
146
+ # Create *fresh* instance of Options for parsing of "hist" command.
147
+ _slop = self.slop
148
+ _slop.parse replay_sequence.split(' ')[1..-1]
149
+
150
+ if _slop.present?(:r)
151
+ replay_sequence = replay_sequence.split("\n").join('; ')
152
+ index = opts[:r]
153
+ index = index.min if index.min == index.max || index.max.nil?
154
+
155
+ raise CommandError, "Replay index #{ index } points out to another replay call: `#{ replay_sequence }`"
156
+ end
157
+ else
158
+ false
159
+ end
160
+ end
161
+
162
+ # Finds history depending on the given switch.
163
+ #
164
+ # @return [Pry::Code] if it finds `--all` (or `-a`) switch, returns all
165
+ # entries in history. Without the switch returns only the entries from the
166
+ # current Pry session.
167
+ def find_history
168
+ h = if opts.present?(:all)
169
+ Pry.history.to_a
170
+ else
171
+ Pry.history.to_a.last(Pry.history.session_line_count)
172
+ end
173
+ # The last value in history will be the 'hist' command itself.
174
+ Pry::Code(h[0..-2])
175
+ end
176
+ end
177
+
178
+ Pry::Commands.add_command(Pry::Command::Hist)
179
+ Pry::Commands.alias_command 'history', 'hist'
180
+ end