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
@@ -2,7 +2,7 @@ class Pry
2
2
  module RbxPath
3
3
  module_function
4
4
  def is_core_path?(path)
5
- path.start_with?("kernel") || path.start_with?("lib")
5
+ Pry::Helpers::BaseHelpers.rbx? && (path.start_with?("kernel") || path.start_with?("lib")) && File.exist?(convert_path_to_full(path))
6
6
  end
7
7
 
8
8
  def convert_path_to_full(path)
@@ -0,0 +1,202 @@
1
+ require 'forwardable'
2
+
3
+ class Pry
4
+ class REPL
5
+ extend Forwardable
6
+ def_delegators :@pry, :input, :output
7
+
8
+ # @return [Pry] The instance of {Pry} that the user is controlling.
9
+ attr_accessor :pry
10
+
11
+ # Instantiate a new {Pry} instance with the given options, then start a
12
+ # {REPL} instance wrapping it.
13
+ # @option options See {Pry#initialize}
14
+ def self.start(options)
15
+ new(Pry.new(options)).start
16
+ end
17
+
18
+ # Create an instance of {REPL} wrapping the given {Pry}.
19
+ # @param [Pry] pry The instance of {Pry} that this {REPL} will control.
20
+ # @param [Hash] options Options for this {REPL} instance.
21
+ # @option options [Object] :target The initial target of the session.
22
+ def initialize(pry, options = {})
23
+ @pry = pry
24
+ @indent = Pry::Indent.new
25
+
26
+ if options[:target]
27
+ @pry.push_binding options[:target]
28
+ end
29
+ end
30
+
31
+ # Start the read-eval-print loop.
32
+ # @return [Object?] If the session throws `:breakout`, return the value
33
+ # thrown with it.
34
+ # @raise [Exception] If the session throws `:raise_up`, raise the exception
35
+ # thrown with it.
36
+ def start
37
+ prologue
38
+ Pry::InputLock.for(:all).with_ownership { repl }
39
+ ensure
40
+ epilogue
41
+ end
42
+
43
+ private
44
+
45
+ # Set up the repl session.
46
+ # @return [void]
47
+ def prologue
48
+ pry.exec_hook :before_session, pry.output, pry.current_binding, pry
49
+
50
+ # Clear the line before starting Pry. This fixes issue #566.
51
+ if pry.config.correct_indent
52
+ Kernel.print Pry::Helpers::BaseHelpers.windows_ansi? ? "\e[0F" : "\e[0G"
53
+ end
54
+ end
55
+
56
+ # The actual read-eval-print loop.
57
+ #
58
+ # The {REPL} instance is responsible for reading and looping, whereas the
59
+ # {Pry} instance is responsible for evaluating user input and printing
60
+ # return values and command output.
61
+ #
62
+ # @return [Object?] If the session throws `:breakout`, return the value
63
+ # thrown with it.
64
+ # @raise [Exception] If the session throws `:raise_up`, raise the exception
65
+ # thrown with it.
66
+ def repl
67
+ loop do
68
+ case val = read
69
+ when :control_c
70
+ output.puts ""
71
+ pry.reset_eval_string
72
+ when :no_more_input
73
+ output.puts "" if output.tty?
74
+ break
75
+ else
76
+ output.puts "" if val.nil? && output.tty?
77
+ return pry.exit_value unless pry.eval(val)
78
+ end
79
+ end
80
+ end
81
+
82
+ # Clean up after the repl session.
83
+ # @return [void]
84
+ def epilogue
85
+ pry.exec_hook :after_session, pry.output, pry.current_binding, pry
86
+ end
87
+
88
+ # Read a line of input from the user.
89
+ # @return [String] The line entered by the user.
90
+ # @return [nil] On `<Ctrl-D>`.
91
+ # @return [:control_c] On `<Ctrl+C>`.
92
+ # @return [:no_more_input] On EOF.
93
+ def read
94
+ @indent.reset if pry.eval_string.empty?
95
+ current_prompt = pry.select_prompt
96
+ indentation = pry.config.auto_indent ? @indent.current_prefix : ''
97
+
98
+ val = read_line("#{current_prompt}#{indentation}")
99
+
100
+ # Return nil for EOF, :no_more_input for error, or :control_c for <Ctrl-C>
101
+ return val unless String === val
102
+
103
+ if pry.config.auto_indent
104
+ original_val = "#{indentation}#{val}"
105
+ indented_val = @indent.indent(val)
106
+
107
+ if output.tty? && pry.config.correct_indent && Pry::Helpers::BaseHelpers.use_ansi_codes?
108
+ output.print @indent.correct_indentation(
109
+ current_prompt, indented_val,
110
+ original_val.length - indented_val.length
111
+ )
112
+ output.flush
113
+ end
114
+ else
115
+ indented_val = val
116
+ end
117
+
118
+ indented_val
119
+ end
120
+
121
+ # Manage switching of input objects on encountering `EOFError`s.
122
+ # @return [Object] Whatever the given block returns.
123
+ # @return [:no_more_input] Indicates that no more input can be read.
124
+ def handle_read_errors
125
+ should_retry = true
126
+ exception_count = 0
127
+
128
+ begin
129
+ yield
130
+ rescue EOFError
131
+ pry.config.input = Pry.config.input
132
+ if !should_retry
133
+ output.puts "Error: Pry ran out of things to read from! " \
134
+ "Attempting to break out of REPL."
135
+ return :no_more_input
136
+ end
137
+ should_retry = false
138
+ retry
139
+
140
+ # Handle <Ctrl+C> like Bash: empty the current input buffer, but don't
141
+ # quit. This is only for MRI 1.9; other versions of Ruby don't let you
142
+ # send Interrupt from within Readline.
143
+ rescue Interrupt
144
+ return :control_c
145
+
146
+ # If we get a random error when trying to read a line we don't want to
147
+ # automatically retry, as the user will see a lot of error messages
148
+ # scroll past and be unable to do anything about it.
149
+ rescue RescuableException => e
150
+ puts "Error: #{e.message}"
151
+ output.puts e.backtrace
152
+ exception_count += 1
153
+ if exception_count < 5
154
+ retry
155
+ end
156
+ puts "FATAL: Pry failed to get user input using `#{input}`."
157
+ puts "To fix this you may be able to pass input and output file " \
158
+ "descriptors to pry directly. e.g."
159
+ puts " Pry.config.input = STDIN"
160
+ puts " Pry.config.output = STDOUT"
161
+ puts " binding.pry"
162
+ return :no_more_input
163
+ end
164
+ end
165
+
166
+ # Returns the next line of input to be sent to the {Pry} instance.
167
+ # @param [String] current_prompt The prompt to use for input.
168
+ # @return [String?] The next line of input, or `nil` on <Ctrl-D>.
169
+ def read_line(current_prompt)
170
+ handle_read_errors do
171
+ if defined? Coolline and input.is_a? Coolline
172
+ input.completion_proc = proc do |cool|
173
+ completions = @pry.complete cool.completed_word
174
+ completions.compact
175
+ end
176
+ elsif input.respond_to? :completion_proc=
177
+ input.completion_proc = proc do |input|
178
+ @pry.complete input
179
+ end
180
+ end
181
+
182
+ if defined?(Readline) and input == Readline
183
+ input_readline(current_prompt, false) # false since we'll add it manually
184
+ elsif defined? Coolline and input.is_a? Coolline
185
+ input_readline(current_prompt)
186
+ else
187
+ if input.method(:readline).arity == 1
188
+ input_readline(current_prompt)
189
+ else
190
+ input_readline
191
+ end
192
+ end
193
+ end
194
+ end
195
+
196
+ def input_readline(*args)
197
+ Pry::InputLock.for(:all).interruptible_region do
198
+ input.readline(*args)
199
+ end
200
+ end
201
+ end
202
+ end
@@ -15,29 +15,40 @@ class Pry
15
15
  full_name = File.expand_path(file_name)
