Refactor learn keys.

* (learn_store_key):
  * (learn_key): use GString for key buffer.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2023-12-27 09:41:02 +03:00
parent 2d86b8889f
commit 07db4c4fa3

View File

@ -1173,28 +1173,19 @@ getch_with_timeout (unsigned int delay_us)
/* --------------------------------------------------------------------------------------------- */
static void
learn_store_key (char *buffer, char **p, int c)
learn_store_key (GString * buffer, int c)
{
if (*p - buffer > 253)
return;
if (c == ESC_CHAR)
{
*(*p)++ = '\\';
*(*p)++ = 'e';
}
g_string_append (buffer, "\\e");
else if (c < ' ')
{
*(*p)++ = '^';
*(*p)++ = c + 'a' - 1;
g_string_append_c (buffer, '^');
g_string_append_c (buffer, c + 'a' - 1);
}
else if (c == '^')
{
*(*p)++ = '^';
*(*p)++ = '^';
}
g_string_append (buffer, "^^");
else
*(*p)++ = (char) c;
g_string_append_c (buffer, (char) c);
}
/* --------------------------------------------------------------------------------------------- */
@ -2163,14 +2154,15 @@ learn_key (void)
fd_set Read_FD_Set;
gint64 end_time;
int c;
char buffer[256];
char *p = buffer;
GString *buffer;
buffer = g_string_sized_new (16);
tty_keypad (FALSE); /* disable interpreting keys by ncurses */
c = tty_lowlevel_getch ();
while (c == -1)
c = tty_lowlevel_getch (); /* Sanity check, should be unnecessary */
learn_store_key (buffer, &p, c);
learn_store_key (buffer, c);
end_time = g_get_monotonic_time () + LEARN_TIMEOUT * MC_USEC_PER_MSEC;
@ -2194,12 +2186,12 @@ learn_key (void)
}
if (c == -1)
break;
learn_store_key (buffer, &p, c);
learn_store_key (buffer, c);
}
tty_keypad (TRUE);
tty_nodelay (FALSE);
*p = '\0';
return (buffer[0] != '\0' ? g_strdup (buffer) : NULL);
return g_string_free (buffer, buffer->len == 0);
#undef LEARN_TIMEOUT
}