term: colors on Windows console

* isConsole moved to builtin is_atty function
* Windows console initialization moved to builtin.init
pull/2293/head
vitalyster 2019-10-10 20:08:36 +03:00 committed by Alexander Medvednikov
parent de36b61931
commit e64609387d
7 changed files with 34 additions and 44 deletions

View File

@ -151,9 +151,6 @@ byteptr g_str_buf;
int load_so(byteptr); int load_so(byteptr);
void reload_so(); void reload_so();
void init_consts(); void init_consts();
#ifdef _WIN32
BOOL isConsole;
#endif
' '
js_headers = ' js_headers = '

View File

@ -387,16 +387,6 @@ fn (v mut V) generate_main() {
mut consts_init_body := cgen.consts_init.join_lines() mut consts_init_body := cgen.consts_init.join_lines()
// vlib can't have `init_consts()` // vlib can't have `init_consts()`
cgen.genln('void 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); g_str_buf=malloc(1000);
$consts_init_body $consts_init_body
}') }')

View File

@ -4,6 +4,24 @@
module builtin 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.memcpy(byteptr, byteptr, int)
fn C.memmove(byteptr, byteptr, int) fn C.memmove(byteptr, byteptr, int)
//fn C.malloc(int) byteptr //fn C.malloc(int) byteptr
@ -160,5 +178,13 @@ fn v_ptr_free(ptr voidptr) {
C.free(ptr) 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
}
}

View File

@ -69,11 +69,6 @@ fn C.ftell(fp voidptr) int
fn C.getenv(byteptr) byteptr fn C.getenv(byteptr) byteptr
fn C.sigaction(int, voidptr, int) 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. // read_file reads the file in `path` and returns the contents.
pub fn read_file(path string) ?string { pub fn read_file(path string) ?string {
mode := 'rb' mode := 'rb'
@ -533,7 +528,7 @@ pub fn get_raw_line() string {
$if windows { $if windows {
max_line_chars := 256 max_line_chars := 256
buf := &byte(malloc(max_line_chars*2)) buf := &byte(malloc(max_line_chars*2))
if C.isConsole > 0 { if is_atty(0) {
h_input := C.GetStdHandle(STD_INPUT_HANDLE) h_input := C.GetStdHandle(STD_INPUT_HANDLE)
mut nr_chars := 0 mut nr_chars := 0
C.ReadConsole(h_input, buf, max_line_chars * 2, &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) ; # backtrace_symbols_fd(buffer, nptrs, STDOUT_FILENO) ;
*/ */
} }

View File

@ -1,11 +1,13 @@
module term module term
import os
pub fn can_show_color_on_stdout() bool { 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 { pub fn can_show_color_on_stderr() bool {
return can_show_color_on_fd(2) return is_atty(2) && os.getenv('TERM') != 'dumb'
} }
////////////////////////////////////////////// //////////////////////////////////////////////

View File

@ -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
}

View File

@ -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
}