16
16
  raise RuntimeError, "No such file: #{full_name}" if !File.exists?(full_name)
17
17
 
18
- @content = StringIO.new(File.read(full_name))
18
+ define_additional_commands
19
+ @content = File.read(full_name)
19
20
  end
20
21
 
21
22
  # Switch to interactive mode, i.e take input from the user
22
23
  # and use the regular print and exception handlers.
23
24
  # @param [Pry] _pry_ the Pry instance to make interactive.
24
25
  def interactive_mode(_pry_)
25
- _pry_.input = Pry.config.input
26
- _pry_.print = Pry.config.print
27
- _pry_.exception_handler = Pry.config.exception_handler
26
+ _pry_.config.input = Pry.config.input
27
+ _pry_.config.print = Pry.config.print
28
+ _pry_.config.exception_handler = Pry.config.exception_handler
29
+ Pry::REPL.new(_pry_).start
28
30
  end
29
31
 
30
32
  # Switch to non-interactive mode. Essentially
31
33
  # this means there is no result output
32
34
  # and that the session becomes interactive when an exception is encountered.
33
35
  # @param [Pry] _pry_ the Pry instance to make non-interactive.
34
- def non_interactive_mode(_pry_)
36
+ def non_interactive_mode(_pry_, content)
35
37
  _pry_.print = proc {}
