v / vlib / readline
Raw file | 78 loc (72 sloc) | 2.66 KB | Latest commit hash 51f4d9939
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 Windows 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//
9module readline
10
11import os
12
13// needed for parity with readline_default.c.v
14struct Termios {
15}
16
17// Only use standard os.get_line
18// Need implementation for readline capabilities
19//
20// read_line_utf8 blocks execution in a loop and awaits user input
21// characters from a terminal until `EOF` or `Enter` key is encountered
22// in the input stream.
23// read_line_utf8 returns the complete input line as an UTF-8 encoded `[]rune` or
24// an error if the line is empty.
25// The `prompt` `string` is output as a prefix text for the input capturing.
26// read_line_utf8 is the main method of the `readline` module and `Readline` struct.
27pub fn (mut r Readline) read_line_utf8(prompt string) ![]rune {
28 r.current = []rune{}
29 r.cursor = 0
30 r.prompt = prompt
31 r.search_index = 0
32 if r.previous_lines.len <= 1 {
33 r.previous_lines << []rune{}
34 r.previous_lines << []rune{}
35 } else {
36 r.previous_lines[0] = []rune{}
37 }
38 print(r.prompt)
39 r.current = os.get_raw_line().runes()
40 r.previous_lines[0] = []rune{}
41 r.search_index = 0
42 if r.current.len == 0 {
43 return error('empty line')
44 }
45 return r.current
46}
47
48// read_line does the same as `read_line_utf8` but returns user input as a `string`.
49// (As opposed to `[]rune` returned by `read_line_utf8`).
50pub fn (mut r Readline) read_line(prompt string) !string {
51 s := r.read_line_utf8(prompt)!
52 return s.string()
53}
54
55// read_line_utf8 blocks execution in a loop and awaits user input
56// characters from a terminal until `EOF` or `Enter` key is encountered
57// in the input stream.
58// read_line_utf8 returns the complete input line as an UTF-8 encoded `[]rune` or
59// an error if the line is empty.
60// The `prompt` `string` is output as a prefix text for the input capturing.
61// read_line_utf8 is the main method of the `readline` module and `Readline` struct.
62// NOTE that this version of `read_line_utf8` is a standalone function without
63// persistent functionalities (e.g. history).
64pub fn read_line_utf8(prompt string) ![]rune {
65 mut r := Readline{}
66 s := r.read_line_utf8(prompt)!
67 return s
68}
69
70// read_line does the same as `read_line_utf8` but returns user input as a `string`.
71// (As opposed to `[]rune` as returned by `read_line_utf8`).
72// NOTE that this version of `read_line` is a standalone function without
73// persistent functionalities (e.g. history).
74pub fn read_line(prompt string) !string {
75 mut r := Readline{}
76 s := r.read_line(prompt)!
77 return s
78}