implement get_line & get_raw_line for windows + REPL

pull/937/head
joe-conigliaro 2019-07-03 00:39:40 +10:00 committed by Alexander Medvednikov
parent b9586a4017
commit 4ed67fbe7e
2 changed files with 27 additions and 26 deletions

View File

@ -937,10 +937,6 @@ fn new_v(args[]string) *V {
}
fn run_repl() []string {
$if windows {
println('REPL does not work on Windows yet, sorry!')
exit(1)
}
println('V $Version')
println('Use Ctrl-D or `exit` to exit')
println('For now you have to use println() to print values, this will be fixed soon\n')

View File

@ -405,34 +405,39 @@ pub fn filename(path string) string {
}
// get_line returns a one-line string from stdin
//u64 is used because C.getline needs a size_t as second argument
//Otherwise, it would cause a valgrind warning and may be dangerous
//Malloc takes an int as argument so a cast has to be made
pub fn get_line() string {
$if windows {
panic('get_line() not implemented on Windows yet, sorry!')
}
$else {
max := u64(256)
buf := malloc(int(max))
nr_chars := C.getline(&buf, &max, stdin)
if nr_chars == 0 {
return ''
}
if buf[nr_chars - 1] == `\n` { // newline
return tos(buf, nr_chars - 1)
}
// To prevent cutting end of line if no newline
return tos(buf, nr_chars)
}
str := get_raw_line()
if str[str.len - 1] == `\n` {
return str.substr(0, str.len - 1)
}
return str
}
const(
STD_INPUT_HANDLE = -10
)
// get_raw_line returns a one-line string from stdin along with '\n' if there is any
pub fn get_raw_line() string {
$if windows {
panic('get_raw_line() not implemented on Windows yet, sorry!')
}
$else {
max := 256
buf := malloc(max)
// TODO: Use HANDLE instead of voidptr
h_input := voidptr(C.GetStdHandle(STD_INPUT_HANDLE))
nr_chars := 0
// NOTE: Once we have UTF8 encode function to
// convert utf16 to utf8, change to ReadConsoleW
C.ReadConsole(h_input, buf, max, &nr_chars, 0)
if nr_chars == 0 {
return ''
}
return tos(buf, nr_chars)
}
$else {
//u64 is used because C.getline needs a size_t as second argument
//Otherwise, it would cause a valgrind warning and may be dangerous
//Malloc takes an int as argument so a cast has to be made
max := u64(256)
buf := malloc(int(max))
nr_chars := C.getline(&buf, &max, stdin)