Skip to content
This repository was archived by the owner on May 4, 2018. It is now read-only.

Commit 68cd6d6

Browse files
retep998saghul
authored andcommitted
windows: add tty unicode support for input
Switches from using ReadConsoleA to ReadConsoleW. Uses an auxiliary buffer to store the temporary utf-16 string. Signed-off-by: Peter Atashian <[email protected]> Signed-off-by: Saúl Ibarra Corretgé <[email protected]>
1 parent cf7b812 commit 68cd6d6

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

Diff for: src/win/tty.c

+22-7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
#define ANSI_IN_STRING 0x40
4949
#define ANSI_BACKSLASH_SEEN 0x80
5050

51+
#define MAX_INPUT_BUFFER_LENGTH 8192
52+
5153

5254
static void uv_tty_update_virtual_window(CONSOLE_SCREEN_BUFFER_INFO* info);
5355

@@ -303,6 +305,8 @@ static DWORD CALLBACK uv_tty_line_read_thread(void* data) {
303305
uv_tty_t* handle;
304306
uv_req_t* req;
305307
DWORD bytes, read_bytes;
308+
WCHAR utf16[MAX_INPUT_BUFFER_LENGTH / 3];
309+
DWORD chars, read_chars;
306310

307311
assert(data);
308312

@@ -314,18 +318,29 @@ static DWORD CALLBACK uv_tty_line_read_thread(void* data) {
314318
assert(handle->read_line_buffer.len > 0);
315319

316320
/* ReadConsole can't handle big buffers. */
317-
if (handle->read_line_buffer.len < 8192) {
321+
if (handle->read_line_buffer.len < MAX_INPUT_BUFFER_LENGTH) {
318322
bytes = handle->read_line_buffer.len;
319323
} else {
320-
bytes = 8192;
324+
bytes = MAX_INPUT_BUFFER_LENGTH;
321325
}
322326

323-
/* Todo: Unicode */
324-
if (ReadConsoleA(handle->read_line_handle,
325-
(void*) handle->read_line_buffer.base,
326-
bytes,
327-
&read_bytes,
327+
/* At last, unicode! */
328+
/* One utf-16 codeunit never takes more than 3 utf-8 codeunits to encode */
329+
chars = bytes / 3;
330+
331+
if (ReadConsoleW(handle->read_line_handle,
332+
(void*) utf16,
333+
chars,
334+
&read_chars,
328335
NULL)) {
336+
read_bytes = WideCharToMultiByte(CP_UTF8,
337+
0,
338+
utf16,
339+
read_chars,
340+
handle->read_line_buffer.base,
341+
bytes,
342+
NULL,
343+
NULL);
329344
SET_REQ_SUCCESS(req);
330345
req->overlapped.InternalHigh = read_bytes;
331346
} else {

0 commit comments

Comments
 (0)