36
- _pry_.exception_handler = proc do |o, e, _pry_|
37
- _pry_.run_command "cat --ex"
38
+ _pry_.exception_handler = proc do |o, e, _p_|
39
+ _p_.run_command "cat --ex"
38
40
  o.puts "...exception encountered, going interactive!"
39
41
  interactive_mode(_pry_)
40
42
  end
43
+
44
+ content.lines.each do |line|
45
+ break unless _pry_.eval line, :generated => true
46
+ end
47
+
48
+ unless _pry_.eval_string.empty?
49
+ _pry_.output.puts "#{_pry_.eval_string}...exception encountered, going interactive!"
50
+ interactive_mode(_pry_)
51
+ end
41
52
  end
42
53
 
43
54
  # Define a few extra commands useful for flipping back & forth
@@ -46,35 +57,18 @@ class Pry
46
57
  s = self
47
58
 
48
59
  Pry::Commands.command "make-interactive", "Make the session interactive" do
49
- _pry_.input_stack.push _pry_.input
50
60
  s.interactive_mode(_pry_)
51
61
  end
52
62
 
53
- Pry::Commands.command "make-non-interactive", "Make the session non-interactive" do
54
- _pry_.input = _pry_.input_stack.pop
55
- s.non_interactive_mode(_pry_)
56
- end
57
-
58
63
  Pry::Commands.command "load-file", "Load another file through the repl" do |file_name|
59
- content = StringIO.new(File.read(File.expand_path(file_name)))
60
- _pry_.input_stack.push(_pry_.input)
61
- _pry_.input = content
64
+ s.non_interactive_mode(_pry_, File.read(File.expand_path(file_name)))
62
65
  end
63
66
  end
64
67
 
65
68
  # Actually load the file through the REPL by setting file content
66
69
  # as the REPL input stream.
67
70
  def load
68
- Pry.initial_session_setup
69
- define_additional_commands
70
-
71
- Pry.config.hooks.add_hook(:when_started, :start_non_interactively) do |o, t, _pry_|
72
- non_interactive_mode(_pry_)
73
- end
74
-
75
- Pry.start(Pry.toplevel_binding,
76
- :input => @content,
77
- :input_stack => [StringIO.new("exit-all\n")])
71
+ non_interactive_mode(Pry.new, @content)
78
72
  end
79
73
  end
80
74
  end
