1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vrepl: implement the 'pin' command from crepl (#15371)

This commit is contained in:
l-m 2022-08-07 21:06:50 +10:00 committed by GitHub
parent fd1b6efea6
commit 8c33a40c5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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)
}
}