Refactor the confirmation dialogue

This commit is contained in:
Manuel Thalmann 2024-09-22 18:51:10 +02:00
parent 9060a6f304
commit 9540680341
3 changed files with 30 additions and 18 deletions

View file

@ -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

View file

@ -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
''

View file

@ -1,11 +0,0 @@
#!/bin/bash
function isTruthy() {
local value;
value="$1";
[ ! -z "$value" ] &&
[ "$value" != "0" ] &&
[ "$value" != "false" ] &&
[ "$value" != "n" ] &&
[ "$value" != "no" ];
}