lib/cliprompt/optionset.rb in cliprompt-0.0.4 vs lib/cliprompt/optionset.rb in cliprompt-0.0.5
- old
+ new
@@ -61,32 +61,68 @@
def parse_float(arg)
@default = arg.to_s
end
def parse_string(arg)
- if arg.match /^[yY1](es)?(\/)?[nN0](o)?/
+ if /^[yY1](es)?(\/)?[nN0](o)?/.match(arg)
@boolean = true
- if /y(es)?(\/)?N/.match arg
+ if /y(es)?(\/)?N/.match(arg)
@default = false
else
@default = true
end
else
@default = arg
end
end
def display
- back = ''
if @boolean
- back = @default ? "[Y/n]" : "[y/N]"
+ display_boolean
+ elsif @choices.count > 0
+ if @aslist
+ display_list
+ else
+ display_choices
+ end
else
- if @choices.count > 0
- back += "(#{@choices.join(' / ')})"
+ display_default
+ end
+ end
+
+ def display_boolean
+ @default ? "[Y/n] " : "[y/N] "
+ end
+
+ def display_list
+ back = "\n"
+ choices.each_with_index do |choice, i|
+ if @default == choice
+ back << sprintf("> %-3s %s\n", i, choice)
+ else
+ back << sprintf(" %-3s %s\n", i, choice)
end
- back += "[#{@default}]" if @default
end
+ back << "#{Cliprompt::MSG_CHOSE_A_NUMBER} "
+ back << display_default_index.to_s
return back
+ end
+
+ def display_choices
+ back = ''
+ if @choices.count > 0
+ back << "(#{@choices.join(' / ')}) "
+ end
+ back << display_default.to_s
+ return back
+ end
+
+ def display_default
+ "[#{@default}] " if @default
+ end
+
+ def display_default_index
+ "[#{@choices.index(@default)}] " if @default
end
def validate(question, answer)
if answer == ''
check_default question