]> granicus.if.org Git - nethack/commitdiff
X11 getline refinement
authorPatR <rankin@nethack.org>
Sun, 21 Feb 2021 00:25:12 +0000 (16:25 -0800)
committerPatR <rankin@nethack.org>
Sun, 21 Feb 2021 00:25:12 +0000 (16:25 -0800)
X11_getlin() echoing its prompt and response to message window
truncates combined value to maximum allowed pline (rather than
having pline truncate it).  But it was truncating the response
as if the prompt was maximum allowed length instead of its actual
length, so possibly hiding some of the user's text unnecessarily.

win/X11/winX.c

index c95ce05ddc5d67583c9cbff4f05c083c9bf8d04b..ed2d664debab7c24759ebc194063a1285b40d0cf 100644 (file)
@@ -1,4 +1,4 @@
-/* NetHack 3.7 winX.c  $NHDT-Date: 1613777904 2021/02/19 23:38:24 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.99 $ */
+/* NetHack 3.7 winX.c  $NHDT-Date: 1613867106 2021/02/21 00:25:06 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.100 $ */
 /* Copyright (c) Dean Luick, 1992                                 */
 /* NetHack may be freely redistributed.  See license for details. */
 
@@ -1942,13 +1942,25 @@ X11_getlin(const char *question, /* prompt */
        put prompt and response into the message window (and into
        core's dumplog history) unless play hasn't started yet */
     if (g.program_state.in_moveloop || g.program_state.gameover) {
-        const char *visanswer = (*input == '\033') ? "ESC" : input;
+        /* single space has meaning (to remove a previously applied name) so
+           show it clearly; don't care about legibility of multiple spaces */
+        const char *visanswer = !input[0] ? "<empty>"
+                                : (input[0] == ' ' && !input[1]) ? "<space>"
+                                  : (input[0] == '\033') ? "<esc>"
+                                    : input;
         int promptlen = (int) strlen(question),
             answerlen = (int) strlen(visanswer);
 
-        pline("%.*s %.*s", /* note: (QBUFSZ-1 + 1 + QBUFSZ-1) == (BUFSZ-1) */
-              min(promptlen, QBUFSZ - 1), question,
-              min(answerlen, QBUFSZ - 1), visanswer);
+        /* prompt should be limited to QBUFSZ-1 by caller; enforce that
+           here for the echoed value; answer could be up to BUFSZ-1 long;
+           pline() will truncate the whole message to that amount so we
+           truncate the answer for display; this only affects the echoed
+           response, not the actual response being returned to the core */
+        if (promptlen >= QBUFSZ)
+            promptlen = QBUFSZ - 1;
+        if (answerlen + 1 + promptlen >= BUFSZ) /* +1: separating space */
+            answerlen = BUFSZ - (1 + promptlen) - 1;
+        pline("%.*s %.*s", promptlen, question, answerlen, visanswer);
     }
 
     /* clear static pointer that's about to go stale */