From cfc50ab49cbbd852439b64a61eb79d5c276f9373 Mon Sep 17 00:00:00 2001 From: Spencer Low Date: Sun, 2 Sep 2018 16:23:29 -0700 Subject: [PATCH] adb: win32: LinePrinter Unicode support Test: Win10: adb push, adb install with Unicode filenames, resize console during test Change-Id: I19cb68a09a4e217fe06329185f4ede0656daa5a6 Signed-off-by: Spencer Low --- adb/client/line_printer.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/adb/client/line_printer.cpp b/adb/client/line_printer.cpp index 4dc2d2896..50c03e81b 100644 --- a/adb/client/line_printer.cpp +++ b/adb/client/line_printer.cpp @@ -31,6 +31,8 @@ // Stuff from ninja's util.h that's needed below. #include using namespace std; +// This does not account for multiple UTF-8 bytes corresponding to a single Unicode code point, or +// multiple code points corresponding to a single grapheme cluster (user-perceived character). string ElideMiddle(const string& str, size_t width) { const int kMargin = 3; // Space for "...". string result = str; @@ -85,9 +87,10 @@ void LinePrinter::Print(string to_print, LineType type) { CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(console_, &csbi); - // TODO: std::wstring to_print_wide; if (!android::base::UTF8ToWide(to_print, &to_print_wide)... - // TODO: wstring ElideMiddle. to_print = ElideMiddle(to_print, static_cast(csbi.dwSize.X)); + std::wstring to_print_wide; + // ElideMiddle may create invalid UTF-8, so ignore conversion errors. + (void)android::base::UTF8ToWide(to_print, &to_print_wide); // We don't want to have the cursor spamming back and forth, so instead of // printf use WriteConsoleOutput which updates the contents of the buffer, // but doesn't move the cursor position. @@ -100,12 +103,10 @@ void LinePrinter::Print(string to_print, LineType type) { }; vector char_data(csbi.dwSize.X); for (size_t i = 0; i < static_cast(csbi.dwSize.X); ++i) { - // TODO: UnicodeChar instead of AsciiChar, to_print_wide[i]. - char_data[i].Char.AsciiChar = i < to_print.size() ? to_print[i] : ' '; - char_data[i].Attributes = csbi.wAttributes; + char_data[i].Char.UnicodeChar = i < to_print_wide.size() ? to_print_wide[i] : L' '; + char_data[i].Attributes = csbi.wAttributes; } - // TODO: WriteConsoleOutputW. - WriteConsoleOutput(console_, &char_data[0], buf_size, zero_zero, &target); + WriteConsoleOutputW(console_, &char_data[0], buf_size, zero_zero, &target); #else // Limit output to width of the terminal if provided so we don't cause // line-wrapping.