diff --git a/lib/modules/partition/choose-disk.fish b/lib/modules/partition/choose-disk.fish index 836daedf..17d9d6ac 100644 --- a/lib/modules/partition/choose-disk.fish +++ b/lib/modules/partition/choose-disk.fish @@ -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 diff --git a/lib/modules/partition/disks.nix b/lib/modules/partition/disks.nix index c2e34ec2..e5ea6993 100644 --- a/lib/modules/partition/disks.nix +++ b/lib/modules/partition/disks.nix @@ -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")" ''; diff --git a/lib/modules/partition/select.fish b/lib/modules/partition/select.fish new file mode 100644 index 00000000..2c0f39e4 --- /dev/null +++ b/lib/modules/partition/select.fish @@ -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