pry 0.9.10pre1-java → 0.9.11-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (194) hide show
  1. data/.travis.yml +3 -1
  2. data/CHANGELOG +63 -2
  3. data/CONTRIBUTORS +43 -25
  4. data/Gemfile +7 -0
  5. data/Guardfile +62 -0
  6. data/README.markdown +4 -4
  7. data/Rakefile +34 -35
  8. data/lib/pry.rb +107 -54
  9. data/lib/pry/cli.rb +34 -11
  10. data/lib/pry/code.rb +165 -182
  11. data/lib/pry/code/code_range.rb +70 -0
  12. data/lib/pry/code/loc.rb +92 -0
  13. data/lib/pry/code_object.rb +153 -0
  14. data/lib/pry/command.rb +160 -22
  15. data/lib/pry/command_set.rb +37 -26
  16. data/lib/pry/commands.rb +4 -27
  17. data/lib/pry/commands/amend_line.rb +99 -0
  18. data/lib/pry/commands/bang.rb +20 -0
  19. data/lib/pry/commands/bang_pry.rb +17 -0
  20. data/lib/pry/commands/cat.rb +53 -0
  21. data/lib/pry/commands/cat/abstract_formatter.rb +27 -0
  22. data/lib/pry/commands/cat/exception_formatter.rb +78 -0
  23. data/lib/pry/commands/cat/file_formatter.rb +84 -0
  24. data/lib/pry/commands/cat/input_expression_formatter.rb +43 -0
  25. data/lib/pry/commands/cd.rb +30 -0
  26. data/lib/pry/commands/code_collector.rb +165 -0
  27. data/lib/pry/commands/deprecated_commands.rb +2 -0
  28. data/lib/pry/commands/disable_pry.rb +27 -0
  29. data/lib/pry/commands/easter_eggs.rb +112 -0
  30. data/lib/pry/commands/edit.rb +206 -0
  31. data/lib/pry/commands/edit/exception_patcher.rb +25 -0
  32. data/lib/pry/commands/edit/file_and_line_locator.rb +38 -0
  33. data/lib/pry/commands/edit/method_patcher.rb +122 -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 +24 -0
  37. data/lib/pry/commands/find_method.rb +199 -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 +29 -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 +95 -0
  44. data/lib/pry/commands/help.rb +164 -0
  45. data/lib/pry/commands/hist.rb +161 -0
  46. data/lib/pry/commands/import_set.rb +22 -0
  47. data/lib/pry/commands/install_command.rb +51 -0
  48. data/lib/pry/commands/jump_to.rb +29 -0
  49. data/lib/pry/commands/ls.rb +339 -0
  50. data/lib/pry/commands/nesting.rb +25 -0
  51. data/lib/pry/commands/play.rb +69 -0
  52. data/lib/pry/commands/pry_backtrace.rb +26 -0
  53. data/lib/pry/commands/pry_version.rb +17 -0
  54. data/lib/pry/commands/raise_up.rb +32 -0
  55. data/lib/pry/commands/reload_code.rb +39 -0
  56. data/lib/pry/commands/reset.rb +18 -0
  57. data/lib/pry/commands/ri.rb +56 -0
  58. data/lib/pry/commands/save_file.rb +61 -0
  59. data/lib/pry/commands/shell_command.rb +43 -0
  60. data/lib/pry/commands/shell_mode.rb +27 -0
  61. data/lib/pry/commands/show_doc.rb +78 -0
  62. data/lib/pry/commands/show_info.rb +139 -0
  63. data/lib/pry/commands/show_input.rb +17 -0
  64. data/lib/pry/commands/show_source.rb +37 -0
  65. data/lib/pry/commands/simple_prompt.rb +22 -0
  66. data/lib/pry/commands/stat.rb +40 -0
  67. data/lib/pry/commands/switch_to.rb +23 -0
  68. data/lib/pry/commands/toggle_color.rb +20 -0
  69. data/lib/pry/commands/whereami.rb +114 -0
  70. data/lib/pry/commands/wtf.rb +57 -0
  71. data/lib/pry/completion.rb +120 -46
  72. data/lib/pry/config.rb +11 -0
  73. data/lib/pry/core_extensions.rb +30 -19
  74. data/lib/pry/editor.rb +129 -0
  75. data/lib/pry/helpers.rb +1 -0
  76. data/lib/pry/helpers/base_helpers.rb +89 -119
  77. data/lib/pry/helpers/command_helpers.rb +7 -122
  78. data/lib/pry/helpers/table.rb +100 -0
  79. data/lib/pry/helpers/text.rb +4 -4
  80. data/lib/pry/history_array.rb +5 -0
  81. data/lib/pry/hooks.rb +1 -3
  82. data/lib/pry/indent.rb +104 -30
  83. data/lib/pry/method.rb +66 -22
  84. data/lib/pry/module_candidate.rb +26 -15
  85. data/lib/pry/pager.rb +70 -0
  86. data/lib/pry/plugins.rb +1 -2
  87. data/lib/pry/pry_class.rb +63 -22
  88. data/lib/pry/pry_instance.rb +58 -37
  89. data/lib/pry/rubygem.rb +74 -0
  90. data/lib/pry/terminal_info.rb +43 -0
  91. data/lib/pry/test/helper.rb +185 -0
  92. data/lib/pry/version.rb +1 -1
  93. data/lib/pry/wrapped_module.rb +58 -24
  94. data/pry.gemspec +21 -37
  95. data/{test/test_cli.rb → spec/cli_spec.rb} +0 -0
  96. data/spec/code_object_spec.rb +277 -0
  97. data/{test/test_code.rb → spec/code_spec.rb} +19 -1
  98. data/{test/test_command_helpers.rb → spec/command_helpers_spec.rb} +0 -0
  99. data/{test/test_command_integration.rb → spec/command_integration_spec.rb} +38 -46
  100. data/{test/test_command_set.rb → spec/command_set_spec.rb} +18 -1
  101. data/{test/test_command.rb → spec/command_spec.rb} +250 -149
  102. data/spec/commands/amend_line_spec.rb +247 -0
  103. data/spec/commands/bang_spec.rb +19 -0
  104. data/spec/commands/cat_spec.rb +164 -0
  105. data/spec/commands/cd_spec.rb +250 -0
  106. data/spec/commands/disable_pry_spec.rb +25 -0
  107. data/spec/commands/edit_spec.rb +727 -0
  108. data/spec/commands/exit_all_spec.rb +34 -0
  109. data/spec/commands/exit_program_spec.rb +19 -0
  110. data/spec/commands/exit_spec.rb +34 -0
  111. data/{test/test_default_commands/test_find_method.rb → spec/commands/find_method_spec.rb} +27 -7
  112. data/spec/commands/gem_list_spec.rb +26 -0
  113. data/spec/commands/gist_spec.rb +75 -0
  114. data/{test/test_default_commands/test_help.rb → spec/commands/help_spec.rb} +8 -9
  115. data/spec/commands/hist_spec.rb +181 -0
  116. data/spec/commands/jump_to_spec.rb +15 -0
  117. data/spec/commands/ls_spec.rb +177 -0
  118. data/spec/commands/play_spec.rb +140 -0
  119. data/spec/commands/raise_up_spec.rb +56 -0
  120. data/spec/commands/save_file_spec.rb +177 -0
  121. data/spec/commands/show_doc_spec.rb +378 -0
  122. data/spec/commands/show_input_spec.rb +17 -0
  123. data/spec/commands/show_source_spec.rb +597 -0
  124. data/spec/commands/whereami_spec.rb +154 -0
  125. data/spec/completion_spec.rb +233 -0
  126. data/spec/control_d_handler_spec.rb +58 -0
  127. data/spec/editor_spec.rb +79 -0
  128. data/{test/test_exception_whitelist.rb → spec/exception_whitelist_spec.rb} +0 -0
  129. data/{test → spec/fixtures}/candidate_helper1.rb +0 -0
  130. data/{test → spec/fixtures}/candidate_helper2.rb +0 -0
  131. data/{test/test_default_commands → spec/fixtures}/example.erb +0 -0
  132. data/spec/fixtures/example_nesting.rb +33 -0
  133. data/spec/fixtures/show_source_doc_examples.rb +15 -0
  134. data/{test → spec/fixtures}/testrc +0 -0
  135. data/{test → spec/fixtures}/testrcbad +0 -0
  136. data/spec/helper.rb +34 -0
  137. data/spec/helpers/bacon.rb +86 -0
  138. data/spec/helpers/mock_pry.rb +43 -0
  139. data/spec/helpers/table_spec.rb +83 -0
  140. data/{test/test_history_array.rb → spec/history_array_spec.rb} +21 -19
  141. data/{test/test_hooks.rb → spec/hooks_spec.rb} +0 -0
  142. data/{test/test_indent.rb → spec/indent_spec.rb} +24 -0
  143. data/{test/test_input_stack.rb → spec/input_stack_spec.rb} +4 -0
  144. data/{test/test_method.rb → spec/method_spec.rb} +65 -1
  145. data/{test/test_prompt.rb → spec/prompt_spec.rb} +0 -0
  146. data/{test/test_pry_defaults.rb → spec/pry_defaults_spec.rb} +14 -14
  147. data/{test/test_pry_history.rb → spec/pry_history_spec.rb} +15 -0
  148. data/spec/pry_output_spec.rb +95 -0
  149. data/{test/test_pry.rb → spec/pry_spec.rb} +74 -32
  150. data/{test/test_sticky_locals.rb → spec/sticky_locals_spec.rb} +27 -25
  151. data/{test/test_syntax_checking.rb → spec/syntax_checking_spec.rb} +17 -1
  152. data/{test/test_wrapped_module.rb → spec/wrapped_module_spec.rb} +92 -5
  153. metadata +239 -115
  154. data/examples/example_basic.rb +0 -15
  155. data/examples/example_command_override.rb +0 -32
  156. data/examples/example_commands.rb +0 -36
  157. data/examples/example_hooks.rb +0 -9
  158. data/examples/example_image_edit.rb +0 -67
  159. data/examples/example_input.rb +0 -7
  160. data/examples/example_input2.rb +0 -29
  161. data/examples/example_output.rb +0 -11
  162. data/examples/example_print.rb +0 -6
  163. data/examples/example_prompt.rb +0 -9
  164. data/examples/helper.rb +0 -6
  165. data/lib/pry/default_commands/cd.rb +0 -81
  166. data/lib/pry/default_commands/commands.rb +0 -62
  167. data/lib/pry/default_commands/context.rb +0 -98
  168. data/lib/pry/default_commands/easter_eggs.rb +0 -95
  169. data/lib/pry/default_commands/editing.rb +0 -420
  170. data/lib/pry/default_commands/find_method.rb +0 -169
  171. data/lib/pry/default_commands/gems.rb +0 -84
  172. data/lib/pry/default_commands/gist.rb +0 -187
  173. data/lib/pry/default_commands/help.rb +0 -127
  174. data/lib/pry/default_commands/hist.rb +0 -120
  175. data/lib/pry/default_commands/input_and_output.rb +0 -306
  176. data/lib/pry/default_commands/introspection.rb +0 -410
  177. data/lib/pry/default_commands/ls.rb +0 -272
  178. data/lib/pry/default_commands/misc.rb +0 -38
  179. data/lib/pry/default_commands/navigating_pry.rb +0 -110
  180. data/lib/pry/default_commands/whereami.rb +0 -92
  181. data/lib/pry/extended_commands/experimental.rb +0 -7
  182. data/test/helper.rb +0 -223
  183. data/test/test_completion.rb +0 -62
  184. data/test/test_control_d_handler.rb +0 -45
  185. data/test/test_default_commands/test_cd.rb +0 -321
  186. data/test/test_default_commands/test_context.rb +0 -288
  187. data/test/test_default_commands/test_documentation.rb +0 -315
  188. data/test/test_default_commands/test_gems.rb +0 -18
  189. data/test/test_default_commands/test_input.rb +0 -428
  190. data/test/test_default_commands/test_introspection.rb +0 -511
  191. data/test/test_default_commands/test_ls.rb +0 -151
  192. data/test/test_default_commands/test_shell.rb +0 -343
  193. data/test/test_default_commands/test_show_source.rb +0 -432
  194. data/test/test_pry_output.rb +0 -41
