PortValhalla/lib/modules/partition/choose-disk.fish

49 lines
1.2 KiB
Fish
Raw Normal View History

#!/bin/env fish
function chooseDisk -a outFile message
set -l disks
lsblk -do NAME,SIZE,TYPE | while read disk
set -l diskInfo (string split -n " " $disk)
if contains "$diskInfo[3]" "TYPE" "disk"
set -a disks "$diskInfo"
end
end
set -l diskCount (count $disks)
set -l padding (math (string length "$diskCount") + 1)
if [ "$diskCount" -gt 0 ]
while true
echo "$message"
set -l prefix
for i in (seq "$diskCount")
if [ "$i" -eq 1 ]
set prefix ""
else
set prefix "$(math "$i" - 1):"
end
printf "%"$padding"s %s\n" "$prefix" "$disks[$i]"
end
read -lP "Your choice: " choice
if [ "$choice" -ge 1 ] && [ "$choice" -le "$diskCount" ]
set -l disk (string split -n " " "$disks[$(math "$choice" + 1)]")
echo "/dev/$disk[1]" > $outFile
return
else
echo "The specified choice \"$choice\" is invalid!"
end
end
else
echo "No valid disk found!" >&2
end
return 1
end
chooseDisk $argv