From 65fed2d784b4f99920497fe206607d94676ff085 Mon Sep 17 00:00:00 2001 From: shivakishore14 Date: Fri, 28 Jun 2019 03:21:25 +0530 Subject: [PATCH] Create new line while pressing enter in repl. Pressing Enter in repl should create new line instead of quiting the repl. --- compiler/main.v | 6 +++++- os/os.v | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/compiler/main.v b/compiler/main.v index efb25a8795..ca0ecdd253 100644 --- a/compiler/main.v +++ b/compiler/main.v @@ -858,7 +858,11 @@ fn run_repl() []string { mut lines := []string for { print('>>> ') - mut line := os.get_line().trim_space() + mut line := os.get_raw_line() + if line.trim_space() == '' && line.ends_with('\n') { + continue + } + line = line.trim_space() if line == '' { break } diff --git a/os/os.v b/os/os.v index 594129fe4a..0dfd7a220b 100644 --- a/os/os.v +++ b/os/os.v @@ -383,6 +383,17 @@ pub fn get_line() string { return tos(buf, nr_chars) } +// get_raw_line returns a one-line string from stdin along with '\n' if there is any +pub fn get_raw_line() string { + max := u64(256) + buf := malloc(int(max)) + nr_chars := C.getline(&buf, &max, stdin) + if nr_chars == 0 { + return '' + } + return tos(buf, nr_chars) +} + pub fn user_os() string { $if linux { return 'linux'