Create a dedicated script for selects
This commit is contained in:
parent
3826985e22
commit
739aa90e88
|
@ -1,52 +1,34 @@
|
|||
#!/bin/env fish
|
||||
function chooseDisk -a outFile message
|
||||
|
||||
while true
|
||||
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 ]
|
||||
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
|
||||
or exit 1
|
||||
|
||||
if math "0+$choice" &> /dev/null
|
||||
if [ "$choice" -ge 1 ] && [ "$choice" -le "$diskCount" ]
|
||||
set -l disk (string split -n " " "$disks[$(math "$choice" + 1)]")
|
||||
echo "/dev/$disk[1]" > $outFile
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
echo "The specified choice \"$choice\" is invalid!"
|
||||
else
|
||||
echo "No valid disk found!" >&2
|
||||
end
|
||||
function chooseDisk -a outFile message selectScript
|
||||
if [ -z "$selectScript" ]
|
||||
set selectScript "$(status dirname)/select.fish"
|
||||
end
|
||||
|
||||
return 1
|
||||
source "$selectScript"
|
||||
|
||||
while true
|
||||
set -l header
|
||||
set -l disks
|
||||
|
||||
lsblk -do NAME,SIZE,TYPE | begin
|
||||
read header
|
||||
|
||||
cat | while read disk
|
||||
set -l diskInfo (string split -n " " $disk)
|
||||
|
||||
if contains "$diskInfo[3]" "disk"
|
||||
set -a disks "$disk"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
select "$header" "$outFile" "$message" "No valid disk found!" "$(string collect $disks)" "false"
|
||||
and begin
|
||||
set -l disk (string split -n " " (cat "$outFile"))
|
||||
echo "/dev/$disk[1]" > $outFile
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
chooseDisk $argv
|
||||
|
|
|
@ -79,7 +79,7 @@
|
|||
|
||||
diskSelector = ''
|
||||
result="$(mktemp)"
|
||||
fish ${./choose-disk.fish} "$result" "Which disk do you wish to install the OS on?"
|
||||
fish ${./choose-disk.fish} "$result" "Which disk do you wish to install the OS on?" ${./select.fish}
|
||||
${diskVarName}="$(cat "$result")"
|
||||
'';
|
||||
|
||||
|
|
46
lib/modules/partition/select.fish
Normal file
46
lib/modules/partition/select.fish
Normal file
|
@ -0,0 +1,46 @@
|
|||
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
|
Loading…
Reference in a new issue