Ship my own wcwidth for Windows and fix up the magic incantations to make UTF-8 in/out work

This commit is contained in:
K Lange 2021-03-04 22:01:23 +09:00
parent 2fafc06203
commit 6a9668041f
3 changed files with 1123 additions and 18 deletions

View File

@ -622,6 +622,10 @@ static int compileFile(char * argv[], int flags, char * fileName) {
#endif
int main(int argc, char * argv[]) {
#ifdef _WIN32
SetConsoleOutputCP(65001);
SetConsoleCP(65001);
#endif
int flags = 0;
int moduleAsMain = 0;
int opt;

View File

@ -24,9 +24,7 @@
#else
#include <windows.h>
#include <io.h>
#define _O_U8TEXT 0x00040000
/* How do we get this in Windows? Seems WT asks the font? */
#define wcwidth(c) (1)
#include "wcwidth.hx"
#endif
#ifdef __toaru__
#include <toaru/rline.h>
@ -235,10 +233,27 @@ static int getch(int timeout) {
#ifndef _WIN32
return fgetc(stdin);
#else
TCHAR buf[1];
static int bytesRead = 0;
static char buf8[8];
static uint8_t * b;
if (bytesRead) {
bytesRead--;
return *(b++);
}
DWORD dwRead;
while (!ReadConsole(GetStdHandle(STD_INPUT_HANDLE),buf,1,&dwRead,NULL));
return buf[0];
uint16_t buf16[8] = {0};
if (ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE),buf16,2,&dwRead,NULL)) {
int r = WideCharToMultiByte(CP_UTF8, 0, buf16, -1, buf8, 8, 0, 0);
if (r > 1 && buf8[r-1] == '\0') r--;
b = (uint8_t*)buf8;
bytesRead = r - 1;
return *(b++);
} else {
fprintf(stderr, "error on console read\n");
return -1;
}
#endif
}
@ -306,11 +321,6 @@ static int codepoint_width(int codepoint) {
/* Non-breaking space _ */
return 1;
}
#ifdef _WIN32
if (codepoint > 127) {
return (codepoint < 0x10000) ? 8 : 10;
}
#else
/* Skip wcwidth for anything under 256 */
if (codepoint > 256) {
/* Higher codepoints may be wider (eg. Japanese) */
@ -319,7 +329,6 @@ static int codepoint_width(int codepoint) {
/* Invalid character, render as [U+ABCD] or [U+ABCDEF] */
return (codepoint < 0x10000) ? 8 : 10;
}
#endif
return 1;
}
@ -1388,11 +1397,7 @@ static void render_line(void) {
#endif
} else if (i > 0 && is_spaces && c.codepoint == ' ' && !(i % 4)) {
set_colors(COLOR_ALT_FG, COLOR_BG); /* Normal background so this is more subtle */
#ifndef _WIN32
printf("");
#else
printf("|");
#endif
set_colors(last_color ? last_color : COLOR_FG, COLOR_BG);
} else {
/* Normal characters get output */
@ -1990,14 +1995,12 @@ static int read_line(void) {
int this_buf[20];
uint32_t istate = 0;
#ifndef _WIN32
/* Let's disable this under Windows... */
set_colors(COLOR_ALT_FG, COLOR_ALT_BG);
fprintf(stdout, "\033[0m"); /* TODO: This could be retrieved from an envvar */
for (int i = 0; i < rline_terminal_width - 1; ++i) {
fprintf(stdout, " ");
}
#endif
if (rline_preload) {
char * c = rline_preload;
@ -2128,7 +2131,11 @@ static int read_line(void) {
* Read a line of text with interactive editing.
*/
int rline(char * buffer, int buf_size) {
#ifndef _WIN32
setlocale(LC_ALL, "");
#else
setlocale(LC_ALL, "C.UTF-8");
#endif
get_initial_termios();
set_unbuffered();
get_size();

1094
src/wcwidth.hx Normal file

File diff suppressed because it is too large Load Diff