From 96b9bd217dcfd2f9e655944aa68b99727ae7838c Mon Sep 17 00:00:00 2001
From: Ineiev <ineiev@gnu.org>
Date: Fri, 8 Apr 2016 15:29:08 +0300
Subject: [PATCH] tty: Show supplied message when using default

tty/pinentry-tty.c (fputs_highlighted): New function.
tty/pinentry-tty.c (button): Display the supplied text when
falling back to default; the default text is shown in braces
and provides the accelerator.

--
This allows using button texts written with non-ASCII characters.

Signed-off-by: Ineiev <ineiev@gnu.org>
---
 tty/pinentry-tty.c | 44 +++++++++++++++++++++++++++++---------------
 1 file changed, 29 insertions(+), 15 deletions(-)

diff --git a/tty/pinentry-tty.c b/tty/pinentry-tty.c
index 700b0f1..77de6d9 100644
--- a/tty/pinentry-tty.c
+++ b/tty/pinentry-tty.c
@@ -69,10 +69,32 @@ cbreak (int fd)
 #define ALERT_START "\033[1;31m"
 #define NORMAL_RESTORE "\033[0m"
 
+static void
+fputs_highlighted (char *text, char *highlight, FILE *ttyfo)
+{
+  for (; *text; text ++)
+    {
+      /* Skip accelerator prefix.  */
+      if (*text == '_')
+        {
+          text ++;
+          if (! *text)
+            break;
+        }
+
+      if (text == highlight)
+        fputs (UNDERLINE_START, ttyfo);
+      fputc (*text, ttyfo);
+      if (text == highlight)
+        fputs (NORMAL_RESTORE, ttyfo);
+    }
+}
+
 static char
 button (char *text, char *default_text, FILE *ttyfo)
 {
   char *highlight;
+  int use_default = 0;
 
   if (! text)
     return 0;
@@ -110,25 +132,17 @@ button (char *text, char *default_text, FILE *ttyfo)
     {
       if (! default_text)
 	return 0;
-      text = highlight = default_text;
+      highlight = default_text;
+      use_default = 1;
     }
 
   fputs ("  ", ttyfo);
-  for (; *text; text ++)
+  fputs_highlighted (text, highlight, ttyfo);
+  if (use_default)
     {
-      /* Skip accelerator prefix.  */
-      if (*text == '_')
-        {
-          text ++;
-          if (! *text)
-            break;
-        }
-
-      if (text == highlight)
-	fputs (UNDERLINE_START, ttyfo);
-      fputc (*text, ttyfo);
-      if (text == highlight)
-	fputs (NORMAL_RESTORE, ttyfo);
+      fputs (" (", ttyfo);
+      fputs_highlighted (default_text, highlight, ttyfo);
+      fputc (')', ttyfo);
     }
   fputc ('\n', ttyfo);
 
-- 
2.7.4

