Create a dedicated script for selects
This commit is contained in:
parent
3826985e22
commit
739aa90e88
3 changed files with 75 additions and 47 deletions
|
@ -1,52 +1,34 @@
|
||||||
#!/bin/env fish
|
#!/bin/env fish
|
||||||
function chooseDisk -a outFile message
|
function chooseDisk -a outFile message selectScript
|
||||||
|
if [ -z "$selectScript" ]
|
||||||
|
set selectScript "$(status dirname)/select.fish"
|
||||||
|
end
|
||||||
|
|
||||||
|
source "$selectScript"
|
||||||
|
|
||||||
while true
|
while true
|
||||||
|
set -l header
|
||||||
set -l disks
|
set -l disks
|
||||||
|
|
||||||
lsblk -do NAME,SIZE,TYPE | while read disk
|
lsblk -do NAME,SIZE,TYPE | begin
|
||||||
|
read header
|
||||||
|
|
||||||
|
cat | while read disk
|
||||||
set -l diskInfo (string split -n " " $disk)
|
set -l diskInfo (string split -n " " $disk)
|
||||||
|
|
||||||
if contains "$diskInfo[3]" "TYPE" "disk"
|
if contains "$diskInfo[3]" "disk"
|
||||||
set -a disks "$diskInfo"
|
set -a disks "$disk"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
set -l diskCount (count $disks)
|
select "$header" "$outFile" "$message" "No valid disk found!" "$(string collect $disks)" "false"
|
||||||
set -l padding (math (string length "$diskCount") + 1)
|
and begin
|
||||||
|
set -l disk (string split -n " " (cat "$outFile"))
|
||||||
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
|
echo "/dev/$disk[1]" > $outFile
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
echo "The specified choice \"$choice\" is invalid!"
|
|
||||||
else
|
|
||||||
echo "No valid disk found!" >&2
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return 1
|
|
||||||
end
|
end
|
||||||
|
|
||||||
chooseDisk $argv
|
chooseDisk $argv
|
||||||
|
|
|
@ -79,7 +79,7 @@
|
||||||
|
|
||||||
diskSelector = ''
|
diskSelector = ''
|
||||||
result="$(mktemp)"
|
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")"
|
${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