Skip to content

Commit

Permalink
Add support for all choices with field
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Hsu committed Feb 8, 2012
1 parent f8fa59a commit 608c9e8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
14 changes: 9 additions & 5 deletions lib/hirb/menu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Error < StandardError; end
CHOSEN_REGEXP = /^(\d([^:]+)?)(?::)?(\S+)?/
CHOSEN_ARG = '%s'
ALL_ARG = '*'
ALL_REGEXP = /^(\*)(?::)?(\S+)?/
DIRECTIONS = "Specify individual choices (4,7), range of choices (1-3) or all choices (*)."


Expand Down Expand Up @@ -141,9 +142,12 @@ def return_cell_values?
end

def map_all_args(tokens)
tokens.map { |t| t == ALL_ARG ? @output : t }
tokens.map { |arr,f|
arr == ALL_ARG ? @output : yield(arr, f)
if arr == ALL_ARG
f.nil? ? @output : yield(@output, f)
else
yield(arr, f)
end
}.flatten
end

Expand All @@ -167,10 +171,10 @@ def parse_word(word)
@new_args << CHOSEN_ARG
field = $3 ? unalias_field($3) : default_field ||
raise(Error, "No default field/column found. Fields must be explicitly picked.")
[Util.choose_from_array(@output, word), field ]
elsif word.index(ALL_ARG)
[Util.choose_from_array(@output, word), field]
elsif word[ALL_REGEXP]
@new_args << CHOSEN_ARG
ALL_ARG
$2 ? [ALL_ARG, unalias_field($2)] : ALL_ARG
else
@new_args << word
nil
Expand Down
2 changes: 1 addition & 1 deletion lib/hirb/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Hirb
VERSION = '0.6.0'
VERSION = '0.6.2'
end
9 changes: 7 additions & 2 deletions test/menu_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def menu_input(input='')
it "with block renders" do
menu_input "1,2"
expected_result = [1,2]
capture_stdout {
capture_stdout {
menu([1,2,3]) {|e| e.should == expected_result }.should == expected_result
}
end
Expand Down Expand Up @@ -202,6 +202,11 @@ def two_d_menu(options={})
two_d_menu(:action=>true, :invoke=>[[{:a => 1, :bro => 2}, {:a => 3, :bro => 4}, {:a => 1, :bro => 2}, {:a => 3, :bro => 4}, 4]])
end

it "with all choices with field" do
menu_input "p *:bro"
two_d_menu(:action=>true, :invoke=>[[2, 4]])
end

it "with no command given prints error" do
menu_input "1"
capture_stderr { two_d_menu(:action=>true) }.should =~ /No command given/
Expand Down Expand Up @@ -252,4 +257,4 @@ def two_d_menu(options={})
}.should =~ /Default.*required/
end
end
end
end

0 comments on commit 608c9e8

Please sign in to comment.