From 9060a6f3041dc9648516bab2904eacf1cbcb4c31 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sun, 22 Sep 2024 18:24:07 +0200 Subject: [PATCH] Implement disk dialogue in a single script --- lib/modules/partition/choose-disk.fish | 48 +++++++++++++++++++++ lib/modules/partition/choose-disk.sh | 59 -------------------------- lib/modules/partition/disks.nix | 5 ++- winiso/lib/choose-disk.fish | 44 ++----------------- 4 files changed, 54 insertions(+), 102 deletions(-) create mode 100644 lib/modules/partition/choose-disk.fish delete mode 100755 lib/modules/partition/choose-disk.sh diff --git a/lib/modules/partition/choose-disk.fish b/lib/modules/partition/choose-disk.fish new file mode 100644 index 00000000..ad8cc38b --- /dev/null +++ b/lib/modules/partition/choose-disk.fish @@ -0,0 +1,48 @@ +#!/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 diff --git a/lib/modules/partition/choose-disk.sh b/lib/modules/partition/choose-disk.sh deleted file mode 100755 index c1e8a8d4..00000000 --- a/lib/modules/partition/choose-disk.sh +++ /dev/null @@ -1,59 +0,0 @@ -#!/bin/bash -function chooseDisk() { - local -n result="$1"; - local message="$2"; - local choice; - local disk; - local -a disks; - local i; - disks=(); - - while read disk - do - local -a diskInfo; - diskInfo=($disk); - - if [ "${diskInfo[2]}" == "TYPE" ] || [ "${diskInfo[2]}" == "disk" ] - then - disks+=("$disk"); - fi; - done < <(lsblk -do NAME,SIZE,TYPE); - - diskCount="$(expr "${#disks[@]}" - 1)"; - padding="${#diskCount}"; - - if [ "$diskCount" -gt 0 ] - then - while true - do - echo "$message"; - - for i in $(seq 0 "$(expr "$diskCount")") - do - local index; - if [ "$i" -eq 0 ] - then - index=""; - else - index="$i:"; - fi; - - printf "%$(expr "${diskCount}" + 1)s ${disks[$i]}" "$index"; - echo ""; - done; - - read -p "Your choice: " choice; - - if [ "$choice" -ge 1 ] && [ "$choice" -le "$diskCount" ] - then - disk=(${disks[$choice]}); - result="/dev/${disk[0]}"; - return; - else - >&2 echo "The specified choice \"$choice\" is invalid!"; - fi; - done; - else - >&2 echo "No valid disk found!"; - fi; -} diff --git a/lib/modules/partition/disks.nix b/lib/modules/partition/disks.nix index e7ddb4f2..abd57b53 100644 --- a/lib/modules/partition/disks.nix +++ b/lib/modules/partition/disks.nix @@ -78,8 +78,9 @@ diskVar = ''''${${diskVarName}}''; diskSelector = '' - . ${./choose-disk.sh}; - chooseDisk ${diskVarName} "Which disk do you wish to install the OS on?"; + result="$(mktemp)" + fish ${./choose-disk.fish} "$result" "Which disk do you wish to install the OS on?" + ${diskVarName}="$(cat "$result")" ''; partitions = lib.lists.sortOn diff --git a/winiso/lib/choose-disk.fish b/winiso/lib/choose-disk.fish index 5082db04..eec18e62 100755 --- a/winiso/lib/choose-disk.fish +++ b/winiso/lib/choose-disk.fish @@ -1,44 +1,6 @@ #!/bin/env fish function chooseDisk -a result 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)]") - set -g "$result" "/dev/$disk[1]" - return - else - echo "The specified choice \"$choice\" is invalid!" >&2 - end - end - else - echo "No valid disk found!" >&2 - end + set -l file (mktemp) + fish "$(status dirname)/../../lib/modules/partition/choose-disk.fish" "$file" "$message" + set -g "$result" (cat "$file") end