Subject: [bug#58345] [PATCH 1/3] guix-install.sh: Improve prompt_yes_no procedure.

From: Maxim Cournoyer <[email-masked]>
Date: Fri, 7 Oct 2022 01:31:58 -0400

* etc/guix-install.sh (_flush): New function.
(prompt_yes_no): Clear input, then only read the first character, silently.
Add the [Yes/no] string to the message.  When a newline is entered by the
user, treat it as the default value, which is "yes".
(chk_gpg_keyring): Remove "(yes/no)" from the prompt message.
(configure_substitute_discovery): Likewise.
(sys_authorize_build_farms): Likewise.
---
 etc/guix-install.sh | 28 +++++++++++++++++++---------
 1 file changed, 19 insertions(+), 9 deletions(-)

diff --git a/etc/guix-install.sh b/etc/guix-install.sh
index 300681e111..8c05d19657 100755
--- a/etc/guix-install.sh
+++ b/etc/guix-install.sh
@@ -9,7 +9,7 @@
 # Copyright © 2020 Daniel Brooks <[email-masked]>
 # Copyright © 2021 Jakub Kądziołka <[email-masked]>
 # Copyright © 2021 Chris Marusich <[email-masked]>
-# Copyright © 2021 Maxim Cournoyer <[email-masked]>
+# Copyright © 2021, 2022 Maxim Cournoyer <[email-masked]>
 #
 # This file is part of GNU Guix.
 #
@@ -92,15 +92,25 @@ _debug()
     fi
 }
 
-# Return true if user answered yes, false otherwise.
+_flush()
+{
+    while read -t0; do
+        read -N1
+    done
+}
+
+# Return true if user answered yes, false otherwise.  It defaults to "yes"
+# when a single newline character is input.
 # $1: The prompt question.
 prompt_yes_no() {
     while true; do
-        read -rp "$1 " yn
+        _flush
+        read -N1 -rsp "$1 [Y/n]" yn
         case $yn in
-            [Yy]*) return 0;;
-            [Nn]*) return 1;;
-            *) _msg "Please answer yes or no."
+            $'\n') echo && return 0;;
+            [Yy]*) echo && return 0;;
+            [Nn]*) echo && return 1;;
+            *) echo && _msg "Please answer yes or no."
         esac
     done
 }
@@ -137,7 +147,7 @@ chk_gpg_keyring()
         if ! gpg --dry-run --list-keys "$gpg_key_id" >/dev/null 2>&1; then
             if prompt_yes_no "${INF}The following OpenPGP public key is \
 required to verify the Guix binary signature: $gpg_key_id.
-Would you like me to fetch it for you? (yes/no)"; then
+Would you like me to fetch it for you?"; then
                 wget "https://sv.gnu.org/people/viewgpg.php?user_id=$user_id" \
                      --no-verbose -O- | gpg --import -
             else
@@ -254,7 +264,7 @@ chk_sys_nscd()
 configure_substitute_discovery() {
     if grep -q -- '--discover=no' "$1" && \
             prompt_yes_no "Would you like the Guix daemon to automatically \
-discover substitute servers on the local network? (yes/no)"; then
+discover substitute servers on the local network?"; then
         sed -i 's/--discover=no/--discover=yes/' "$1"
     fi
 }
@@ -490,7 +500,7 @@ sys_enable_guix_daemon()
 sys_authorize_build_farms()
 { # authorize the public key of the build farm
     if prompt_yes_no "Permit downloading pre-built package binaries from the \
-project's build farm? (yes/no)"; then
+project's build farm?"; then
         guix archive --authorize \
              < ~root/.config/guix/current/share/guix/ci.guix.gnu.org.pub \
             && _msg "${PAS}Authorized public key for ci.guix.gnu.org"
-- 
2.37.3