From: Jenson@Win32 Date: Sat, 3 Sep 2016 23:56:00 +0000 (+1000) Subject: fix display of utf-8 chars on Windows X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=571322f9bf8ee1f8b57a88796d2c5239dadf67b3;p=linenoise.git fix display of utf-8 chars on Windows Need to use WriteConsoleOutputCharacterW() to write wide chars --- 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;