@@ -0,0 +1,82 @@
1
+ require 'rubygems'
2
+
3
+ class Pry
4
+ module Rubygem
5
+
6
+ class << self
7
+ def installed?(name)
8
+ if Gem::Specification.respond_to?(:find_all_by_name)
9
+ Gem::Specification.find_all_by_name(name).any?
10
+ else
11
+ Gem.source_index.find_name(name).first
12
+ end
13
+ end
14
+
15
+ # Get the gem spec object for the given gem name.
16
+ #
17
+ # @param [String] name
18
+ # @return [Gem::Specification]
19
+ def spec(name)
20
+ specs = if Gem::Specification.respond_to?(:each)
21
+ Gem::Specification.find_all_by_name(name)
22
+ else
23
+ Gem.source_index.find_name(name)
24
+ end
25
+
26
+ first_spec = specs.sort_by{ |spec| Gem::Version.new(spec.version) }.last
27
+
28
+ first_spec or raise CommandError, "Gem `#{name}` not found"
29
+ end
30
+
31
+ # List gems matching a pattern.
32
+ #
33
+ # @param [Regexp] pattern
34
+ # @return [Array<Gem::Specification>]
35
+ def list(pattern = /.*/)
36
+ if Gem::Specification.respond_to?(:each)
37
+ Gem::Specification.select{|spec| spec.name =~ pattern }
38
+ else
39
+ Gem.source_index.gems.values.select{|spec| spec.name =~ pattern }
40
+ end
41
+ end
42
+
43
+ # Completion function for gem-cd and gem-open.
44
+ #
45
+ # @param [String] so_far what the user's typed so far
46
+ # @return [Array<String>] completions
47
+ def complete(so_far)
48
+ if so_far =~ / ([^ ]*)\z/
49
+ self.list(%r{\A#{$2}}).map(&:name)
50
+ else
51
+ self.list.map(&:name)
52
+ end
53
+ end
54
+
55
+ # Installs a gem with all its dependencies.
56
+ #
57
+ # @param [String] name
58
+ # @return [void]
59
+ def install(name)
60
+ gemrc_opts = Gem.configuration['gem'].split(' ')
61
+ destination = if gemrc_opts.include?('--user-install')
62
+ Gem.user_dir
63
+ elsif File.writable?(Gem.dir)
64
+ Gem.dir
65
+ else
66
+ Gem.user_dir
67
+ end
68
+ installer = Gem::DependencyInstaller.new(:install_dir => destination)
69
+ installer.install(name)
70
+ rescue Errno::EACCES
71
+ raise CommandError,
72
+ "Insufficient permissions to install #{ Pry::Helpers::Text.green(name) }."
73
+ rescue Gem::GemNotFoundException
74
+ raise CommandError,
75
+ "Gem #{ Pry::Helpers::Text.green(name) } not found. Aborting installation."
76
+ else
77
+ Gem.refresh
78
+ end
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,79 @@
1
+ class Pry::Terminal
2
+ class << self
3
+ # Return a pair of [rows, columns] which gives the size of the window.
4
+ #
5
+ # If the window size cannot be determined, return nil.
6
+ def screen_size
7
+ rows, cols = actual_screen_size
8
+ if rows && cols
9
+ [rows.to_i, cols.to_i]
10
+ else
11
+ nil
12
+ end
13
+ end
14
+
15
+ # Return a screen size or a default if that fails.
16
+ def size! default = [27, 80]
17
+ screen_size || default
18
+ end
19
+
20
+ # Return a screen width or the default if that fails.
21
+ def width!
22
+ size![1]
23
+ end
24
+
25
+ # Return a screen height or the default if that fails.
26
+ def height!
27
+ size![0]
28
+ end
29
+
30
+ def actual_screen_size
31
+ # The best way, if possible (requires non-jruby ≥1.9 or io-console gem)
32
+ screen_size_according_to_io_console or
33
+ # Fall back to the old standby, though it might be stale:
34
+ screen_size_according_to_env or
35
+ # Fall further back, though this one is also out of date without something
36
+ # calling Readline.set_screen_size
37
+ screen_size_according_to_readline or
38
+ # Windows users can otherwise run ansicon and get a decent answer:
39
+ screen_size_according_to_ansicon_env
40
+ end
41
+
42
+ def screen_size_according_to_io_console
43
+ return if Pry::Helpers::BaseHelpers.jruby?
44
+ require 'io/console'
45
+ $stdout.winsize if $stdout.tty? and $stdout.respond_to?(:winsize)
46
+ rescue LoadError
47
+ # They probably don't have the io/console stdlib or the io-console gem.
48
+ # We'll keep trying.
49
+ end
50
+
51
+ def screen_size_according_to_env
52
+ size = [ENV['LINES'] || ENV['ROWS'], ENV['COLUMNS']]
53
+ size if nonzero_column?(size)
54
+ end
55
+
56
+ def screen_size_according_to_readline
57
+ if defined?(Readline) && Readline.respond_to?(:get_screen_size)
58
+ size = Readline.get_screen_size
59
+ size if nonzero_column?(size)
60
+ end
61
+ rescue Java::JavaLang::NullPointerException
62
+ # This rescue won't happen on jrubies later than:
63
+ # https://github.com/jruby/jruby/pull/436
64
+ nil
65
+ end
66
+
67
+ def screen_size_according_to_ansicon_env
68
+ return unless ENV['ANSICON'] =~ /\((.*)x(.*)\)/
69
+ size = [$2, $1]
70
+ size if nonzero_column?(size)
71
+ end
72
+
73
+ private
74
+
75
+ def nonzero_column?(size)
76
+ size[1].to_i > 0
77
+ end
78
+ end
79
+ end