1 | // Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved. |
2 | // Use of this source code is governed by an MIT license |
3 | // that can be found in the LICENSE file. |
4 | // |
5 | // TODO Mac version needs to be implemented |
6 | // Will serve as more advanced input method |
7 | // based on the work of https://github.com/AmokHuginnsson/replxx |
8 | // |
9 | module readline |
10 | |
11 | import os |
12 | |
13 | // termios definitions |
14 | // https://github.com/openbsd/src/blob/master/sys/sys/termios.h |
15 | // https://github.com/vocho/openqnx/blob/master/trunk/lib/c/public/termios.h |
16 | // https://github.com/omniti-labs/illumos-omnios/blob/master/usr/src/uts/common/sys/termios.h |
17 | // https://github.com/torvalds/linux/blob/master/include/uapi/asm-generic/termbits.h |
18 | |
19 | struct Termios { |
20 | } |
21 | |
22 | // Only use standard os.get_line |
23 | // Need implementation for readline capabilities |
24 | // |
25 | // read_line_utf8 blocks execution in a loop and awaits user input |
26 | // characters from a terminal until `EOF` or `Enter` key is encountered |
27 | // in the input stream. |
28 | // read_line_utf8 returns the complete input line as an UTF-8 encoded `[]rune` or |
29 | // an error if the line is empty. |
30 | // The `prompt` `string` is output as a prefix text for the input capturing. |
31 | // read_line_utf8 is the main method of the `readline` module and `Readline` struct. |
32 | pub fn (mut r Readline) read_line_utf8(prompt string) ![]rune { |
33 | r.current = []rune{} |
34 | r.cursor = 0 |
35 | r.prompt = prompt |
36 | r.search_index = 0 |
37 | if r.previous_lines.len <= 1 { |
38 | r.previous_lines << []rune{} |
39 | r.previous_lines << []rune{} |
40 | } else { |
41 | r.previous_lines[0] = []rune{} |
42 | } |
43 | print(r.prompt) |
44 | line := os.get_raw_line() |
45 | if line.len >= 0 { |
46 | r.current = line.runes() |
47 | } |
48 | r.previous_lines[0] = []rune{} |
49 | r.search_index = 0 |
50 | if r.current.len == 0 { |
51 | return error('empty line') |
52 | } |
53 | return r.current |
54 | } |
55 | |
56 | // read_line does the same as `read_line_utf8` but returns user input as a `string`. |
57 | // (As opposed to `[]rune` returned by `read_line_utf8`). |
58 | pub fn (mut r Readline) read_line(prompt string) !string { |
59 | s := r.read_line_utf8(prompt)! |
60 | return s.string() |
61 | } |
62 | |
63 | // read_line_utf8 blocks execution in a loop and awaits user input |
64 | // characters from a terminal until `EOF` or `Enter` key is encountered |
65 | // in the input stream. |
66 | // read_line_utf8 returns the complete input line as an UTF-8 encoded `[]rune` or |
67 | // an error if the line is empty. |
68 | // The `prompt` `string` is output as a prefix text for the input capturing. |
69 | // read_line_utf8 is the main method of the `readline` module and `Readline` struct. |
70 | // NOTE that this version of `read_line_utf8` is a standalone function without |
71 | // persistent functionalities (e.g. history). |
72 | pub fn read_line_utf8(prompt string) ![]rune { |
73 | mut r := Readline{} |
74 | s := r.read_line_utf8(prompt)! |
75 | return s |
76 | } |
77 | |
78 | // read_line does the same as `read_line_utf8` but returns user input as a `string`. |
79 | // (As opposed to `[]rune` as returned by `read_line_utf8`). |
80 | // NOTE that this version of `read_line` is a standalone function without |
81 | // persistent functionalities (e.g. history). |
82 | pub fn read_line(prompt string) !string { |
83 | mut r := Readline{} |
84 | s := r.read_line(prompt)! |
85 | return s |
86 | } |