PortValhalla/lib/modules/partition/select.fish
2024-10-06 21:25:34 +02:00

46 lines
1 KiB
Fish

function select -a header outFile message error choices loop
if [ -z "$loop" ]
set loop true
end
while true
set -l items
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
if math "0+$choice" &>/dev/null
if [ "$choice" -ge 1 ] && [ "$choice" -le "$count" ]
echo "$items[$choice]" >$outFile
return
end
end
echo "The specified \"$choice\" is invalid!"
else
echo "$error" >&2
exit
end
if not "$loop"
break
end
end
return 1
end