2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2020-12-24 14:10:34 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2020-12-30 16:09:13 +03:00
|
|
|
//
|
|
|
|
// TODO Windows version needs to be implemented.
|
2020-12-24 14:10:34 +03:00
|
|
|
// Will serve as more advanced input method
|
2020-12-30 16:09:13 +03:00
|
|
|
// based on the work of https://github.com/AmokHuginnsson/replxx
|
|
|
|
//
|
2020-12-24 14:10:34 +03:00
|
|
|
module readline
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
2021-09-06 19:04:14 +03:00
|
|
|
// needed for parity with readline_default.c.v
|
|
|
|
struct Termios {
|
|
|
|
}
|
|
|
|
|
2020-12-24 14:10:34 +03:00
|
|
|
// Only use standard os.get_line
|
|
|
|
// Need implementation for readline capabilities
|
2020-12-30 16:09:13 +03:00
|
|
|
//
|
|
|
|
// read_line_utf8 blocks execution in a loop and awaits user input
|
|
|
|
// characters from a terminal until `EOF` or `Enter` key is encountered
|
|
|
|
// in the input stream.
|
2021-06-30 22:30:28 +03:00
|
|
|
// read_line_utf8 returns the complete input line as an UTF-8 encoded `[]rune` or
|
2020-12-30 16:09:13 +03:00
|
|
|
// an error if the line is empty.
|
|
|
|
// The `prompt` `string` is output as a prefix text for the input capturing.
|
|
|
|
// read_line_utf8 is the main method of the `readline` module and `Readline` struct.
|
2022-10-20 22:14:33 +03:00
|
|
|
pub fn (mut r Readline) read_line_utf8(prompt string) ![]rune {
|
2021-06-30 22:30:28 +03:00
|
|
|
r.current = []rune{}
|
2020-12-30 16:09:13 +03:00
|
|
|
r.cursor = 0
|
|
|
|
r.prompt = prompt
|
|
|
|
r.search_index = 0
|
|
|
|
if r.previous_lines.len <= 1 {
|
2021-06-30 22:30:28 +03:00
|
|
|
r.previous_lines << []rune{}
|
|
|
|
r.previous_lines << []rune{}
|
2020-12-30 16:09:13 +03:00
|
|
|
} else {
|
2021-06-30 22:30:28 +03:00
|
|
|
r.previous_lines[0] = []rune{}
|
2020-12-30 16:09:13 +03:00
|
|
|
}
|
|
|
|
print(r.prompt)
|
2021-06-30 22:30:28 +03:00
|
|
|
r.current = os.get_raw_line().runes()
|
|
|
|
r.previous_lines[0] = []rune{}
|
2020-12-30 16:09:13 +03:00
|
|
|
r.search_index = 0
|
2021-06-30 22:30:28 +03:00
|
|
|
if r.current.len == 0 {
|
2020-12-30 16:09:13 +03:00
|
|
|
return error('empty line')
|
|
|
|
}
|
|
|
|
return r.current
|
2020-12-24 14:10:34 +03:00
|
|
|
}
|
|
|
|
|
2020-12-30 16:09:13 +03:00
|
|
|
// read_line does the same as `read_line_utf8` but returns user input as a `string`.
|
2021-06-30 22:30:28 +03:00
|
|
|
// (As opposed to `[]rune` returned by `read_line_utf8`).
|
2022-10-20 22:14:33 +03:00
|
|
|
pub fn (mut r Readline) read_line(prompt string) !string {
|
|
|
|
s := r.read_line_utf8(prompt)!
|
2021-06-30 22:30:28 +03:00
|
|
|
return s.string()
|
2020-12-24 14:10:34 +03:00
|
|
|
}
|
|
|
|
|
2020-12-30 16:09:13 +03:00
|
|
|
// read_line_utf8 blocks execution in a loop and awaits user input
|
|
|
|
// characters from a terminal until `EOF` or `Enter` key is encountered
|
|
|
|
// in the input stream.
|
2021-06-30 22:30:28 +03:00
|
|
|
// read_line_utf8 returns the complete input line as an UTF-8 encoded `[]rune` or
|
2020-12-30 16:09:13 +03:00
|
|
|
// an error if the line is empty.
|
|
|
|
// The `prompt` `string` is output as a prefix text for the input capturing.
|
|
|
|
// read_line_utf8 is the main method of the `readline` module and `Readline` struct.
|
|
|
|
// NOTE that this version of `read_line_utf8` is a standalone function without
|
|
|
|
// persistent functionalities (e.g. history).
|
2022-10-20 22:14:33 +03:00
|
|
|
pub fn read_line_utf8(prompt string) ![]rune {
|
2020-12-30 16:09:13 +03:00
|
|
|
mut r := Readline{}
|
2022-10-20 22:14:33 +03:00
|
|
|
s := r.read_line_utf8(prompt)!
|
2020-12-30 16:09:13 +03:00
|
|
|
return s
|
2020-12-24 14:10:34 +03:00
|
|
|
}
|
|
|
|
|
2020-12-30 16:09:13 +03:00
|
|
|
// read_line does the same as `read_line_utf8` but returns user input as a `string`.
|
2021-06-30 22:30:28 +03:00
|
|
|
// (As opposed to `[]rune` as returned by `read_line_utf8`).
|
2020-12-30 16:09:13 +03:00
|
|
|
// NOTE that this version of `read_line` is a standalone function without
|
|
|
|
// persistent functionalities (e.g. history).
|
2022-10-20 22:14:33 +03:00
|
|
|
pub fn read_line(prompt string) !string {
|
2020-12-30 16:09:13 +03:00
|
|
|
mut r := Readline{}
|
2022-10-20 22:14:33 +03:00
|
|
|
s := r.read_line(prompt)!
|
2020-12-30 16:09:13 +03:00
|
|
|
return s
|
2020-12-24 14:10:34 +03:00
|
|
|
}
|