2020-01-23 21:04:46 +01:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-10-15 08:04:22 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Mac 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
|
|
|
|
|
2019-10-16 11:46:24 +02:00
|
|
|
#include <sys/termios.h>
|
|
|
|
|
2019-10-15 08:04:22 +02:00
|
|
|
// Only use standard os.get_line
|
|
|
|
// Need implementation for readline capabilities
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Readline) read_line_utf8(prompt string) ?ustring {
|
2019-10-15 08:04:22 +02:00
|
|
|
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)
|
2019-10-16 11:46:24 +02:00
|
|
|
line := os.get_raw_line()
|
2019-10-15 08:04:22 +02:00
|
|
|
|
2019-10-16 11:46:24 +02:00
|
|
|
if line.len >= 0 {
|
|
|
|
r.current = line.ustring()
|
|
|
|
}
|
2019-10-15 08:04:22 +02:00
|
|
|
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
|
2020-05-17 13:51:18 +02:00
|
|
|
pub fn (mut r Readline) read_line(prompt string) ?string {
|
2020-08-29 01:58:03 +02:00
|
|
|
s := r.read_line_utf8(prompt)?
|
2019-10-15 08:04:22 +02:00
|
|
|
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{}
|
2020-08-29 01:58:03 +02:00
|
|
|
s := r.read_line_utf8(prompt)?
|
2019-10-15 08:04:22 +02:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Standalone function without persistent functionnalities (eg: history)
|
|
|
|
// Return string from utf8 ustring
|
|
|
|
pub fn read_line(prompt string) ?string {
|
|
|
|
mut r := Readline{}
|
2020-08-29 01:58:03 +02:00
|
|
|
s := r.read_line(prompt)?
|
2019-10-15 08:04:22 +02:00
|
|
|
return s
|
|
|
|
}
|