Skip to content

Commit 1571645

Browse files
authored
vrepl: fix os.input() (#21811)
1 parent 4475759 commit 1571645

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

cmd/tools/vrepl.v

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,11 @@ fn run_repl(workdir string, vrepl_prefix string) int {
422422
}
423423
continue
424424
}
425-
// Save the source only if the user is printing something,
426-
// but don't add this print call to the `lines` array,
427-
// so that it doesn't get called during the next print.
428425
if r.line.starts_with('=') {
429426
r.line = 'println(' + r.line[1..] + ')'
430427
}
431428
if r.line.starts_with('print(') || r.line.starts_with('println(') {
429+
// >>> println('hello')
432430
source_code := r.current_source_code(false, false) + '\n${r.line}\n'
433431
os.write_file(temp_file, source_code) or { panic(err) }
434432
s := repl_run_vfile(temp_file) or { return 1 }
@@ -440,6 +438,17 @@ fn run_repl(workdir string, vrepl_prefix string) int {
440438
r.lines << r.line
441439
}
442440
}
441+
} else if r.line.contains('os.input(') {
442+
// >>> s := os.input('name: ')
443+
prompt_str := r.line.all_after('os.input(').all_before(')').trim('\'"')
444+
line_t := r.get_one_line(prompt_str) or { break }.trim_right('\n')
445+
trans_line := r.line.all_before('os.input(') + "'${line_t}'"
446+
source_code := r.current_source_code(false, false) + '\n${trans_line}\n'
447+
os.write_file(temp_file, source_code) or { panic(err) }
448+
s := repl_run_vfile(temp_file) or { return 1 }
449+
if s.exit_code == 0 {
450+
r.lines << trans_line
451+
}
443452
} else {
444453
mut temp_line := r.line
445454
func_call, fntype := r.function_call(r.line)

vlib/v/slow_tests/repl/input.repl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
mut s := os.input('aaa:')
2+
hello
3+
s
4+
s = os.input('bbb:')
5+
world
6+
s
7+
===output===
8+
hello
9+
world

0 commit comments

Comments
 (0)