timocratic-utility_belt 1.0.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/History.txt +5 -0
  2. data/Manifest.txt +7 -0
  3. data/README +356 -0
  4. data/bin/amazon +16 -0
  5. data/bin/google +7 -0
  6. data/bin/pastie +7 -0
  7. data/html/andreas00.css +449 -0
  8. data/html/authorship.html +86 -0
  9. data/html/bg.gif +0 -0
  10. data/html/front.jpg +0 -0
  11. data/html/index.html +81 -0
  12. data/html/menubg.gif +0 -0
  13. data/html/menubg2.gif +0 -0
  14. data/html/test.jpg +0 -0
  15. data/html/usage.html +304 -0
  16. data/lib/utility_belt/amazon_upload_shortcut.rb +25 -0
  17. data/lib/utility_belt/clipboard.rb +52 -0
  18. data/lib/utility_belt/command_history.rb +110 -0
  19. data/lib/utility_belt/convertable_to_file.rb +34 -0
  20. data/lib/utility_belt/equipper.rb +71 -0
  21. data/lib/utility_belt/google.rb +33 -0
  22. data/lib/utility_belt/hash_math.rb +13 -0
  23. data/lib/utility_belt/interactive_editor.rb +78 -0
  24. data/lib/utility_belt/irb_options.rb +3 -0
  25. data/lib/utility_belt/irb_verbosity_control.rb +30 -0
  26. data/lib/utility_belt/is_an.rb +4 -0
  27. data/lib/utility_belt/language_greps.rb +28 -0
  28. data/lib/utility_belt/not.rb +15 -0
  29. data/lib/utility_belt/pastie.rb +34 -0
  30. data/lib/utility_belt/pipe.rb +24 -0
  31. data/lib/utility_belt/rails_finder_shortcut.rb +18 -0
  32. data/lib/utility_belt/rails_verbosity_control.rb +8 -0
  33. data/lib/utility_belt/string_to_proc.rb +72 -0
  34. data/lib/utility_belt/symbol_to_proc.rb +30 -0
  35. data/lib/utility_belt/wirble.rb +83 -0
  36. data/lib/utility_belt/with.rb +21 -0
  37. data/lib/utility_belt.rb +22 -0
  38. data/spec/convertable_to_file_spec.rb +31 -0
  39. data/spec/equipper_spec.rb +70 -0
  40. data/spec/hash_math_spec.rb +17 -0
  41. data/spec/interactive_editor_spec.rb +146 -0
  42. data/spec/language_greps_spec.rb +9 -0
  43. data/spec/pastie_spec.rb +92 -0
  44. data/spec/pipe_spec.rb +30 -0
  45. data/spec/spec_helper.rb +8 -0
  46. data/spec/string_to_proc_spec.rb +41 -0
  47. data/spec/utility_belt_spec.rb +4 -0
  48. metadata +141 -0
