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,19 +1,23 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Pry
2
- class Command::FixIndent < Pry::ClassCommand
3
- match 'fix-indent'
4
- group 'Input and Output'
4
+ class Command
5
+ class FixIndent < Pry::ClassCommand
6
+ match 'fix-indent'
7
+ group 'Input and Output'
5
8
 
6
- description "Correct the indentation for contents of the input buffer"
9
+ description "Correct the indentation for contents of the input buffer"
7
10
 
8
- banner <<-USAGE
9
- Usage: fix-indent
10
- USAGE
11
+ banner <<-USAGE
12
+ Usage: fix-indent
13
+ USAGE
11
14
 
12
- def process
13
- indented_str = Pry::Indent.indent(eval_string)
14
- eval_string.replace indented_str
15
+ def process
16
+ indented_str = Pry::Indent.indent(eval_string)
17
+ pry_instance.eval_string = indented_str
18
+ end
15
19
  end
16
- end
17
20
 
18
- Pry::Commands.add_command(Pry::Command::FixIndent)
21
+ Pry::Commands.add_command(Pry::Command::FixIndent)
22
+ end
19
23
  end
@@ -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 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?
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>>] groups
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
- _pry_.pager.page 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
- "#{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
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
- _pry_.pager.page 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] 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
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] key
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,181 +1,182 @@
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
1
+ # frozen_string_literal: true
37
2
 
38
- if opts.present?(:show)
39
- @history = @history.between(opts[:show])
3
+ class Pry
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"
40
41
  end
41
42
 
42
- if opts.present?(:grep)
43
- @history = @history.grep(opts[:grep])
44
- end
43
+ def process
44
+ @history = find_history
45
45
 
46
- @history =
47
- case
48
- when opts.present?(:head)
49
- @history.take_lines(1, opts[:head] || 10)
50
- when opts.present?(:tail)
51
- @history.take_lines(-(opts[:tail] || 10), opts[:tail] || 10)
52
- when opts.present?(:show)
53
- @history.between(opts[:show])
54
- else
55
- @history
56
- end
46
+ @history = @history.between(opts[:show]) if opts.present?(:show)
57
47
 
58
- if opts.present?(:'exclude-pry')
59
- @history = @history.select do |loc|
60
- !command_set.valid_command?(loc.line)
61
- end
62
- end
48
+ @history = @history.grep(opts[:grep]) if opts.present?(:grep)
63
49
 
64
- if opts.present?(:save)
65
- process_save
66
- elsif opts.present?(:clear)
67
- process_clear
68
- elsif opts.present?(:replay)
69
- process_replay
70
- else
71
- process_display
72
- end
73
- end
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
74
58
 
75
- private
59
+ if opts.present?(:'exclude-pry')
60
+ @history = @history.reject do |loc|
61
+ command_set.valid_command?(loc.line)
62
+ end
63
+ end
76
64
 
77
- def process_display
78
- unless opts.present?(:'no-numbers')
79
- @history = @history.with_line_numbers
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
80
74
  end
81
75
 
82
- _pry_.pager.open do |pager|
83
- @history.print_to_output(pager, true)
84
- end
85
- end
76
+ private
86
77
 
87
- def process_save
88
- case opts[:save]
89
- when Range
90
- @history = @history.between(opts[:save])
78
+ def process_display
79
+ @history = @history.with_line_numbers unless opts.present?(:'no-numbers')
91
80
 
92
- unless args.first
93
- raise CommandError, "Must provide a file name."
81
+ pry_instance.pager.open do |pager|
82
+ @history.print_to_output(pager, true)
94
83
  end
95
-
96
- file_name = File.expand_path(args.first)
97
- when String
98
- file_name = File.expand_path(opts[:save])
99
84
  end
100
85
 
101
- output.puts "Saving history in #{file_name}..."
86
+ def process_save
87
+ case opts[:save]
88
+ when Range
89
+ @history = @history.between(opts[:save])
102
90
 
103
- File.open(file_name, 'w') { |f| f.write(@history.raw) }
91
+ raise CommandError, "Must provide a file name." unless args.first
104
92
 
105
- output.puts "History saved."
106
- end
93
+ file_name = File.expand_path(args.first)
94
+ when String
95
+ file_name = File.expand_path(opts[:save])
96
+ end
107
97
 
108
- def process_clear
109
- Pry.history.clear
110
- output.puts "History cleared."
111
- end
98
+ output.puts "Saving history in #{file_name}..."
112
99
 
113
- def process_replay
114
- @history = @history.between(opts[:r])
115
- replay_sequence = @history.raw
100
+ File.open(file_name, 'w') { |f| f.write(@history.raw) }
116
101
 
117
- # If we met follow-up "hist" call, check for the "--replay" option
118
- # presence. If "hist" command is called with other options, proceed
119
- # further.
120
- check_for_juxtaposed_replay(replay_sequence)
102
+ output.puts "History saved."
103
+ end
121
104
 
122
- replay_sequence.lines.each do |line|
123
- _pry_.eval line, generated: true
105
+ def process_clear
106
+ Pry.history.clear
107
+ output.puts "History cleared."
124
108
  end
125
- end
126
109
 
127
- # Checks +replay_sequence+ for the presence of neighboring replay calls.
128
- # @example
129
- # [1] pry(main)> hist --show 46894
130
- # 46894: hist --replay 46675..46677
131
- # [2] pry(main)> hist --show 46675..46677
132
- # 46675: 1+1
133
- # 46676: a = 100
134
- # 46677: hist --tail
135
- # [3] pry(main)> hist --replay 46894
136
- # Error: Replay index 46894 points out to another replay call: `hist -r 46675..46677`
137
- # [4] pry(main)>
138
- #
139
- # @raise [Pry::CommandError] If +replay_sequence+ contains another
140
- # "hist --replay" call
141
- # @param [String] replay_sequence The sequence of commands to be replayed
142
- # (per saltum)
143
- # @return [Boolean] `false` if +replay_sequence+ does not contain another
144
- # "hist --replay" call
145
- def check_for_juxtaposed_replay(replay_sequence)
146
- if replay_sequence =~ /\Ahist(?:ory)?\b/
147
- # Create *fresh* instance of Options for parsing of "hist" command.
148
- _slop = self.slop
149
- _slop.parse replay_sequence.split(' ')[1..-1]
150
-
151
- if _slop.present?(:r)
152
- replay_sequence = replay_sequence.split("\n").join('; ')
153
- index = opts[:r]
154
- index = index.min if index.min == index.max || index.max.nil?
155
-
156
- raise CommandError, "Replay index #{ index } points out to another replay call: `#{ replay_sequence }`"
110
+ def process_replay
111
+ @history = @history.between(opts[:r])
112
+ replay_sequence = @history.raw
113
+
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)
118
+
119
+ replay_sequence.lines.each do |line|
120
+ pry_instance.eval line, generated: true
157
121
  end
158
- else
159
- false
160
122
  end
161
- end
162
123
 
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)
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}`"
173
157
  end
158
+ else
159
+ false
160
+ end
161
+ end
174
162
 
175
- Pry::Code(Pry.history.filter(h[0..-2]))
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]))
176
+ end
176
177
  end
177
- end
178
178
 
179
- Pry::Commands.add_command(Pry::Command::Hist)
180
- Pry::Commands.alias_command 'history', 'hist'
179
+ Pry::Commands.add_command(Pry::Command::Hist)
180
+ Pry::Commands.alias_command 'history', 'hist'
181
+ end
181
182
  end