lib/planter/array.rb in planter-cli-3.0.4 vs lib/planter/array.rb in planter-cli-3.0.5
- old
+ new
@@ -7,14 +7,21 @@
## Convert an array of "(c)hoices" to abbrevation. If a default character is
## provided it will be highlighted. Output is a color template, unprocessed.
##
## @example ["(c)hoice", "(o)ther"].abbr_choices #=> "[c/o]"
##
- ## @param default [String] The color templated output string
+ ## @param default [String] The (unprocessed) color templated output string
##
def abbr_choices(default: nil)
+ return default.nil? ? '' : "{xdw}[{xbc}#{default}{dw}]{x}" if all? { |c| c.to_i.positive? }
+
chars = join(' ').scan(/\((?:(.)\.?)\)/).map { |c| c[0] }
+
+ return default.nil? ? '' : "{xdw}[{xbc}#{default}{dw}]{x}" if chars.all? { |c| c.to_i.positive? }
+
+ die('Array contains duplicates', :input) if chars.duplicates?
+
out = String.new
out << '{xdw}['
out << chars.map do |c|
if default && c.downcase == default.downcase
"{xbc}#{c}"
@@ -30,17 +37,24 @@
## @param numeric [Boolean] Include numbering
##
## @return [Array] Array of choices
##
def to_options(numeric)
+ die('Array contains duplicates', :input) if duplicates?
+
map.with_index do |c, i|
# v = c.to_s.match(/\(?([a-z]|\d+\.?)\)?/)[1].strip
if numeric
"(#{i + 1}). #{c.to_s.sub(/^\(?\d+\.?\)? +/, '')}"
else
c
end
end
+ end
+
+ ## test if array has duplicates
+ def duplicates?
+ uniq.size != size
end
## Find the index of a choice in an array of choices
##
## @param choice [String] The choice to find