From 954068034113b1776ba788d6372d726b46ac3409 Mon Sep 17 00:00:00 2001
From: Manuel Thalmann <m@nuth.ch>
Date: Sun, 22 Sep 2024 18:51:10 +0200
Subject: [PATCH] Refactor the confirmation dialogue

---
 lib/modules/partition/confirm.fish | 28 ++++++++++++++++++++++++++++
 lib/modules/partition/disks.nix    |  9 ++-------
 lib/modules/partition/is-truthy.sh | 11 -----------
 3 files changed, 30 insertions(+), 18 deletions(-)
 create mode 100644 lib/modules/partition/confirm.fish
 delete mode 100755 lib/modules/partition/is-truthy.sh

diff --git a/lib/modules/partition/confirm.fish b/lib/modules/partition/confirm.fish
new file mode 100644
index 00000000..6598038f
--- /dev/null
+++ b/lib/modules/partition/confirm.fish
@@ -0,0 +1,28 @@
+#!/bin/env fish
+function confirm -a message default
+    set -l options (echo "[y/n]" | string replace $default (string upper $default))
+
+    while true
+        read -l value -P "$message $options "
+        set value (string lower "$value")
+
+        if [ -z "$value" ]
+            set value $default
+        end
+
+        if contains "$value" "0" "false" "n" "no"
+            false
+            return
+        end
+
+        if contains "$value" "1" "true" "y" "yes"
+            true
+            return
+        end
+
+        echo "The specified value `$value` is invalid!"
+        echo "Please try again"
+    end
+end
+
+confirm $argv
diff --git a/lib/modules/partition/disks.nix b/lib/modules/partition/disks.nix
index abd57b53..c2e34ec2 100644
--- a/lib/modules/partition/disks.nix
+++ b/lib/modules/partition/disks.nix
@@ -177,13 +177,10 @@
             else ''
               ${diskVarName}=${config.devicePath}
               ${if osDisk then ''
-                . ${./is-truthy.sh}
                 if [ ! -b ${diskVar} ]; then
                   function fallback() {
                     echo "Couldn't find the specified disk \"${diskVar}\"."
-                    local answer
-                    read -p "Do you want to install the OS on another disk? [y/n] " answer
-                    if isTruthy "$answer"; then
+                    if fish ${./confirm.fish} "Do you want to install the OS on another disk?"; then
                       ${diskSelector}
                     else
                       exit 1
@@ -358,14 +355,12 @@
               ] ++
               (builtins.map (_: _.deviceScript) disks) ++
               lib.optionals ((builtins.length disks) > 0) [
-                ". ${./is-truthy.sh}"
                 ''echo "$(tput setaf 3)==== WARNING ====$(tput sgr0)"''
                 (''echo "Continuing this script will alter the partitions of '' + (
                   lib.strings.concatStringsSep ", " (builtins.map (_: "${_.deviceVariable}") (lib.lists.init disks))
                 ) + (if (builtins.length disks) > 1 then " and " else "") + (lib.lists.last disks).deviceVariable + ''"'')
-                ''read -p "Are you sure you want to continue? [y/n] " answer''
                 ''
-                  if ! isTruthy "$answer"; then
+                  if ! fish ${./confirm.fish} "Are you sure you want to continue?" "n"; then
                     exit 1
                   fi
                 ''
diff --git a/lib/modules/partition/is-truthy.sh b/lib/modules/partition/is-truthy.sh
deleted file mode 100755
index beb97b08..00000000
--- a/lib/modules/partition/is-truthy.sh
+++ /dev/null
@@ -1,11 +0,0 @@
-#!/bin/bash
-function isTruthy() {
-    local value;
-    value="$1";
-
-    [ ! -z "$value" ] &&
-    [ "$value" != "0" ] &&
-    [ "$value" != "false" ] &&
-    [ "$value" != "n" ] &&
-    [ "$value" != "no" ];
-}