From 8c33a40c5a1637eab8937e520259c9c2de6875e3 Mon Sep 17 00:00:00 2001 From: l-m <66291634+l1mey112@users.noreply.github.com> Date: Sun, 7 Aug 2022 21:06:50 +1000 Subject: [PATCH] vrepl: implement the 'pin' command from crepl (#15371) --- cmd/tools/vrepl.v | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/cmd/tools/vrepl.v b/cmd/tools/vrepl.v index 2e29383750..0e9732e31e 100644 --- a/cmd/tools/vrepl.v +++ b/cmd/tools/vrepl.v @@ -16,6 +16,7 @@ mut: indent int // indentation level in_func bool // are we inside a new custom user function line string // the current line entered by the user + is_pin bool // does the repl 'pin' entered source code // modules []string // all the import modules alias map[string]string // all the alias used in the import @@ -68,6 +69,7 @@ fn repl_help() { |reset Clears the accumulated program, so you can start a fresh. |Ctrl-C, Ctrl-D, exit Exits the REPL. |clear Clears the screen. + |pin Pins the entered program to the top. '.strip_margin()) } @@ -208,6 +210,18 @@ fn (mut r Repl) parse_import(line string) { } } +// clear the screen, then list source code +fn (mut r Repl) pin() { + term.erase_clear() + r.list_source() +} + +// print source code +fn (mut r Repl) list_source() { + source_code := r.current_source_code(true, true) + println('\n${source_code.replace('\n\n', '\n')}') +} + fn highlight_console_command(command string) string { return term.bright_white(term.bright_bg_black(' $command ')) } @@ -349,8 +363,15 @@ fn run_repl(workdir string, vrepl_prefix string) int { continue } if r.line == 'list' { - source_code := r.current_source_code(true, true) - println('\n${source_code.replace('\n\n', '\n')}') + r.list_source() + continue + } + if r.line == 'pin' { + r.is_pin = !r.is_pin + if r.is_pin { + r.pin() + println('') + } continue } // Save the source only if the user is printing something, @@ -452,6 +473,10 @@ fn run_repl(workdir string, vrepl_prefix string) int { r.temp_lines.delete(0) } } + if r.is_pin && !temp_flag { + r.pin() + println('') + } print_output(s) } }