From 2ec568579823c991b919fa3a2c8583a8db21cb81 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Wed, 30 Jul 2025 23:58:32 +0300 Subject: implemented ^T command: allows typing by code and getting characters from stdin or the user * n:^T always prints bytes (cf. :^A) * ^T without arguments returns a codepoint or byte from stdin. In interactive mode, this currentply places a cursor in the message line and waits for a keypress. --- src/interface.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/interface.c') diff --git a/src/interface.c b/src/interface.c index cf8f1ca..c3103fd 100644 --- a/src/interface.c +++ b/src/interface.c @@ -130,3 +130,32 @@ teco_interface_stdio_msg(teco_msg_t type, const gchar *str, gsize len) break; } } + +/** + * Get character from stdin. + * + * @param widechar If TRUE reads one glyph encoded in UTF-8. + * If FALSE, returns exactly one byte. + * @return Codepoint or -1 in case of EOF. + */ +teco_int_t +teco_interface_stdio_getch(gboolean widechar) +{ + gchar buf[4]; + gint i = 0; + gint32 cp; + + do { + if (G_UNLIKELY(fread(buf+i, 1, 1, stdin) < 1)) + return -1; /* EOF */ + if (!widechar || !buf[i]) + return (guchar)buf[i]; + + /* doesn't work as expected when passed a null byte */ + cp = g_utf8_get_char_validated(buf, ++i); + if (i >= sizeof(buf) || cp != -2) + i = 0; + } while (cp < 0); + + return cp; +} -- cgit v1.2.3