pry 0.9.10pre1-i386-mswin32 → 0.9.11-i386-mswin32

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
@@ -0,0 +1,24 @@
1
+ class Pry
2
+ class Command::ExitProgram < Pry::ClassCommand
3
+ match 'exit-program'
4
+ group 'Navigating Pry'
5
+ description 'End the current program.'
6
+
7
+ banner <<-'BANNER'
8
+ Usage: exit-program [--help]
9
+ Aliases: quit-program
10
+ !!!
11
+
12
+ End the current program.
13
+ BANNER
14
+
15
+ def process
16
+ Pry.save_history if Pry.config.history.should_save
17
+ Kernel.exit target.eval(arg_string).to_i
18
+ end
19
+ end
20
+
21
+ Pry::Commands.add_command(Pry::Command::ExitProgram)
22
+ Pry::Commands.alias_command 'quit-program', 'exit-program'
23
+ Pry::Commands.alias_command '!!!', 'exit-program'
24
+ end
@@ -0,0 +1,199 @@
1
+ class Pry
2
+ class Command::FindMethod < Pry::ClassCommand
3
+ extend Pry::Helpers::BaseHelpers
4
+
5
+ match 'find-method'
6
+ group 'Context'
7
+ description 'Recursively search for a method within a Class/Module or the current namespace.'
8
+ command_options :shellwords => false
9
+ command_options :requires_gem => 'ruby18_source_location' if mri_18?
10
+
11
+
12
+ banner <<-'BANNER'
13
+ Usage: find-method [-n|-c] METHOD [NAMESPACE]
14
+
15
+ Recursively search for a method within a Class/Module or the current namespace.
16
+ Use the `-n` switch (the default) to search for methods whose name matches the
17
+ given regex. Use the `-c` switch to search for methods that contain the given
18
+ code.
19
+
20
+ # Find all methods whose name match /re/ inside
21
+ # the Pry namespace. Matches Pry#repl, etc.
22
+ find-method re Pry
23
+
24
+ # Find all methods that contain the code:
25
+ # output.puts inside the Pry namepsace.
26
+ find-method -c 'output.puts' Pry
27
+ BANNER
28
+
29
+ def setup
30
+ require 'ruby18_source_location' if mri_18?
31
+ end
32
+
33
+ def options(opti)
34
+ opti.on :n, :name, "Search for a method by name"
35
+ opti.on :c, :content, "Search for a method based on content in Regex form"
36
+ end
37
+
38
+ def process
39
+ return if args.size < 1
40
+ klass = search_class
41
+
42
+ matches = if opts.content?
43
+ content_search(klass)
44
+ else
45
+ name_search(klass)
46
+ end
47
+
48
+ show_search_results(matches)
49
+ end
50
+
51
+ private
52
+
53
+ # @return [Regexp] The pattern to search for.
54
+ def pattern
55
+ @pattern ||= ::Regexp.new args[0]
56
+ end
57
+
58
+ # Output the result of the search.
59
+ #
60
+ # @param [Array] matches
61
+ def show_search_results(matches)
62
+ if matches.empty?
63
+ output.puts text.bold("No Methods Matched")
64
+ else
65
+ print_matches(matches)
66
+ end
67
+ end
68
+
69
+ # The class to search for methods.
70
+ # We only search classes, so if the search object is an
71
+ # instance, return its class. If no search object is given
72
+ # search `target_self`.
73
+ def search_class
74
+ klass = if args[1]
75
+ target.eval(args[1])
76
+ else
77
+ target_self
78
+ end
79
+
80
+ klass.is_a?(Module) ? klass : klass.class
81
+ end
82
+
83
+ # pretty-print a list of matching methods.
84
+ #
85
+ # @param Array[Method]
86
+ def print_matches(matches)
87
+ grouped = matches.group_by(&:owner)
88
+ order = grouped.keys.sort_by{ |x| x.name || x.to_s }
89
+
90
+ order.each do |klass|
91
+ print_matches_for_class(klass, grouped)
92
+ end
93
+ end
94
+
95
+ # Print matched methods for a class
96
+ def print_matches_for_class(klass, grouped)
97
+ output.puts text.bold(klass.name)
98
+ grouped[klass].each do |method|
99
+ header = method.name_with_owner
100
+ output.puts header + additional_info(header, method)
101
+ end
102
+ end
103
+
104
+ # Return the matched lines of method source if `-c` is given or ""
105
+ # if `-c` was not given
106
+ def additional_info(header, method)
107
+ if opts.content?
108
+ ": " + colorize_code(matched_method_lines(header, method))
109
+ else
110
+ ""
111
+ end
112
+ end
113
+
114
+ def matched_method_lines(header, method)
115
+ method.source.split(/\n/).select {|x| x =~ pattern }.join("\n#{' ' * header.length}")
116
+ end
117
+
118
+ # Run the given block against every constant in the provided namespace.
119
+ #
120
+ # @param Module The namespace in which to start the search.
121
+ # @param Hash[Module,Boolean] The namespaces we've already visited (private)
122
+ # @yieldparam klazz Each class/module in the namespace.
123
+ #
124
+ def recurse_namespace(klass, done={}, &block)
125
+ return if !(Module === klass) || done[klass]
126
+
127
+ done[klass] = true
128
+
129
+ yield klass
130
+
131
+ klass.constants.each do |name|
132
+ next if klass.autoload?(name)
133
+ begin
134
+ const = klass.const_get(name)
135
+ rescue RescuableException
136
+ # constant loading is an inexact science at the best of times,
137
+ # this often happens when a constant was .autoload? but someone
138
+ # tried to load it. It's now not .autoload? but will still raise
139
+ # a NameError when you access it.
140
+ else
141
+ recurse_namespace(const, done, &block)
142
+ end
143
+ end
144
+ end
145
+
146
+ # Gather all the methods in a namespace that pass the given block.
147
+ #
148
+ # @param Module The namespace in which to search.
149
+ # @yieldparam Method The method to test
150
+ # @yieldreturn Boolean
151
+ # @return Array[Method]
152
+ #
153
+ def search_all_methods(namespace)
154
+ done = Hash.new{ |h,k| h[k] = {} }
155
+ matches = []
156
+
157
+ recurse_namespace(namespace) do |klass|
158
+ (Pry::Method.all_from_class(klass) + Pry::Method.all_from_obj(klass)).each do |method|
159
+ next if done[method.owner][method.name]
160
+ done[method.owner][method.name] = true
161
+
162
+ matches << method if yield method
163
+ end
164
+ end
165
+
166
+ matches
167
+ end
168
+
169
+ # Search for all methods with a name that matches the given regex
170
+ # within a namespace.
171
+ #
172
+ # @param Module The namespace to search
173
+ # @return Array[Method]
174
+ #
175
+ def name_search(namespace)
176
+ search_all_methods(namespace) do |meth|
177
+ meth.name =~ pattern
178
+ end
179
+ end
180
+
181
+ # Search for all methods who's implementation matches the given regex
182
+ # within a namespace.
183
+ #
184
+ # @param Module The namespace to search
185
+ # @return Array[Method]
186
+ #
187
+ def content_search(namespace)
188
+ search_all_methods(namespace) do |meth|
189
+ begin
190
+ meth.source =~ pattern
191
+ rescue RescuableException
192
+ false
193
+ end
194
+ end
195
+ end
196
+ end
197
+
198
+ Pry::Commands.add_command(Pry::Command::FindMethod)
199
+ end
@@ -0,0 +1,19 @@
1
+ class Pry
2
+ class Command::FixIndent < Pry::ClassCommand
3
+ match 'fix-indent'
4
+ group 'Input and Output'
5
+
6
+ description "Correct the indentation for contents of the input buffer"
7
+
8
+ banner <<-USAGE
9
+ Usage: fix-indent
10
+ USAGE
11
+
12
+ def process
13
+ indented_str = Pry::Indent.indent(eval_string)
14
+ eval_string.replace indented_str
15
+ end
16
+ end
17
+
18
+ Pry::Commands.add_command(Pry::Command::FixIndent)
19
+ end
@@ -0,0 +1,26 @@
1
+ class Pry
2
+ class Command::GemCd < Pry::ClassCommand
3
+ match 'gem-cd'
4
+ group 'Gems'
5
+ description "Change working directory to specified gem's directory."
6
+ command_options :argument_required => true
7
+
8
+ banner <<-'BANNER'
9
+ Usage: gem-cd GEM_NAME
10
+
11
+ Change the current working directory to that in which the given gem is
12
+ installed.
13
+ BANNER
14
+
15
+ def process(gem)
16
+ Dir.chdir(Rubygem.spec(gem).full_gem_path)
17
+ output.puts(Dir.pwd)
18
+ end
19
+
20
+ def complete(str)
21
+ Rubygem.complete(str)
22
+ end
23
+ end
24
+
25
+ Pry::Commands.add_command(Pry::Command::GemCd)
26
+ end
@@ -0,0 +1,29 @@
1
+ class Pry
2
+ class Command::GemInstall < Pry::ClassCommand
3
+ match 'gem-install'
4
+ group 'Gems'
5
+ description 'Install a gem and refresh the gem cache.'
6
+ command_options :argument_required => true
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
12
+ 'require GEM_FILE'.
13
+
14
+ gem-install pry-stack_explorer
15
+ BANNER
16
+
17
+ def setup
18
+ require 'rubygems/dependency_installer' unless defined? Gem::DependencyInstaller
19
+ end
20
+
21
+ def process(gem)
22
+ Rubygem.install(gem)
23
+ output.puts "Gem `#{ text.green(gem) }` installed."
24
+ require gem
25
+ end
26
+ end
27
+
28
+ Pry::Commands.add_command(Pry::Command::GemInstall)
29
+ end
@@ -0,0 +1,33 @@
1
+ class Pry
2
+ class Command::GemList < Pry::ClassCommand
3
+ match 'gem-list'
4
+ group 'Gems'
5
+ description 'List and search installed gems.'
6
+
7
+ banner <<-'BANNER'
8
+ Usage: gem-list [REGEX]
9
+
10
+ List all installed gems, when a regex is provided, limit the output to those
11
+ that match the regex.
12
+ BANNER
13
+
14
+ def process(pattern = nil)
15
+ pattern = Regexp.compile(pattern || '')
16
+ gems = Rubygem.list(pattern).group_by(&:name)
17
+
18
+ gems.each do |gem, specs|
19
+ specs.sort! do |a,b|
20
+ Gem::Version.new(b.version) <=> Gem::Version.new(a.version)
21
+ end
22
+
23
+ versions = specs.each_with_index.map do |spec, index|
24
+ index == 0 ? text.bright_green(spec.version.to_s) : text.green(spec.version.to_s)
25
+ end
26
+
27
+ output.puts "#{text.default gem} (#{versions.join ', '})"
28
+ end
29
+ end
30
+ end
31
+
32
+ Pry::Commands.add_command(Pry::Command::GemList)
33
+ end
@@ -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,95 @@
1
+ class Pry
2
+ class Command::Gist < Pry::ClassCommand
3
+ match 'gist'
4
+ group 'Misc'
5
+ description 'Playback a string variable or a method or a file as input.'
6
+
7
+ banner <<-'BANNER'
8
+ Usage: gist [OPTIONS] [--help]
9
+
10
+ The gist command enables you to gist code from files and methods to github.
11
+
12
+ gist -i 20 --lines 1..3
13
+ gist Pry#repl --lines 1..-1
14
+ gist Rakefile --lines 5
15
+ BANNER
16
+
17
+ def setup
18
+ require 'jist'
19
+ end
20
+
21
+ def options(opt)
22
+ CodeCollector.inject_options(opt)
23
+ opt.on :login, "Authenticate the jist gem with GitHub"
24
+ opt.on :p, :public, "Create a public gist (default: false)", :default => false
25
+ opt.on :clip, "Copy the selected content to clipboard instead, do NOT gist it", :default => false
26
+ end
27
+
28
+ def process
29
+ return Jist.login! if opts.present?(:login)
30
+ cc = CodeCollector.new(args, opts, _pry_)
31
+
32
+ if cc.content =~ /\A\s*\z/
33
+ raise CommandError, "Found no code to gist."
34
+ end
35
+
36
+ if opts.present?(:clip)
37
+ clipboard_content(cc.content)
38
+ else
39
+ # we're overriding the default behavior of the 'in' option (as
40
+ # defined on CodeCollector) with our local behaviour.
41
+ content = opts.present?(:in) ? input_content : cc.content
42
+ gist_content content, cc.file
43
+ end
44
+ end
45
+
46
+ def clipboard_content(content)
47
+ Jist.copy(content)
48
+ output.puts "Copied content to clipboard!"
49
+ end
50
+
51
+ def input_content
52
+ content = ""
53
+ CodeCollector.input_expression_ranges.each do |range|
54
+ input_expressions = _pry_.input_array[range] || []
55
+ Array(input_expressions).each_with_index do |code, index|
56
+ corrected_index = index + range.first
57
+ if code && code != ""
58
+ content << code
59
+ if code !~ /;\Z/
60
+ content << "#{comment_expression_result_for_gist(Pry.config.gist.inspecter.call(_pry_.output_array[corrected_index]))}"
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ content
67
+ end
68
+
69
+ def comment_expression_result_for_gist(result)
70
+ content = ""
71
+ result.lines.each_with_index do |line, index|
72
+ if index == 0
73
+ content << "# => #{line}"
74
+ else
75
+ content << "# #{line}"
76
+ end
77
+ end
78
+
79
+ content
80
+ end
81
+
82
+ def gist_content(content, filename)
83
+ response = Jist.gist(content, :filename => filename || "pry_gist.rb", :public => !!opts[:p])
84
+ if response
85
+ url = response['html_url']
86
+ Jist.copy(url)
87
+ output.puts 'Gist created at URL, which is now in the clipboard: ', url
88
+ end
89
+ end
90
+ end
91
+
92
+ Pry::Commands.add_command(Pry::Command::Gist)
93
+ Pry::Commands.alias_command 'clipit', 'gist --clip'
94
+ Pry::Commands.alias_command 'jist', 'gist'
95
+ end