interact 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -245,15 +245,22 @@ module Interactive
245
245
  [true, options[:default]]
246
246
  end
247
247
  elsif choices = options[:choices]
248
- matches = choices.select { |x| x.start_with? ans }
248
+ matches = choices.select { |x|
249
+ choice_completion(x, options).start_with? ans
250
+ }
249
251
 
250
252
  if matches.size == 1
251
253
  [true, matches.first]
252
254
  elsif choices and ans =~ /^\s*\d+\s*$/ and \
253
- res = choices.to_a[ans.to_i - 1]
255
+ ans.to_i - 1 >= 0 and res = choices.to_a[ans.to_i - 1]
254
256
  [true, res]
255
257
  elsif matches.size > 1
256
- puts "Please disambiguate: #{matches.join " or "}?"
258
+ matches_list = matches.collect { |m|
259
+ show_choice(m, options)
260
+ }.join " or "
261
+
262
+ puts "Please disambiguate: #{matches_list}?"
263
+
257
264
  [false, nil]
258
265
  else
259
266
  puts "Unknown answer, please try again!"
@@ -268,8 +275,30 @@ module Interactive
268
275
  return unless options[:indexed]
269
276
 
270
277
  choices.each_with_index do |o, i|
271
- puts "#{i + 1}: #{o}"
278
+ puts "#{i + 1}: #{show_choice(o, options)}"
279
+ end
280
+ end
281
+
282
+ def show_choice(choice, options = {})
283
+ display = options[:display] || proc(&:to_s)
284
+ display.call(choice)
285
+ end
286
+
287
+ def choice_completion(choice, options = {})
288
+ complete = options[:complete] || options[:display] || proc(&:to_s)
289
+ complete.call(choice)
290
+ end
291
+
292
+ def common_prefix(*strs)
293
+ longest = strs.sort_by(&:size).last
294
+ longest.size.times do |i|
295
+ sub = longest[0..(-1 - i)]
296
+ if strs.all? { |s| s.start_with?(sub) }
297
+ return sub
298
+ end
272
299
  end
300
+
301
+ ""
273
302
  end
274
303
 
275
304
  def handler(which, state)
@@ -286,7 +315,11 @@ module Interactive
286
315
  when :tab
287
316
  matches =
288
317
  if choices = state.options[:choices]
289
- choices.select { |c| c.start_with? ans }
318
+ choices.collect { |c|
319
+ choice_completion(c, state.options)
320
+ }.select { |c|
321
+ c.start_with? ans
322
+ }
290
323
  else
291
324
  matching_paths(ans)
292
325
  end
@@ -295,6 +328,8 @@ module Interactive
295
328
  ans = state.answer = matches[0].dup
296
329
  state.display(ans[pos .. -1])
297
330
  else
331
+ ans = state.answer = common_prefix(*matches)
332
+ state.display(ans[pos .. -1])
298
333
  print("\a") # bell
299
334
  end
300
335
 
@@ -1,5 +1,5 @@
1
1
  # Copyright (c) 2011 Alex Suraci
2
2
 
3
3
  module Interact
4
- VERSION = "0.4.3"
4
+ VERSION = "0.4.4"
5
5
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: interact
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 3
10
- version: 0.4.3
9
+ - 4
10
+ version: 0.4.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Alex Suraci
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-05-25 00:00:00 Z
18
+ date: 2012-07-14 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rake