diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2025-07-30 23:58:32 +0300 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2025-07-31 00:33:43 +0300 |
commit | 2ec568579823c991b919fa3a2c8583a8db21cb81 (patch) | |
tree | 5ee30e3cde1df8b284aec73380c34b79afdbc8ab /src/interface.c | |
parent | 86fbf212de71a83e7bb4d83a4b33e54bed52dff9 (diff) | |
download | sciteco-2ec568579823c991b919fa3a2c8583a8db21cb81.tar.gz |
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.
Diffstat (limited to 'src/interface.c')
-rw-r--r-- | src/interface.c | 29 |
1 files changed, 29 insertions, 0 deletions
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; +} |