repl: add readline for user input

pull/2385/head
Henrixounez 2019-10-16 17:38:07 +02:00 committed by Alexander Medvednikov
parent 99e07718da
commit 83732642ac
1 changed files with 18 additions and 10 deletions

View File

@ -4,8 +4,11 @@
module compiler module compiler
import os import (
import term os
term
readline
)
struct Repl { struct Repl {
mut: mut:
@ -74,6 +77,7 @@ pub fn run_repl() []string {
println('Use Ctrl-C or `exit` to exit') println('Use Ctrl-C or `exit` to exit')
file := '.vrepl.v' file := '.vrepl.v'
temp_file := '.vrepl_temp.v' temp_file := '.vrepl_temp.v'
mut prompt := '>>> '
defer { defer {
os.rm(file) os.rm(file)
os.rm(temp_file) os.rm(temp_file)
@ -81,22 +85,26 @@ pub fn run_repl() []string {
os.rm(temp_file.left(temp_file.len - 2)) os.rm(temp_file.left(temp_file.len - 2))
} }
mut r := Repl{} mut r := Repl{}
mut readline := readline.Readline{}
vexe := os.args[0] vexe := os.args[0]
for { for {
if r.indent == 0 { if r.indent == 0 {
print('>>> ') prompt = '>>> '
} }
else { else {
print('... ') prompt = '... '
} }
r.line = os.get_raw_line() mut line := readline.read_line(prompt) or {
if r.line.trim_space() == '' && r.line.ends_with('\n') {
continue
}
r.line = r.line.trim_space()
if r.line.len == -1 || r.line == '' || r.line == 'exit' {
break break
} }
if line.trim_space() == '' && line.ends_with('\n') {
continue
}
line = line.trim_space()
if line.len <= -1 || line == '' || line == 'exit' {
break
}
r.line = line
if r.line == '\n' { if r.line == '\n' {
continue continue
} }