commander 4.1.0 → 4.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ === 4.1.1 / 2012-02-16
2
+
3
+ * Update `ask_editor` to work with any unix editor - emacs, vim, etc. (thanks to Federico Galassi)
4
+
1
5
  === 4.1.0 / 2012-02-12
2
6
 
3
7
  * Update highline dependency.
@@ -134,11 +134,14 @@ following methods to simplify common tasks:
134
134
  # Enable paging of output after this point
135
135
  enable_paging
136
136
 
137
- # Ask editor for input (EDITOR or TextMate by default)
137
+ # Ask editor for input (EDITOR environment variable or whichever is available: TextMate, vim, vi, emacs, nano, pico)
138
138
  ask_editor
139
139
 
140
140
  # Ask editor, supplying initial text
141
141
  ask_editor 'previous data to update'
142
+
143
+ # Ask editor, preferring a specific editor
144
+ ask_editor 'previous data', 'vim'
142
145
 
143
146
  # Choose from an array of elements
144
147
  choice = choose("Favorite language?", :ruby, :perl, :js)
@@ -344,10 +347,6 @@ OR
344
347
  Feel free to fork and request a pull, or submit a ticket
345
348
  http://github.com/visionmedia/commander/issues
346
349
 
347
- == Known Issues
348
-
349
- * ask_editor has been tested with TextMate only
350
-
351
350
  == License
352
351
 
353
352
  (The MIT License)
@@ -1,3 +1,5 @@
1
+ require 'tempfile'
2
+ require 'shellwords'
1
3
 
2
4
  module Commander
3
5
 
@@ -40,7 +42,7 @@ module Commander
40
42
 
41
43
  def choose message, *choices
42
44
  say message
43
- super *choices
45
+ super(*choices)
44
46
  end
45
47
 
46
48
  ##
@@ -224,26 +226,40 @@ module Commander
224
226
  def reset_io
225
227
  $stdin, $stdout = STDIN, STDOUT
226
228
  end
229
+
230
+ ##
231
+ # Find an editor available in path. Optionally supply the _preferred_
232
+ # editor. Returns the name as a string, nil if none is available.
233
+
234
+ def available_editor preferred = nil
235
+ [preferred, ENV['EDITOR'], 'mate -w', 'vim', 'vi', 'emacs', 'nano', 'pico'].
236
+ compact.
237
+ find {|name| system("/usr/bin/which #{name.split.first} > /dev/null") }
238
+ end
227
239
 
228
240
  ##
229
- # Prompt _editor_ for input. Optionally supply initial
241
+ # Prompt an editor for input. Optionally supply initial
230
242
  # _input_ which is written to the editor.
231
243
  #
232
- # The _editor_ defaults to the EDITOR environment variable
233
- # when present, or 'mate' for TextMate.
244
+ # _preferred_editor_ can be hinted.
234
245
  #
235
246
  # === Examples
236
247
  #
237
248
  # ask_editor # => prompts EDITOR with no input
238
249
  # ask_editor('foo') # => prompts EDITOR with default text of 'foo'
239
- # ask_editor('foo', :mate) # => prompts TextMate with default text of 'foo'
250
+ # ask_editor('foo', 'mate -w') # => prompts TextMate with default text of 'foo'
240
251
  #
241
252
 
242
- def ask_editor input = nil, editor = ENV['EDITOR'] || 'mate'
243
- IO.popen(editor.to_s, 'w+') do |pipe|
244
- pipe.puts input.to_s unless input.nil?
245
- pipe.close_write
246
- pipe.read
253
+ def ask_editor input = nil, preferred_editor = nil
254
+ editor = available_editor preferred_editor
255
+ program = Commander::Runner.instance.program(:name).downcase rescue 'commander'
256
+ tmpfile = Tempfile.new program
257
+ begin
258
+ tmpfile.write input if input
259
+ tmpfile.close
260
+ system("#{editor} #{tmpfile.path.shellescape}") ? IO.read(tmpfile.path) : nil
261
+ ensure
262
+ tmpfile.unlink
247
263
  end
248
264
  end
249
265
 
@@ -1,3 +1,3 @@
1
1
  module Commander
2
- VERSION = '4.1.0'
2
+ VERSION = '4.1.1'
3
3
  end
@@ -20,5 +20,11 @@ describe Commander::UI do
20
20
  exception.should_not be_true
21
21
  end
22
22
  end
23
+
24
+ describe ".available_editor" do
25
+ it "should not fail on available editors with shell arguments" do
26
+ Commander::UI.available_editor('sh -c').should eq('sh -c')
27
+ end
28
+ end
23
29
 
24
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commander
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.0
4
+ version: 4.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-02-13 00:00:00.000000000Z
13
+ date: 2012-02-16 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: highline
17
- requirement: &70213543949460 !ruby/object:Gem::Requirement
17
+ requirement: &2157651520 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 1.6.11
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70213543949460
25
+ version_requirements: *2157651520
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rspec
28
- requirement: &70213543948960 !ruby/object:Gem::Requirement
28
+ requirement: &2157681480 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '2'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70213543948960
36
+ version_requirements: *2157681480
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: rake
39
- requirement: &70213543948580 !ruby/object:Gem::Requirement
39
+ requirement: &2157681100 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *70213543948580
47
+ version_requirements: *2157681100
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: simplecov
50
- requirement: &70213543948120 !ruby/object:Gem::Requirement
50
+ requirement: &2157680640 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,7 +55,7 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *70213543948120
58
+ version_requirements: *2157680640
59
59
  description: The complete solution for Ruby command-line executables. Commander bridges
60
60
  the gap between other terminal related libraries you know and love (OptionParser,
61
61
  HighLine), while providing many new features, and an elegant API.
@@ -123,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
123
  version: '0'
124
124
  requirements: []
125
125
  rubyforge_project: commander
126
- rubygems_version: 1.8.12
126
+ rubygems_version: 1.8.15
127
127
  signing_key:
128
128
  specification_version: 3
129
129
  summary: The complete solution for Ruby command-line executables