From 571322f9bf8ee1f8b57a88796d2c5239dadf67b3 Mon Sep 17 00:00:00 2001 From: "Jenson@Win32" Date: Sun, 4 Sep 2016 09:56:00 +1000 Subject: [PATCH] fix display of utf-8 chars on Windows Need to use WriteConsoleOutputCharacterW() to write wide chars --- linenoise.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/linenoise.c b/linenoise.c index aed0e02..dd62fae 100644 --- a/linenoise.c +++ b/linenoise.c @@ -670,11 +670,34 @@ static int outputChars(struct current *current, const char *buf, int len) COORD pos; DWORD n; - pos.X = (SHORT)current->x; pos.Y = (SHORT)current->y; - WriteConsoleOutputCharacter(current->outh, buf, len, pos, &n); +#ifdef USE_UTF8 + while ( len > 0 ) { + int c, s; + wchar_t wc; + + s = utf8_tounicode(buf, &c); + + len -= s; + buf += s; + + wc = (wchar_t)c; + + pos.X = (SHORT)current->x; + + /* fixed display utf8 character */ + WriteConsoleOutputCharacterW(current->outh, &wc, 1, pos, &n); + + current->x += utf8_width(c); + } +#else + pos.X = (SHORT)current->x; + + WriteConsoleOutputCharacterA(current->outh, buf, len, pos, &n); current->x += len; +#endif + return 0; } @@ -752,7 +775,7 @@ static int fd_read(struct current *current) } /* Note that control characters are already translated in AsciiChar */ else if (k->wVirtualKeyCode == VK_CONTROL) - continue; + continue; else { #ifdef USE_UTF8 return k->uChar.UnicodeChar; -- 2.44.0