data/html/usage.html ADDED
@@ -0,0 +1,304 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
+ <head>
5
+ <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
6
+ <meta name="description" content="Baddest Ruby gem evar." />
7
+ <meta name="keywords" content="ruby, giles bowkett, batman, utility belt, gem, attack of the killer tomatoes" />
8
+ <meta name="author" content="Giles Bowkett / Original design: Andreas Viklund - http://andreasviklund.com/" />
9
+ <link rel="stylesheet" type="text/css" href="andreas00.css" media="screen,projection" />
10
+ <title>Utility Belt: Usage</title>
11
+ </head>
12
+
13
+ <body>
14
+ <div id="wrap">
15
+ <div id="header">
16
+ <h1><a href="http://utilitybelt.rubyforge.org">Utility Belt</a></h1>
17
+ <p><strong>IRB tools, tricks, and techniques</strong></p>
18
+ </div>
19
+
20
+ <div id="avmenu">
21
+ <h2 class="hide">Site menu:</h2>
22
+ <ul>
23
+ <li><a href="index.html">Overview</a></li>
24
+ <li><a href="usage.html">Usage</a></li>
25
+ <li><a href="authorship.html">Authorship</a></li>
26
+ <li><a href="http://rubyforge.org/projects/utilitybelt/">RubyForge Project</a></li>
27
+ </ul>
28
+
29
+ </div>
30
+
31
+ <div id="contentwide">
32
+ <h2>Synopsis And Usage</h2>
33
+
34
+
35
+ <h3>SYNOPSIS</h3>
36
+
37
+ <p>Utility Belt gives you a ton of new options and techniques in IRB. (You may find yourself putting its language patches in actual projects as well.) The way to use Utility Belt is to edit, or create, your .irbrc file. This is analogous to a Unix .bashrc, .tcshrc, .profile, or similar file.</p>
38
+
39
+ <p>Windows users have a couple options. The easiest is to define an environment variable called IRBRC and set it to the full path of your irbrc (e.g. "C:\Documents and Settings\<username>\_irbrc"). Presto, .irbrc for Windows. You could also set your HOME-Variable to any directory you want and put your ".irbrc" in there. (It needs to be called ".irbrc", "_irbrc" won't work). Since Windows Explorer won't let you create a file with a dot in the beginning, use any text editor (Notepad will do just fine) and "Save As" to save it as ".irbrc". (The quotation marks make the magic happen.) We don't recommend this if you're also using MSYS or Cygwin, as they use the same variable for their own purposes.</p>
40
+
41
+ <p>Adding Utility Belt to your .irbrc is easy:</p>
42
+
43
+ <pre>require 'rubygems'
44
+ require 'utility_belt'</pre>
45
+
46
+ <p>To use the Amazon shortcuts, you also need to set some environment variables. See the section on S3 under "Usage" for more. Also, if your terminal has a light background, you'll need to enable the light-background syntax coloring theme - see the section on themes under "Usage" for more.</p>
47
+
48
+ <h3>USAGE</h3>
49
+
50
+ <p>Using each of Utility Belt's tools is different:</p>
51
+
52
+ <h4>Interactively edit IRB code in your preferred text editor</h4>
53
+
54
+ <p>If your preferred text editor is vi(m), emacs, or TextMate, there are built-in commands: vi, emacs, and mate, respectively. To use another editor, use this command:</p>
55
+
56
+ <pre>edit_interactively(:the_name_of_my_editor)</pre>
57
+
58
+ <p>or</p>
59
+
60
+ <pre>edit_interactively("the name of my editor")</pre>
61
+
62
+ <p>or</p>
63
+
64
+ <pre>edit_interactively("/the/path/to/my/editor")</pre>
65
+
66
+ <p>If you're wondering why the method name is so ridiculously long, <a href="http://gilesbowkett.blogspot.com/2007/12/utility-belt-vs-rails.html" title="Giles Bowkett: Utility Belt vs. Rails">there's a story there</a>.</p>
67
+
68
+ <h4>Read from and write to OS X or Windows clipboard</h4>
69
+
70
+ <p>To read:</p>
71
+
72
+ <pre>Clipboard.read</pre>
73
+
74
+ <p>To write:</p>
75
+
76
+ <pre>Clipboard.write("something")</pre>
77
+
78
+ <h4>Post your code to Pastie with one command (OS X and Windows only)</h4>
79
+
80
+ <p>You must first equip the UtilityBelt with pastie in your .irbrc:</p>
81
+
82
+ <pre>UtilityBelt.equip(:pastie)</pre>
83
+
84
+ <p>This line will also automatically equip the Clipboard, which is required to use pastie.</p>
85
+
86
+ <p>First copy some code into your clipboard, or put it there with the Clipboard.write() class method. Then use the command:</p>
87
+
88
+ <pre>pastie</pre>
89
+
90
+ <p>or the shortcut version:</p>
91
+
92
+ <pre>pst</pre>
93
+
94
+ <p>Note that this method currently auto-formats all code as Ruby. Support for other languages is possible but not implemented yet. Also, this code is OS X and Windows only because it uses the Clipboard. (A platform-agnostic version without the clipboard aspect is certainly possible.)</p>
95
+
96
+ <h4>Unix-style history buffer</h4>
97
+
98
+ <p>Use "h" or "history" to view your command history, and "h! 123" to re-invoke command #123 in your buffer. "h!" with no args will run the last command.</p>
99
+
100
+ <h4>Write command history to file or vi</h4>
101
+
102
+ <p>To write your history to a file, use "history_write('/path/to/file')". To edit your history in vi, use "history_to_vi" or its shortcut alias "hvi".</p>
103
+
104
+ <h4>Grep classes and methods for strings</h4>
105
+
106
+ <p>Classes:</p>
107
+
108
+ <pre>&gt;&gt; grep_classes(&quot;ixnum&quot;)
109
+ #=&gt; [&quot;Fixnum&quot;]
110
+
111
+ &gt;&gt; grep_classes(&quot;num&quot;)
112
+ #=&gt; [&quot;Bignum&quot;, &quot;Fixnum&quot;, &quot;Numeric&quot;, &quot;REXML::SyncEnumerator&quot;]</pre>
113
+
114
+ <p>Methods:</p>
115
+
116
+ <pre>&gt;&gt; &quot;any arbitrary string&quot;.grep_methods(&quot;pretty&quot;)
117
+ #=&gt; [&quot;pretty_inspect&quot;, &quot;pretty_print_instance_variables&quot;,
118
+ &quot;pretty_print_inspect&quot;, &quot;pretty_print&quot;, &quot;pretty_print_cycle&quot;]
119
+
120
+ &gt;&gt; 32.grep_methods(&quot;*&quot;)
121
+ #=&gt; [&quot;*&quot;, &quot;**&quot;]</pre>
122
+
123
+ <p>At some point it'll probably be a good idea to add regex support for these methods. (Of course if I do that, I'll probably have to change the name from grep to ack.)</p>
124
+
125
+ <h4>Verbosity controls for regular IRB and Rails console</h4>
126
+
127
+ <p>regular IRB: "verbose" or "v" / "quiet" or "q"</p>
128
+
129
+ <p>Rails: "log" method turns verbosity on, there isn't actually any method for turning it off. (Might add one at some point.)</p>
130
+
131
+ <h4>Finder shortcuts for Rails console</h4>
132
+
133
+ <pre>user(:all) / user(1)</pre>
134
+
135
+ <p>instead of</p>
136
+
137
+ <pre>User.find(:all) / User.find(1)</pre>
138
+
139
+ <p>Supports arbitrarily complex finds.</p>
140
+
141
+ <h4>Upload shortcut for Amazon S3</h4>
142
+
143
+ <p>First, define your secret keys in your environment variables:</p>
144
+
145
+ <pre>export AMAZON_ACCESS_KEY_ID='foofoofoofoofoo'
146
+ export AMAZON_SECRET_ACCESS_KEY='barbarbarbarbar'</pre>
147
+
148
+ <p>(Obviously, replace those with actual values.)</p>
149
+
150
+ <p>Then, you can use</p>
151
+
152
+ <pre>aws_upload("bucket_name","filename")</pre>
153
+
154
+ <p>To easily upload public-access files. This method will automatically connect to Amazon S3 for you, if your keys are defined in your environment variables. It also writes the resulting Amazon URL to your clipboard, if you're on OS X, so you can easily share the file with others. Note that you have to create the initial buckets manually - see <a href="http://amazon.rubyforge.org" title="AWS::S3 - Ruby Library for Amazon Simple Storage Service (S3)">amazon.rubyforge.org</a> for details on that.</p>
155
+
156
+ <p>A good way to use this is actually to have a default bucket, and create a further method in your .irbrc file:</p>
157
+
158
+ <pre>def my_bucket_name(filename)
159
+ aws_upload("my_default_bucket",filename)
160
+ end</pre>
161
+
162
+ <h4>Command-line Amazon S3 upload script</h4>
163
+
164
+ <p>Utility Belt also includes <code>amazon</code>, a simple command-line uploader for S3. It requires that you set your Amazon environment variables (see above). The usage is:</p>
165
+
166
+ <pre>amazon -b my_bucket_name filename other_filename</pre>
167
+
168
+ <p>(For any arbitrary number of filenames.)</p>
169
+
170
+ <p>Anytime you want to upload a file to your default bucket on S3, for example, the "images" bucket for your blog - or the "mp3" bucket if you're running a blog like Projectionist - all you have to do is</p>
171
+
172
+ <pre>amazon -b blog_files some_file.name</pre>
173
+
174
+ <p>Or</p>
175
+
176
+ <pre>amazon --bucket blog_files some_file.name</pre>
177
+
178
+ <p>And you're good to go. Of course, if that were too many characters to type, you could always take it one step further with a shell script. For instance, in bash:</p>
179
+
180
+ <pre>alias b1="amazon --bucket my_main_bucket $1"
181
+ alias b2="amazon --bucket my_other_bucket $1"</pre>
182
+
183
+ <p>And then you would only have to type:</p>
184
+
185
+ <pre>b1 file.name</pre>
186
+
187
+ <p>And of course tab completion will handle all the typing of the file name, assuming you choose your file names deliberately ahead of time to make tab completion effective, in which case all you have to type is "b1 f[tab]" and bam.</p>
188
+
189
+ <h4>Command-line Google shortcut (OS X only)</h4>
190
+
191
+ <p>To pop open a new window in your default browser with Google search results for a given search term, do:</p>
192
+
193
+ <pre>google "any given term"</pre>
194
+
195
+ <p>For one-word terms, you can omit the quotes:</p>
196
+
197
+ <pre>google term</pre>
198
+
199
+ <p>If you already have the search term in your clipboard, you can just type:</p>
200
+
201
+ <pre>google</pre>
202
+
203
+ <p>(The last approach is useful for unusual command-line error messages.)</p>
204
+
205
+ <h4>Auto-indentation</h4>
206
+
207
+ <p>Just type as normally. This just enables built-in but underused functionality in IRB. (The IRB default auto-indentation isn't perfect, but it clobbers the living hell out of no indentation at all.)</p>
208
+
209
+ <h4>_ special variable (like Unix shell var !!)</h4>
210
+
211
+ <p>More activation of built-in but underused functionality in IRB. _ functions like !! in Unix, with one subtle difference. Instead of re-issuing the previous command in your history, it gives you the last result of evaluating a command. This means when you start a new IRB session, _ won't go to your history, it'll just give you nil, and it means that some commands can't be repeated exactly. Nine out of ten times, however, it's pretty much the same thing.</p>
212
+
213
+ <p>Note that when combined with the h! in the command history code, you basically have two synonyms for !! here. Neither one is an exact replica, but between the two of them, you should have what you need.</p>
214
+
215
+ <h4>Extremely basic themes for Wirble syntax coloring</h4>
216
+
217
+ <p>Utility Belt incorporates <a href="http://pablotron.org/software/wirble/" title="Pablotron: Wirble">Wirble</a> as a dependency. Wirble provides syntax coloring. Utility Belt allows you to choose between two syntax coloring themes: Wirble's default, and an alternate color set for terminals with light backgrounds. The default theme is enabled by default (duh), but if you have a terminal with a light background, you can enable the light-background theme just by putting the following line of code in your .irbrc:</p>
218
+
219
+ <pre>UtilityBelt::Themes.background(:light)</pre>
220
+
221
+ <p>To revert back to the default, just use this:</p>
222
+
223
+ <pre>UtilityBelt::Themes.background(:dark)</pre>
224
+
225
+ <p>To specify an entirely new syntax color scheme, see the source code for this gem or for Wirble.</p>
226
+
227
+ <h4>Pascal/JavaScript-style "with" statement</h4>
228
+
229
+ <p>I use this with RSpec's spec_helper. Instead of:</p>
230
+
231
+ <pre>Spec::Runner.configure do |config|
232
+ config.use_transactional_fixtures = true
233
+ config.use_instantiated_fixtures = false
234
+ config.fixture_path = RAILS_ROOT + '/spec/fixtures'
235
+ config.mock_with :flexmock
236
+ end</pre>
237
+
238
+ <p>I do this:</p>
239
+
240
+ <pre>Spec::Runner.configure do |config|
241
+ with(config) do
242
+ use_transactional_fixtures = true
243
+ use_instantiated_fixtures = false
244
+ fixture_path = RAILS_ROOT + '/spec/fixtures'
245
+ mock_with :flexmock
246
+ end
247
+ end</pre>
248
+
249
+ <p>Note however that this blew up once and I have no idea why. Caveat emptor, et cetera.</p>
250
+
251
+ <h4>String#to_proc</h4>
252
+
253
+ <p>Syntactic sugar for functional programming. (<a href="http://weblog.raganwald.com/2007/10/stringtoproc.html" title="String#to_proc">More detail here</a>.) Rails' Symbol#to_proc is also included.</p>
254
+
255
+ <pre>(1..3).map(&amp;&apos;*2&apos;) =&gt; [2, 4, 6]
256
+ (1..3).map(&amp;&apos;[-1, _, 0]&apos;) =&gt; [[-1, 1, 0], [-1, 2, 0], [-1, 3, 0]]
257
+ (1..3).map(&amp;&apos;x -&gt; y -&gt; x * y&apos;).map(&amp;&apos;[2]&apos;) =&gt; [2, 4, 6]
258
+ (1..5).select(&amp;&apos;&gt;2&apos;) =&gt; [3, 4, 5]
259
+ (1..3).map(&amp;&apos;x -&gt; y -&gt; x * y&apos;).map(&amp;&apos;.call(2)&apos;) =&gt; [2, 4, 6]
260
+ [5].map(&amp;&quot;(1.._).inject(&amp;&apos;*&apos;)&quot;) =&gt; [120]</pre>
261
+
262
+ <h4>Add and subtract Hashes with + and -</h4>
263
+
264
+ <pre>{:a =&gt; :b} + {:c =&gt; :d} = {:a =&gt; :b, :c =&gt; :d}
265
+ {:a =&gt; :b, :c =&gt; :d} - {:c =&gt; :d} = {:a =&gt; :b}
266
+ {:a =&gt; :b, :c =&gt; :d} - :c = {:a =&gt; :b}</pre>
267
+
268
+ <p>You could already do this with Arrays; now you can do it with Hashes as well. (I was kinda surprised the first time I tried to do this and realized I'd have to write it myself.)</p>
269
+
270
+
271
+ <h4>Grammatically-correct is_an? method - no more "is_a? Array" statements</h4>
272
+
273
+ <pre>&gt;&gt; [].is_an? Array
274
+ #=&gt; true</pre>
275
+
276
+ <p>Most people don't care, but personally, "is_a? Array" drives me completely insane.</p>
277
+
278
+ <h4>Object#not</h4>
279
+
280
+ <p>Replace this:</p>
281
+
282
+ <pre>if !response.incomplete? &amp;&amp; !response.invalid? &amp;&amp; response.total &gt; 0</pre>
283
+
284
+ <p>With this:</p>
285
+
286
+ <pre>if response.not.incomplete? &amp;&amp; response.not.invalid? &amp;&amp; response.total &gt; 0</pre>
287
+
288
+ <p>Syntactic sugar for boolean conditions. (<a href="http://blog.jayfields.com/2007/08/ruby-adding-not-method-for-readability.html" title="Jay Fields Thoughts: Ruby: Adding a "not" method for readability">More detail here</a>.)</p>
289
+
290
+ <h4>One-character exit command</h4>
291
+
292
+ <pre>x</pre>
293
+
294
+ <p>Because life is too short to type whole words.</p>
295
+
296
+
297
+ </div>
298
+
299
+ <div id="footer">
300
+ <p>Copyright &copy; 2007 Giles Bowkett | Design by <a href="http://andreasviklund.com">Andreas Viklund</a>.</p>
301
+ </div>
302
+ </div>
303
+ </body>
304
+ </html>
@@ -0,0 +1,25 @@
1
+ # S3 (http://amazon.rubyforge.org/)
2
+ %w{aws/s3 cgi platform}.each {|lib| require lib}
3
+
4
+ module UtilityBelt
5
+ module AmazonUploadShortcut
6
+ def aws_upload(bucket,filename)
7
+ AWS::S3::Base.establish_connection!(:access_key_id => ENV['AMAZON_ACCESS_KEY_ID'],
8
+ :secret_access_key => ENV['AMAZON_SECRET_ACCESS_KEY'])
9
+ AWS::S3::S3Object.store(filename, open(filename), bucket, :access => :public_read)
10
+ url = "http://s3.amazonaws.com/#{bucket}/#{filename}".gsub(/ /, "%20")
11
+ Clipboard.write(url) if Clipboard.available?
12
+ url
13
+ end
14
+ end
15
+ end
16
+
17
+ class Object
18
+ include UtilityBelt::AmazonUploadShortcut
19
+ end if Object.const_defined? :IRB
20
+
21
+ # a quick note: the "google" command uses CGI.escape, but the URLs produced by CGI.escape
22
+ # don't seem to succeed here, in practice. this may differ by OS and/or browser. Let me
23
+ # know if you see something weird -- the Utility Belt mailing list is here:
24
+ #
25
+ # http://rubyforge.org/mailman/listinfo/utilitybelt-tinkering
@@ -0,0 +1,52 @@
1
+ # original clipboard code: http://project.ioni.st/post/1334#snippet_1334
2
+ # turned it into a class to make it flexxy:
3
+ # http://gilesbowkett.blogspot.com/2007/09/improved-auto-pastie-irb-code.html
4
+ # Extended to handle windows and linux as well
5
+ require 'platform'
6
+
7
+ module UtilityBelt
8
+ class Clipboard
9
+
10
+ def self.available?
11
+ @@implemented || false
12
+ end
13
+
14
+ case Platform::IMPL
15
+ when :macosx
16
+
17
+ def self.read
18
+ IO.popen('pbpaste') {|clipboard| clipboard.read}
19
+ end
20
+
21
+ def self.write(stuff)
22
+ IO.popen('pbcopy', 'w+') {|clipboard| clipboard.write(stuff)}
23
+ end
24
+ @@implemented = true
25
+
26
+ when :mswin
27
+
28
+ begin
29
+ # Try loading the win32-clipboard gem
30
+ require 'win32/clipboard'
31
+
32
+ def self.read
33
+ Win32::Clipboard.data
34
+ end
35
+
36
+ def self.write(stuff)
37
+ Win32::Clipboard.set_data(stuff)
38
+ end
39
+ @@implemented = true
40
+
41
+ rescue LoadError
42
+ raise "You need the win32-clipboard gem for clipboard functionality!"
43
+ end
44
+
45
+ else
46
+ raise "No suitable clipboard implementation for your platform found!"
47
+ end
48
+
49
+ end
50
+ end
51
+
52
+ Clipboard = UtilityBelt::Clipboard if Object.const_defined? :IRB
@@ -0,0 +1,110 @@
1
+ # history
2
+ # http://blog.bleything.net/pages
3
+ # http://gilesbowkett.blogspot.com/2007/06/irbrc-modifications.html
4
+ # Ben's history stuff, slightly modified, plus hvi method
5
+
6
+
7
+
8
+ # Adds shell-style history display and replay to irb. The magic happens in
9
+ # the h, h!, and hw methods.
10
+ #
11
+ # == Authors
12
+ #
13
+ # * Ben Bleything <ben@bleything.net>
14
+ #
15
+ # == Copyright
16
+ #
17
+ # Copyright (c) 2007 Ben Bleything
18
+ #
19
+ # This code released under the terms of the BSD license.
20
+ #
21
+ # == Version
22
+ #
23
+ # $Id: history.rb 50 2007-07-30 18:55:09Z ben $
24
+ #
25
+
26
+ require 'tempfile'
27
+
28
+ class Object
29
+ def history(how_many = 50)
30
+ history_size = Readline::HISTORY.size
31
+ # no lines, get out of here
32
+ puts "No history" and return if history_size == 0
33
+ start_index = 0
34
+ # not enough lines, only show what we have
35
+ if history_size <= how_many
36
+ how_many = history_size - 1
37
+ end_index = how_many
38
+ else
39
+ end_index = history_size - 1 # -1 to adjust for array offset
40
+ start_index = end_index - how_many
41
+ end
42
+ start_index.upto(end_index) {|i| print_line i}
43
+ end
44
+ alias :h :history
45
+
46
+ # -2 because -1 is ourself
47
+ def history_do(lines = (Readline::HISTORY.size - 2))
48
+ irb_eval lines
49
+ end
50
+ alias :h! :history_do
51
+
52
+ def history_write(filename, lines)
53
+ file = File.open(filename, 'w')
54
+ get_lines(lines).each do |l|
55
+ file << "#{l}\n"
56
+ end
57
+ file.close
58
+ end
59
+
60
+ # hack to handle JRuby bug
61
+ def handling_jruby_bug(&block)
62
+ if RUBY_PLATFORM =~ /java/
63
+ puts "JRuby IRB has a bug which prevents successful IRB vi interoperation."
64
+ puts "The JRuby team is aware of this and working on it."
65
+ puts "(http://jira.codehaus.org/browse/JRUBY-2049)"
66
+ else
67
+ yield
68
+ end
69
+ end
70
+
71
+ # TODO: history_write should go to a file, or the clipboard, or a file which opens in an application
72
+ def history_to_vi
73
+ handling_jruby_bug do
74
+ file = Tempfile.new("irb_tempfile")
75
+ get_lines(0..(Readline::HISTORY.size - 1)).each do |line|
76
+ file << "#{line}\n"
77
+ end
78
+ file.close
79
+ system("vim #{file.path}")
80
+ end
81
+ end
82
+ alias :hvi :history_to_vi
83
+
84
+ private
85
+ def get_line(line_number)
86
+ Readline::HISTORY[line_number] rescue ""
87
+ end
88
+
89
+ def get_lines(lines = [])
90
+ return [get_line(lines)] if lines.is_a? Fixnum
91
+ out = []
92
+ lines = lines.to_a if lines.is_a? Range
93
+ lines.each do |l|
94
+ out << Readline::HISTORY[l]
95
+ end
96
+ out
97
+ end
98
+
99
+ def print_line(line_number, show_line_numbers = true)
100
+ print line_number.to_s + ": " if show_line_numbers
101
+ puts get_line(line_number)
102
+ end
103
+
104
+ def irb_eval(lines)
105
+ to_eval = get_lines(lines)
106
+ to_eval.each {|l| Readline::HISTORY << l}
107
+ eval to_eval.join("\n")
108
+ end
109
+ end
110
+
@@ -0,0 +1,34 @@
1
+ require 'tempfile'
2
+
3
+ # This module adds a method, #to_file, which dumps the contents of self into a
4
+ # temp file and then returns the path of that file. This is particularly useful
5
+ # when calling out to shell commands which expect their input in the form of
6
+ # files.
7
+ #
8
+ # Example: use UNIX 'diff' to compare two objects:
9
+ #
10
+ # >> a = ["foo", "bar", "baz"].join("\n")
11
+ # => "foo\nbar\nbaz"
12
+ # >> b = ["foo", "buz", "baz"].join("\n")
13
+ # => "foo\nbuz\nbaz"
14
+ # >> puts `diff #{a.to_file} #{b.to_file}`
15
+ # 2c2
16
+ # < bar
17
+ # ---
18
+ # > buz
19
+ # => nil
20
+ #
21
+ module ConvertableToFile
22
+ def to_file
23
+ path = nil
24
+ Tempfile.open(object_id.to_s) do |tempfile|
25
+ tempfile << self
26
+ path = tempfile.path
27
+ end
28
+ path
29
+ end
30
+ end
31
+
32
+ class Object
33
+ include ConvertableToFile
34
+ end
@@ -0,0 +1,71 @@
1
+ # Allow to select which gadgets to equip
2
+ #
3
+ # Author: Markus Prinz <markus.prinz@qsig.org>
4
+
5
+ module UtilityBelt
6
+ class << self
7
+ def equip(*args)
8
+ Equipper.equip(*args)
9
+ end
10
+ def equipped?
11
+ Equipper.equipped?
12
+ end
13
+ end
14
+ module Equipper
15
+ GADGETS = Dir[File.join(File.dirname(__FILE__), '*.rb')].map{|file| File.basename(file)[0..-4]}.reject{|gadget| "equipper" == gadget }
16
+
17
+ DEFAULTS = %w{wirble
18
+ hash_math
19
+ interactive_editor
20
+ irb_options
21
+ irb_verbosity_control}
22
+
23
+ @equipped = false
24
+
25
+ class << self
26
+ def equip(*args)
27
+ return if args.empty?
28
+
29
+ gadgets_to_equip = []
30
+
31
+ # Special case using :all or :none
32
+ if args[0].is_a?(Symbol) && [:all, :none, :defaults].include?(args[0])
33
+ what = args[0]
34
+
35
+ unless args[1].nil?
36
+ exceptions = args[1].has_key?(:except) ? args[1][:except] : []
37
+
38
+ # Handle special case where we get a string or a symbol instead of an array
39
+ exceptions = exceptions.to_s.to_a unless exceptions.is_a?( Array )
40
+ else
41
+ exceptions = []
42
+ end
43
+
44
+ case what
45
+ when :all
46
+ gadgets_to_equip.push(*(GADGETS - exceptions))
47
+ when :none
48
+ gadgets_to_equip.push(*exceptions)
49
+ when :defaults
50
+ gadgets_to_equip.push(*DEFAULTS)
51
+ end
52
+ # otherwise, args is a list of gadgets to equip
53
+ else
54
+ args.each do |arg|
55
+ gadget = arg.to_s
56
+
57
+ # Silently ignore unkown gadgets
58
+ gadgets_to_equip << gadget if GADGETS.include? gadget
59
+ end
60
+ end
61
+
62
+ gadgets_to_equip.each{|gadget| require "utility_belt/#{gadget}" }
63
+
64
+ @equipped ||= true
65
+ end
66
+ def equipped?
67
+ @equipped
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+ %w{rubygems platform cgi}.each {|library| require library}
3
+
4
+ UtilityBelt.equip(:clipboard)
5
+
6
+ module UtilityBelt
7
+ module Google
8
+ def google(search_term = nil)
9
+ search_term ||= Clipboard.read if Clipboard.available?
10
+ if search_term.empty?
11
+ puts "Usage: google search_term_without_spaces (Unix command line only)"
12
+ puts " google 'search term with spaces' (Unix or IRB)"
13
+ puts " google (Unix or IRB)"
14
+ puts " (if invoking without args, must have text in clipboard)"
15
+ else
16
+ url = "http://google.com/search?q=#{CGI.escape(search_term)}"
17
+ case Platform::IMPL
18
+ when :macosx
19
+ Kernel.system("open #{url}")
20
+ when :windows
21
+ Kernel.system("start #{url}")
22
+ #when :linux
23
+ else
24
+ puts "Sorry, don't know how to open an URL from the command line on your platform"
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ class Object
32
+ include UtilityBelt::Google
33
+ end if Object.const_defined? :IRB
@@ -0,0 +1,13 @@
1
+ class Hash
2
+ alias :+ :merge
3
+ def -(thing_to_be_deleted)
4
+ if thing_to_be_deleted.is_a? Hash
5
+ thing_to_be_deleted.each do |key, value|
6
+ self.delete(key) if self[key] == value
7
+ end
8
+ elsif self.keys.include? thing_to_be_deleted
9
+ self.delete(thing_to_be_deleted)
10
+ end
11
+ self
12
+ end
13
+ end