v
Зеркало из https://github.com/vlang/v
1// Copyright (c) 2019-2024 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//
9module readline
10
11import 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
19struct 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.
32pub fn (mut r Readline) read_line_utf8(prompt string) ![]rune {
33r.current = []rune{}
34r.cursor = 0
35r.prompt = prompt
36r.search_index = 0
37if r.previous_lines.len <= 1 {
38r.previous_lines << []rune{}
39r.previous_lines << []rune{}
40} else {
41r.previous_lines[0] = []rune{}
42}
43print(r.prompt)
44line := os.get_raw_line()
45if line.len >= 0 {
46r.current = line.runes()
47}
48r.previous_lines[0] = []rune{}
49r.search_index = 0
50if r.current.len == 0 {
51return error('empty line')
52}
53return 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`).
58pub fn (mut r Readline) read_line(prompt string) !string {
59s := r.read_line_utf8(prompt)!
60return 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).
72pub fn read_line_utf8(prompt string) ![]rune {
73mut r := Readline{}
74s := r.read_line_utf8(prompt)!
75return 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).
82pub fn read_line(prompt string) !string {
83mut r := Readline{}
84s := r.read_line(prompt)!
85return s
86}
87