term: colors on Windows console
* isConsole moved to builtin is_atty function * Windows console initialization moved to builtin.initpull/2293/head
parent
de36b61931
commit
e64609387d
|
@ -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 = '
|
||||
|
|
|
@ -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
|
||||
}')
|
||||
|
|
|
@ -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
|
||||
|
@ -160,5 +178,13 @@ 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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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) ;
|
||||
*/
|
||||
}
|
||||
|
||||
|
|
|
@ -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'
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue