windows: read console with ReadConsole when it is not redirected

pull/2260/head
vitalyster 2019-10-08 12:29:09 +03:00 committed by Alexander Medvednikov
parent b4bf12378e
commit e10848e0d5
1 changed files with 15 additions and 10 deletions

View File

@ -530,23 +530,28 @@ pub fn get_line() string {
// get_raw_line returns a one-line string from stdin along with '\n' if there is any // get_raw_line returns a one-line string from stdin along with '\n' if there is any
pub fn get_raw_line() string { 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 {
h_input := C.GetStdHandle(STD_INPUT_HANDLE)
mut nr_chars := 0
C.ReadConsole(h_input, buf, max_line_chars * 2, &nr_chars, 0)
return string_from_wide2(&u16(buf), nr_chars)
}
res := int( C.fgetws(buf, max_line_chars, C.stdin ) ) res := int( C.fgetws(buf, max_line_chars, C.stdin ) )
len := int( C.wcslen(&u16(buf)) ) len := int( C.wcslen(&u16(buf)) )
if 0 != res { return string_from_wide2( &u16(buf), len ) } if 0 != res { return string_from_wide2( &u16(buf), len ) }
return '' return ''
} $else {
max := size_t(256)
buf := *char(malloc(int(max)))
nr_chars := C.getline(&buf, &max, stdin)
if nr_chars == 0 {
return ''
}
return string(byteptr(buf), nr_chars)
} }
$else {
max := size_t(256)
buf := *char(malloc(int(max)))
nr_chars := C.getline(&buf, &max, stdin)
if nr_chars == 0 {
return ''
}
return string(byteptr(buf), nr_chars)
}
} }
pub fn get_lines() []string { pub fn get_lines() []string {