diff --git a/compiler/cheaders.v b/compiler/cheaders.v index 197a9a92c3..2753c09f28 100644 --- a/compiler/cheaders.v +++ b/compiler/cheaders.v @@ -151,9 +151,6 @@ byteptr g_str_buf; int load_so(byteptr); void reload_so(); void init_consts(); -#ifdef _WIN32 -BOOL isConsole; -#endif ' js_headers = ' diff --git a/compiler/main.v b/compiler/main.v index f62291fe35..742a25577d 100644 --- a/compiler/main.v +++ b/compiler/main.v @@ -387,16 +387,6 @@ fn (v mut V) generate_main() { mut consts_init_body := cgen.consts_init.join_lines() // vlib can't have `init_consts()` cgen.genln('void init_consts() { -#ifdef _WIN32 -DWORD consoleMode; -isConsole = GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &consoleMode); -int mode = isConsole ? _O_U16TEXT : _O_U8TEXT; -_setmode(_fileno(stdin), mode); -_setmode(_fileno(stdout), _O_U8TEXT); -SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), ENABLE_PROCESSED_OUTPUT | 0x0004); -// ENABLE_VIRTUAL_TERMINAL_PROCESSING -setbuf(stdout,0); -#endif g_str_buf=malloc(1000); $consts_init_body }') diff --git a/vlib/builtin/builtin.v b/vlib/builtin/builtin.v index f9f229571f..60d0d0e9f3 100644 --- a/vlib/builtin/builtin.v +++ b/vlib/builtin/builtin.v @@ -4,6 +4,24 @@ module builtin +fn builtin_init() int { + $if windows { + if is_atty(0) { + C._setmode(C._fileno(C.stdin), C._O_U16TEXT) + } else { + C._setmode(C._fileno(C.stdin), C._O_U8TEXT) + } + C._setmode(C._fileno(C.stdout), C._O_U8TEXT) + C.SetConsoleMode(C.GetStdHandle(C.STD_OUTPUT_HANDLE), C.ENABLE_PROCESSED_OUTPUT | 0x0004) // ENABLE_VIRTUAL_TERMINAL_PROCESSING + C.setbuf(C.stdout,0) + } + return 1 +} + +const ( + _ = builtin_init() +) + fn C.memcpy(byteptr, byteptr, int) fn C.memmove(byteptr, byteptr, int) //fn C.malloc(int) byteptr @@ -158,7 +176,15 @@ fn memdup(src voidptr, sz int) voidptr { fn v_ptr_free(ptr voidptr) { C.free(ptr) -} - +} +pub fn is_atty(fd int) bool { + $if windows { + mut mode := 0 + C.GetConsoleMode(C._get_osfhandle(fd), &mode) + return mode > 0 + } $else { + return C.isatty(fd) != 0 + } +} diff --git a/vlib/os/os.v b/vlib/os/os.v index df4b160206..92d7b5c069 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -69,11 +69,6 @@ fn C.ftell(fp voidptr) int fn C.getenv(byteptr) byteptr fn C.sigaction(int, voidptr, int) -fn parse_windows_cmd_line(cmd byteptr) []string { - s := string(cmd) - return s.split(' ') -} - // read_file reads the file in `path` and returns the contents. pub fn read_file(path string) ?string { mode := 'rb' @@ -533,7 +528,7 @@ pub fn get_raw_line() string { $if windows { max_line_chars := 256 buf := &byte(malloc(max_line_chars*2)) - if C.isConsole > 0 { + if is_atty(0) { h_input := C.GetStdHandle(STD_INPUT_HANDLE) mut nr_chars := 0 C.ReadConsole(h_input, buf, max_line_chars * 2, &nr_chars, 0) @@ -861,3 +856,4 @@ pub fn print_backtrace() { # backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO) ; */ } + diff --git a/vlib/term/can_show_color.v b/vlib/term/can_show_color.v index c142b5b947..2e8ad05631 100644 --- a/vlib/term/can_show_color.v +++ b/vlib/term/can_show_color.v @@ -1,11 +1,13 @@ module term +import os + pub fn can_show_color_on_stdout() bool { - return can_show_color_on_fd(1) + return is_atty(1) && os.getenv('TERM') != 'dumb' } pub fn can_show_color_on_stderr() bool { - return can_show_color_on_fd(2) + return is_atty(2) && os.getenv('TERM') != 'dumb' } ////////////////////////////////////////////// diff --git a/vlib/term/can_show_color_nix.v b/vlib/term/can_show_color_nix.v deleted file mode 100644 index 8b24aa0544..0000000000 --- a/vlib/term/can_show_color_nix.v +++ /dev/null @@ -1,11 +0,0 @@ -module term - -import os - -fn C.isatty(int) int - -pub fn can_show_color_on_fd(fd int) bool { - if os.getenv('TERM') == 'dumb' { return false } - if C.isatty(fd) != 0 { return true } - return false -} diff --git a/vlib/term/can_show_color_win.v b/vlib/term/can_show_color_win.v deleted file mode 100644 index edfabddd54..0000000000 --- a/vlib/term/can_show_color_win.v +++ /dev/null @@ -1,10 +0,0 @@ -module term - -import os - -// TODO: implement proper checking on windows too. -// For now, just return false by default -pub fn can_show_color_on_fd(fd int) bool { - if os.getenv('TERM') == 'dumb' { return false } - return false -}