]> git.lizzy.rs Git - linenoise.git/commitdiff
fix display of utf-8 chars on Windows
authorJenson@Win32 <jenson.shixf@gmail.com>
Sat, 3 Sep 2016 23:56:00 +0000 (09:56 +1000)
committerSteve Bennett <steveb@workware.net.au>
Sun, 4 Sep 2016 01:30:20 +0000 (11:30 +1000)
Need to use WriteConsoleOutputCharacterW() to write wide chars

linenoise.c

index aed0e0223924b3d97dabf7fdacdc31c4cba11105..dd62faea9a61ad461c13496392b12cf443eee8f7 100644 (file)
@@ -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;