Revert "repl: readline line editing "

This reverts commit 1956c6f906.
pull/2369/head
Alexander Medvednikov 2019-10-16 03:31:48 +03:00
parent 00fd1abf82
commit 66f36be7d0
5 changed files with 13 additions and 92 deletions

View File

@ -4,11 +4,8 @@
module compiler module compiler
import ( import os
os import term
term
readline
)
struct Repl { struct Repl {
mut: mut:
@ -84,27 +81,22 @@ 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]
mut prompt := '>>> '
for { for {
if r.indent == 0 { if r.indent == 0 {
prompt = '>>> ' print('>>> ')
} }
else { else {
prompt = '... ' print('... ')
} }
mut line := readline.read_line(prompt) or { r.line = os.get_raw_line()
break if r.line.trim_space() == '' && r.line.ends_with('\n') {
}
if line.trim_space() == '' && line.ends_with('\n') {
continue continue
} }
line = line.trim_space() r.line = r.line.trim_space()
if line.len <= -1 || line == '' || line == 'exit' { if r.line.len == -1 || r.line == '' || r.line == 'exit' {
break break
} }
r.line = line
if r.line == '\n' { if r.line == '\n' {
continue continue
} }

View File

@ -1,66 +0,0 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
// JS version
// Need to be implemented
// Will serve as more advanced input method
// Based on the work of https://github.com/AmokHuginnsson/replxx
module readline
import os
// Only use standard os.get_line
// Need implementation for readline capabilities
pub fn (r mut Readline) read_line_utf8(prompt string) ?ustring {
r.current = ''.ustring()
r.cursor = 0
r.prompt = prompt
r.search_index = 0
if r.previous_lines.len <= 1 {
r.previous_lines << ''.ustring()
r.previous_lines << ''.ustring()
}
else {
r.previous_lines[0] = ''.ustring()
}
print(r.prompt)
r.current = os.get_raw_line().ustring()
r.previous_lines[0] = ''.ustring()
r.search_index = 0
if r.current.s == '' {
return error('empty line')
}
return r.current
}
// Returns the string from the utf8 ustring
pub fn (r mut Readline) read_line(prompt string) ?string {
s := r.read_line_utf8(prompt) or {
return error(err)
}
return s.s
}
// Standalone function without persistent functionnalities (eg: history)
// Returns utf8 based ustring
pub fn read_line_utf8(prompt string) ?ustring {
mut r := Readline{}
s := r.read_line_utf8(prompt) or {
return error(err)
}
return s
}
// Standalone function without persistent functionnalities (eg: history)
// Return string from utf8 ustring
pub fn read_line(prompt string) ?string {
mut r := Readline{}
s := r.read_line(prompt) or {
return error(err)
}
return s
}

View File

@ -149,7 +149,7 @@ pub fn read_line(prompt string) ?string {
return s return s
} }
fn (r Readline) analyse(c int) Action { fn (r Readline) analyse(c byte) Action {
switch c { switch c {
case `\0`: return Action.eof case `\0`: return Action.eof
case 0x3 : return Action.eof // End of Text case 0x3 : return Action.eof // End of Text
@ -295,9 +295,7 @@ fn (r mut Readline) refresh_line() {
fn (r mut Readline) eof() bool { fn (r mut Readline) eof() bool {
r.previous_lines.insert(1, r.current) r.previous_lines.insert(1, r.current)
r.cursor = r.current.len r.cursor = r.current.len
if r.is_tty {
r.refresh_line() r.refresh_line()
}
return true return true
} }
@ -339,8 +337,8 @@ fn (r mut Readline) commit_line() bool {
a := '\n'.ustring() a := '\n'.ustring()
r.current = r.current + a r.current = r.current + a
r.cursor = r.current.len r.cursor = r.current.len
if r.is_tty {
r.refresh_line() r.refresh_line()
if r.is_tty {
println('') println('')
} }
return true return true

View File

@ -29,11 +29,8 @@ pub fn (r mut Readline) read_line_utf8(prompt string) ?ustring {
} }
print(r.prompt) print(r.prompt)
line := os.get_raw_line() r.current = os.get_line().ustring()
if line.len >= 0 {
r.current = line.ustring()
}
r.previous_lines[0] = ''.ustring() r.previous_lines[0] = ''.ustring()
r.search_index = 0 r.search_index = 0
if r.current.s == '' { if r.current.s == '' {

View File

@ -27,7 +27,7 @@ pub fn (r mut Readline) read_line_utf8(prompt string) ?ustring {
} }
print(r.prompt) print(r.prompt)
r.current = os.get_raw_line().ustring() r.current = os.get_line().ustring()
r.previous_lines[0] = ''.ustring() r.previous_lines[0] = ''.ustring()
r.search_index = 0 r.search_index = 0