]> git.sur5r.net Git - c128-kasse/blobdiff - src/kasse.c
Implement space completion when selling items.
[c128-kasse] / src / kasse.c
index c225d8783233d8045bc6a5cdd43fc288e746dc16..2a78826978b1987cbb24bfee7f00dee196034afc 100644 (file)
@@ -128,10 +128,11 @@ static signed int buy(char *name, unsigned int price) {
        BYTE c, x, y, nickname_len;
        int einheiten;
        char *input;
-       char nickname[11];
+       char nickname[NICKNAME_MAX_LEN+1];
        char rest[11];
        struct credits_t *credit;
 
+       memset(nickname, '\0', sizeof(nickname));
        memset(rest, ' ', sizeof(rest));
        rest[8] = '\0';
 
@@ -146,10 +147,10 @@ static signed int buy(char *name, unsigned int price) {
 
                c = cgetc();
                /* Enter */
-               if (c == 13)
+               if (c == PETSCII_CR)
                        break;
                /* Backspace */
-               if (c == 20) {
+               if (c == PETSCII_DEL) {
                        if (i == 0)
                                continue;
                        entered[--i] = '\0';
@@ -165,7 +166,7 @@ static signed int buy(char *name, unsigned int price) {
                if (c == '-' && i == 0) {
                        negative = -1;
                        cputc(c);
-               } else if (c > 47 && c < 58) {
+               } else if (c >= PETSCII_0 && c <= PETSCII_9) {
                        entered[i++] = c;
                        cputc(c);
                }
@@ -184,10 +185,82 @@ static signed int buy(char *name, unsigned int price) {
        cprintf("\r\n             *** VERKAUF ***\r\n\r\n");
        cprintf("%dx %s", einheiten, name);
        toggle_videomode();
-       
+
        cprintf("\r\nAuf ein Guthaben kaufen? Wenn ja, Nickname eingeben:\r\n");
-       input = get_input();
-       strncpy(nickname, input, 11);
+       {
+               BYTE i;
+               BYTE x;
+               BYTE y;
+               BYTE matches;
+               char *uniquematch;
+               input_terminator_t terminator;
+               while (1) {
+                       terminator = get_input_terminated_by(
+                                       INPUT_TERMINATOR_RETURN |
+                                       INPUT_TERMINATOR_SPACE,
+                                       nickname,
+                                       sizeof(nickname));
+
+                       /* Clear the screen from any previous completions */
+                       x = wherex();
+                       y = wherey();
+                       for (i = 1; i < 7; i++) {
+                               /* "Completion:" is longer than NICKNAME_MAX_LEN */
+                               cclearxy(0, y + i, strlen("Completion:"));
+                       }
+                       gotoxy(x, y);
+
+                       if (terminator != INPUT_TERMINATOR_SPACE) {
+                               break;
+                       }
+
+                       matches = 0;
+                       uniquematch = NULL;
+                       for (i = 0; i < credits.num_items; i++) {
+                               if (strncmp(nickname, credits.credits[i].nickname, strlen(nickname)) != 0) {
+                                       continue;
+                               }
+                               matches++;
+                               if (matches > 1) {
+                                       break;
+                               }
+                               uniquematch = credits.credits[i].nickname;
+                       }
+                       if (matches == 1) {
+                               /* Display the rest of the nickname */
+                               textcolor(TC_LIGHT_GREEN);
+                               cprintf("%s", uniquematch + strlen(nickname));
+                               textcolor(TC_LIGHT_GRAY);
+                               strcat(nickname, uniquematch + strlen(nickname));
+                       } else {
+                               /* Multiple nicknames match what was entered so far. Abort and
+                                * display all matches, then prompt the user again. */
+                               char completion[NICKNAME_MAX_LEN+1];
+                               BYTE len = strlen(nickname);
+                               x = wherex();
+                               y = wherey();
+                               cprintf("\r\nCompletion:\r\n");
+                               matches = 0;
+                               for (i = 0; i < credits.num_items; i++) {
+                                       if (strncmp(nickname, credits.credits[i].nickname, len) != 0) {
+                                               continue;
+                                       }
+                                       if (++matches == 5) {
+                                               cprintf("...\r\n");
+                                               break;
+                                       }
+                                       strcpy(completion, credits.credits[i].nickname);
+                                       *(completion + len) = '\0';
+                                       cprintf("%s", completion);
+                                       textcolor(TC_LIGHT_GREEN);
+                                       cprintf("%c", *(credits.credits[i].nickname + len));
+                                       textcolor(TC_LIGHT_GRAY);
+                                       cprintf("%s\r\n", completion + len + 1);
+                               }
+                               gotoxy(x, y);
+                       }
+               }
+       }
        if (*nickname != '\0') {
                toggle_videomode();
                cprintf(" fuer %s\r\n", nickname);