PortValhalla/lib/modules/partition/select.fish

47 lines
1 KiB
Fish
Raw Permalink Normal View History

2024-09-30 12:36:20 +00:00
function select -a header outFile message error choices loop
if [ -z "$loop" ]
2024-10-06 19:25:34 +00:00
set loop true
2024-09-30 12:36:20 +00:00
end
while true
set -l items
2024-10-06 19:25:34 +00:00
2024-09-30 12:36:20 +00:00
echo "$choices" | while read choice
set -a items "$choice"
end
set -l count (count $items)
set -l padding (math (string length "$count") + 1)
if [ "$count" -gt 0 ]
echo "$message"
printf "%"$padding"s %s\n" "" "$header"
for i in (seq "$count")
printf "%"$padding"s %s\n" "$i" "$items[$i]"
end
read -lP "Your choice: " choice
or exit 1
2024-10-06 19:25:34 +00:00
if math "0+$choice" &>/dev/null
2024-09-30 12:36:20 +00:00
if [ "$choice" -ge 1 ] && [ "$choice" -le "$count" ]
2024-10-06 19:25:34 +00:00
echo "$items[$choice]" >$outFile
2024-09-30 12:36:20 +00:00
return
end
end
echo "The specified \"$choice\" is invalid!"
else
echo "$error" >&2
exit
end
if not "$loop"
break
end
end
return 1
end