@@ -1,169 +0,0 @@
1
- class Pry
2
- module DefaultCommands
3
- FindMethod = Pry::CommandSet.new do
4
-
5
- create_command "find-method" do
6
- extend Helpers::BaseHelpers
7
-
8
- group "Context"
9
-
10
- options :requires_gem => "ruby18_source_location" if mri_18?
11
-
12
- description "Recursively search for a method within a Class/Module or the current namespace. find-method [-n | -c] METHOD [NAMESPACE]"
13
-
14
- banner <<-BANNER
15
- Usage: find-method [-n | -c] METHOD [NAMESPACE]
16
-
17
- Recursively search for a method within a Class/Module or the current namespace.
18
- Use the `-n` switch (the default) to search for methods whose name matches the given regex.
19
- Use the `-c` switch to search for methods that contain the given code.
20
-
21
- e.g find-method re Pry # find all methods whose name match /re/ inside the Pry namespace. Matches Pry#repl, etc.
22
- e.g find-method -c 'output.puts' Pry # find all methods that contain the code: output.puts inside the Pry namepsace.
23
- BANNER
24
-
25
- def setup
26
- require 'ruby18_source_location' if mri_18?
27
- end
28
-
29
- def options(opti)
30
- opti.on :n, :name, "Search for a method by name"
31
- opti.on :c, :content, "Search for a method based on content in Regex form"
32
- end
33
-
34
- def process
35
- return if args.size < 1
36
- pattern = ::Regexp.new args[0]
37
- if args[1]
38
- klass = target.eval(args[1])
39
- if !klass.is_a?(Module)
40
- klass = klass.class
41
- end
42
- else
43
- klass = (target_self.is_a?(Module)) ? target_self : target_self.class
44
- end
45
-
46
- matches = if opts.content?
47
- content_search(pattern, klass)
48
- else
49
- name_search(pattern, klass)
50
- end
51
-
52
- if matches.empty?
53
- output.puts text.bold("No Methods Matched")
54
- else
55
- print_matches(matches, pattern)
56
- end
57
-
58
- end
59
-
60
- private
61
-
62
- # pretty-print a list of matching methods.
63
- #
64
- # @param Array[Method]
65
- def print_matches(matches, pattern)
66
- grouped = matches.group_by(&:owner)
67
- order = grouped.keys.sort_by{ |x| x.name || x.to_s }
68
-
69
- order.each do |klass|
70
- output.puts text.bold(klass.name)
71
- grouped[klass].each do |method|
72
- header = method.name_with_owner
73
-
74
- extra = if opts.content?
75
- header += ": "
76
- colorize_code((method.source.split(/\n/).select {|x| x =~ pattern }).join("\n#{' ' * header.length}"))
77
- else
78
- ""
79
- end
80
-
81
- output.puts header + extra
82
- end
83
- end
84
- end
85
-
86
- # Run the given block against every constant in the provided namespace.
87
- #
88
- # @param Module The namespace in which to start the search.
89
- # @param Hash[Module,Boolean] The namespaces we've already visited (private)
90
- # @yieldparam klazz Each class/module in the namespace.
91
- #
92
- def recurse_namespace(klass, done={}, &block)
93
- return if !(Module === klass) || done[klass]
94
-
95
- done[klass] = true
96
-
97
- yield klass
98
-
99
- klass.constants.each do |name|
100
- next if klass.autoload?(name)
101
- begin
102
- const = klass.const_get(name)
103
- rescue RescuableException
104
- # constant loading is an inexact science at the best of times,
105
- # this often happens when a constant was .autoload? but someone
106
- # tried to load it. It's now not .autoload? but will still raise
107
- # a NameError when you access it.
108
- else
109
- recurse_namespace(const, done, &block)
110
- end
111
- end
112
- end
113
-
114
- # Gather all the methods in a namespace that pass the given block.
115
- #
116
- # @param Module The namespace in which to search.
117
- # @yieldparam Method The method to test
118
- # @yieldreturn Boolean
119
- # @return Array[Method]
120
- #
121
- def search_all_methods(namespace)
122
- done = Hash.new{ |h,k| h[k] = {} }
123
- matches = []
124
-
125
- recurse_namespace(namespace) do |klass|
126
- (Pry::Method.all_from_class(klass) + Pry::Method.all_from_obj(klass)).each do |method|
127
- next if done[method.owner][method.name]
128
- done[method.owner][method.name] = true
129
-
130
- matches << method if yield method
131
- end
132
- end
133
-
134
- matches
135
- end
136
-
137
- # Search for all methods with a name that matches the given regex
138
- # within a namespace.
139
- #
140
- # @param Regex The regex to search for
141
- # @param Module The namespace to search
142
- # @return Array[Method]
143
- #
144
- def name_search(regex, namespace)
145
- search_all_methods(namespace) do |meth|
146
- meth.name =~ regex
147
- end
148
- end
149
-
150
- # Search for all methods who's implementation matches the given regex
151
- # within a namespace.
152
- #
153
- # @param Regex The regex to search for
154
- # @param Module The namespace to search
155
- # @return Array[Method]
156
- #
157
- def content_search(regex, namespace)
158
- search_all_methods(namespace) do |meth|
159
- begin
160
- meth.source =~ regex
161
- rescue RescuableException
162
- false
163
- end
164
- end
165
- end
166
- end
167
- end
168
- end
169
- end
@@ -1,84 +0,0 @@
1
- class Pry
2
- module DefaultCommands
3
-
4
- Gems = Pry::CommandSet.new do
5
-
6
- create_command "gem-install", "Install a gem and refresh the gem cache.", :argument_required => true do |gem|
7
-
8
- banner <<-BANNER
9
- Usage: gem-install GEM_NAME
10
-
11
- Installs the given gem and refreshes the gem cache so that you can immediately 'require GEM_FILE'
12
- BANNER
13
-
14
- def setup
15
- require 'rubygems/dependency_installer' unless defined? Gem::DependencyInstaller
16
- end
17
-
18
- def process(gem)
19
- begin
20
- destination = File.writable?(Gem.dir) ? Gem.dir : Gem.user_dir
21
- installer = Gem::DependencyInstaller.new :install_dir => destination
22
- installer.install gem
23
- rescue Errno::EACCES
24
- raise CommandError, "Insufficient permissions to install `#{text.green gem}`."
25
- rescue Gem::GemNotFoundException
26
- raise CommandError, "Gem `#{text.green gem}` not found."
27
- else
28
- Gem.refresh
29
- output.puts "Gem `#{text.green gem}` installed."
30
- end
31
- end
32
- end
33
-
34
- create_command "gem-cd", "Change working directory to specified gem's directory.", :argument_required => true do |gem|
35
- banner <<-BANNER
36
- Usage: gem-cd GEM_NAME
37
-
38
- Change the current working directory to that in which the given gem is installed.
39
- BANNER
40
-
41
- def process(gem)
42
- specs = Gem::Specification.respond_to?(:each) ? Gem::Specification.find_all_by_name(gem) : Gem.source_index.find_name(gem)
43
- spec = specs.sort { |a,b| Gem::Version.new(b.version) <=> Gem::Version.new(a.version) }.first
44
- if spec
45
- Dir.chdir(spec.full_gem_path)
46
- output.puts(Dir.pwd)
47
- else
48
- raise CommandError, "Gem `#{gem}` not found."
49
- end
50
- end
51
- end
52
-
53
- create_command "gem-list", "List and search installed gems." do |pattern|
54
- banner <<-BANNER
55
- Usage: gem-list [REGEX]
56
-
57
- List all installed gems, when a regex is provided, limit the output to those that
58
- match the regex.
59
- BANNER
60
-
61
- def process(pattern=nil)
62
- pattern = Regexp.compile(pattern || '')
63
- gems = if Gem::Specification.respond_to?(:each)
64
- Gem::Specification.select{|spec| spec.name =~ pattern }.group_by(&:name)
65
- else
66
- Gem.source_index.gems.values.group_by(&:name).select { |gemname, specs| gemname =~ pattern }
67
- end
68
-
69
- gems.each do |gem, specs|
70
- specs.sort! do |a,b|
71
- Gem::Version.new(b.version) <=> Gem::Version.new(a.version)
72
- end
73
-
74
- versions = specs.each_with_index.map do |spec, index|
75
- index == 0 ? text.bright_green(spec.version.to_s) : text.green(spec.version.to_s)
76
- end
77
-
78
- output.puts "#{text.default gem} (#{versions.join ', '})"
79
- end
80
- end
81
- end
82
- end
83
- end
84
- end
@@ -1,187 +0,0 @@
1
- class Pry
2
- module DefaultCommands
3
- Gist = Pry::CommandSet.new do
4
- create_command "gist", "Gist a method or expression history to GitHub.", :requires_gem => "jist" do
5
- include Pry::Helpers::DocumentationHelpers
6
-
7
- banner <<-USAGE
8
- Usage: gist [OPTIONS] [METH]
9
- Gist method (doc or source) or input expression to GitHub.
10
-
11
- If you'd like to permanently associate your gists with your GitHub account run `gist --login`.
12
-
13
- e.g: gist -m my_method # gist the method my_method
14
- e.g: gist -d my_method # gist the documentation for my_method
15
- e.g: gist -i 1..10 # gist the input expressions from 1 to 10
16
- e.g: gist -k show-method # gist the command show-method
17
- e.g: gist -c Pry # gist the Pry class
18
- e.g: gist -m hello_world --lines 2..-2 # gist from lines 2 to the second-last of the hello_world method
19
- e.g: gist -m my_method --clip # Copy my_method source to clipboard, do not gist it.
20
- USAGE
21
-
22
- command_options :shellwords => false
23
-
24
- attr_accessor :content
25
- attr_accessor :filename
26
-
27
- def setup
28
- require 'jist'
29
- self.content = ""
30
- self.filename = "a.rb"
31
- end
32
-
33
- def options(opt)
34
- opt.on :login, "Authenticate the jist gem with GitHub"
35
- opt.on :m, :method, "Gist a method's source.", :argument => true do |meth_name|
36
- meth = get_method_or_raise(meth_name, target, {})
37
- self.content << meth.source << "\n"
38
- self.filename = meth.source_file
39
- end
40
- opt.on :d, :doc, "Gist a method's documentation.", :argument => true do |meth_name|
41
- meth = get_method_or_raise(meth_name, target, {})
42
- text.no_color do
43
- self.content << process_comment_markup(meth.doc) << "\n"
44
- end
45
- self.filename = meth.source_file + ".doc"
46
- end
47
- opt.on :k, :command, "Gist a command's source.", :argument => true do |command_name|
48
- command = find_command(command_name)
49
- block = Pry::Method.new(command.block)
50
- self.content << block.source << "\n"
51
- self.filename = block.source_file
52
- end
53
- opt.on :c, :class, "Gist a class or module's source.", :argument => true do |class_name|
54
- mod = Pry::WrappedModule.from_str(class_name, target)
55
- self.content << mod.source << "\n"
56
- self.filename = mod.source_file
57
- end
58
- opt.on :var, "Gist a variable's content.", :argument => true do |variable_name|
59
- begin
60
- obj = target.eval(variable_name)
61
- rescue Pry::RescuableException
62
- raise CommandError, "Gist failed: Invalid variable name: #{variable_name}"
63
- end
64
-
65
- self.content << Pry.config.gist.inspecter.call(obj) << "\n"
66
- end
67
- opt.on :hist, "Gist a range of Readline history lines.", :optional_argument => true, :as => Range, :default => -20..-1 do |range|
68
- h = Pry.history.to_a
69
- self.content << h[one_index_range(convert_to_range(range))].join("\n") << "\n"
70
- end
71
-
72
- opt.on :f, :file, "Gist a file.", :argument => true do |file|
73
- self.content << File.read(File.expand_path(file)) << "\n"
74
- self.filename = file
75
- end
76
- opt.on :o, :out, "Gist entries from Pry's output result history. Takes an index or range.", :optional_argument => true,
77
- :as => Range, :default => -1 do |range|
78
- range = convert_to_range(range)
79
-
80
- range.each do |v|
81
- self.content << Pry.config.gist.inspecter.call(_pry_.output_array[v])
82
- end
83
-
84
- self.content << "\n"
85
- end
86
- opt.on :clip, "Copy the selected content to clipboard instead, do NOT gist it.", :default => false
87
- opt.on :p, :public, "Create a public gist (default: false)", :default => false
88
- opt.on :l, :lines, "Only gist a subset of lines from the gistable content.", :optional_argument => true, :as => Range, :default => 1..-1
89
- opt.on :i, :in, "Gist entries from Pry's input expression history. Takes an index or range.", :optional_argument => true,
90
- :as => Range, :default => -1 do |range|
91
- range = convert_to_range(range)
92
- input_expressions = _pry_.input_array[range] || []
93
- Array(input_expressions).each_with_index do |code, index|
94
- corrected_index = index + range.first
95
- if code && code != ""
96
- self.content << code
97
- if code !~ /;\Z/
98
- self.content << "#{comment_expression_result_for_gist(Pry.config.gist.inspecter.call(_pry_.output_array[corrected_index]))}"
99
- end
100
- end
101
- end
102
- end
103
- end
104
-
105
- def process
106
- return Jist.login! if opts.present?(:login)
107
-
108
- if self.content =~ /\A\s*\z/
109
- raise CommandError, "Found no code to gist."
110
- end
111
-
112
- if opts.present?(:clip)
113
- perform_clipboard
114
- else
115
- perform_gist
116
- end
117
- end
118
-
119
- # copy content to clipboard instead (only used with --clip flag)
120
- def perform_clipboard
121
- copy(self.content)
122
- output.puts "Copied content to clipboard!"
123
- end
124
-
125
- def perform_gist
126
- if opts.present?(:lines)
127
- self.content = restrict_to_lines(content, opts[:l])
128
- end
129
-
130
- response = Jist.gist(content, :filename => File.basename(filename),
131
- :public => !!opts[:p])
132
-
133
- if response
134
- copy(response['html_url'])
135
- output.puts "Gist created at #{response['html_url']} and added to clipboard."
136
- end
137
- end
138
-
139
- def convert_to_range(n)
140
- if !n.is_a?(Range)
141
- (n..n)
142
- else
143
- n
144
- end
145
- end
146
-
147
- def comment_expression_result_for_gist(result)
148
- content = ""
149
- result.lines.each_with_index do |line, index|
150
- if index == 0
151
- content << "# => #{line}"
152
- else
153
- content << "# #{line}"
154
- end
155
- end
156
- content
157
- end
158
-
159
- # Copy a string to the clipboard.
160
- #
161
- # @param [String] content
162
- #
163
- # @copyright Copyright (c) 2008 Chris Wanstrath (MIT)
164
- # @see https://github.com/defunkt/gist/blob/master/lib/gist.rb#L178
165
- def copy(content)
166
- cmd = case true
167
- when system("type pbcopy > /dev/null 2>&1")
168
- :pbcopy
169
- when system("type xclip > /dev/null 2>&1")
170
- :xclip
171
- when system("type putclip > /dev/null 2>&1")
172
- :putclip
173
- end
174
-
175
- if cmd
176
- IO.popen(cmd.to_s, 'r+') { |clip| clip.print content }
177
- end
178
-
179
- content
180
- end
181
- end
182
-
183
- alias_command "clipit", "gist --clip"
184
- alias_command "jist", "gist"
185
- end
186
- end
187
- end
@@ -1,127 +0,0 @@
1
- class Pry
2
- module DefaultCommands
3
- Help = Pry::CommandSet.new do
4
- create_command "help" do |cmd|
5
- description "Show a list of commands. Type `help <foo>` for information about <foo>."
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.
12
-
13
- When given a command name as an argument, shows the help for that command.
14
- BANNER
15
-
16
- # We only want to show commands that have descriptions, so that the
17
- # easter eggs don't show up.
18
- def visible_commands
19
- visible = {}
20
- commands.each do |key, command|
21
- visible[key] = command if command.description && !command.description.empty?
22
- end
23
- visible
24
- end
25
-
26
- # Get a hash of available commands grouped by the "group" name.
27
- def command_groups
28
- visible_commands.values.group_by(&:group)
29
- end
30
-
31
- def process
32
- if args.empty?
33
- display_index(command_groups)
34
- else
35
- display_search(args.first)
36
- end
37
- end
38
-
39
- # Display the index view, with headings and short descriptions per command.
40
- #
41
- # @param Hash[String => Array[Commands]]
42
- def display_index(groups)
43
- help_text = []
44
-
45
- groups.keys.sort_by(&method(:group_sort_key)).each do |key|
46
- commands = groups[key].sort_by{ |command| command.options[:listing].to_s }
47
-
48
- unless commands.empty?
49
- help_text << "#{text.bold(key)}\n" + commands.map do |command|
50
- " #{command.options[:listing].to_s.ljust(18)} #{command.description}"
51
- end.join("\n")
52
- end
53
- end
54
-
55
- stagger_output(help_text.join("\n\n"))
56
- end
57
-
58
- # Display help for an individual command or group.
59
- #
60
- # @param String The string to search for.
61
- def display_search(search)
62
- if command = command_set.find_command_for_help(search)
63
- display_command(command)
64
- else
65
- groups = search_hash(search, command_groups)
66
-
67
- if groups.size > 0
68
- display_index(groups)
69
- return
70
- end
71
-
72
- filtered = search_hash(search, visible_commands)
73
- raise CommandError, "No help found for '#{args.first}'" if filtered.empty?
74
-
75
- if filtered.size == 1
76
- display_command(filtered.values.first)
77
- else
78
- display_index({"'#{search}' commands" => filtered.values})
79
- end
80
- end
81
- end
82
-
83
- # Display help for an individual command.
84
- #
85
- # @param [Pry::Command]
86
- def display_command(command)
87
- stagger_output command.new.help
88
- end
89
-
90
- # Find a subset of a hash that matches the user's search term.
91
- #
92
- # If there's an exact match a Hash of one element will be returned,
93
- # otherwise a sub-Hash with every key that matches the search will
94
- # be returned.
95
- #
96
- # @param [String] the search term
97
- # @param [Hash] the hash to search
98
- def search_hash(search, hash)
99
- matching = {}
100
-
101
- hash.each_pair do |key, value|
102
- next unless key.is_a?(String)
103
- if normalize(key) == normalize(search)
104
- return {key => value}
105
- elsif normalize(key).start_with?(normalize(search))
106
- matching[key] = value
107
- end
108
- end
109
-
110
- matching
111
- end
112
-
113
- # Clean search terms to make it easier to search group names
114
- #
115
- # @param String
116
- # @return String
117
- def normalize(key)
118
- key.downcase.gsub(/pry\W+/, '')
119
- end
120
-
121
- def group_sort_key(group_name)
122
- [%w(Help Context Editing Introspection Input_and_output Navigating_pry Gems Basic Commands).index(group_name.gsub(' ', '_')) || 99, group_name]
123
- end
124
- end
125
- end
126
- end
127
- end