readline #
Description
The readline
module lets you await and read user input from a terminal in an easy and structured manner.
The module provides an easy way to prompt the user for questions or even make a REPL or an embedded console.
Usage:
import readline
mut r := readline.Readline{}
answer := r.read_line('hello: ')!
println(answer)
or just:
import readline { read_line }
input := read_line('What is your name: ')!
println('Your name is: ${input}')
fn read_line #
fn read_line(prompt string) !string
read_line does the same as read_line_utf8
but returns user input as a string
. (As opposed to []rune
as returned by read_line_utf8
). NOTE that this version of read_line
is a standalone function without persistent functionalities (e.g. history).
fn read_line_utf8 #
fn read_line_utf8(prompt string) ![]rune
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. read_line_utf8 returns the complete UTF-8 input line as an UTF-32 encoded []rune
or 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).
struct Readline #
struct Readline {
pub mut:
is_raw bool
orig_termios termios.Termios // Linux
current []rune // Line being edited
cursor int // Cursor position
overwrite bool
cursor_row_offset int
prompt string
prompt_offset int
previous_lines [][]rune
skip_empty bool // skip the empty lines when calling .history_previous()
search_index int
is_tty bool
last_prefix_completion []rune
last_completion_offset int
completion_list []string
completion_callback fn (string) []string = unsafe { nil }
}
Readline is the key struct for reading and holding user input via a terminal.
Example
import readline { Readline }
fn (Readline) disable_raw_mode #
fn (mut r Readline) disable_raw_mode()
disable_raw_mode disables the raw mode of the terminal. For a description of raw mode please see the enable_raw_mode
method.
fn (Readline) enable_raw_mode #
fn (mut r Readline) enable_raw_mode()
enable_raw_mode enables the raw mode of the terminal. In raw mode all key presses are directly sent to the program and no interpretation is done. Please note that enable_raw_mode
catches the SIGUSER
(CTRL + C) signal. For a method that does please see enable_raw_mode_nosig
.
fn (Readline) enable_raw_mode_nosig #
fn (mut r Readline) enable_raw_mode_nosig()
enable_raw_mode_nosig enables the raw mode of the terminal. In raw mode all key presses are directly sent to the program and no interpretation is done. Please note that enable_raw_mode_nosig
does not catch the SIGUSER
(CTRL + C) signal as opposed to enable_raw_mode
.
fn (Readline) read_char #
fn (r Readline) read_char() !int
read_char reads a single character.
fn (Readline) read_line #
fn (mut r Readline) read_line(prompt string) !string
read_line does the same as read_line_utf8
but returns user input as a string
. (As opposed to []rune
returned by read_line_utf8
).
fn (Readline) read_line_utf8 #
fn (mut r Readline) read_line_utf8(prompt string) ![]rune
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. read_line_utf8 returns the complete UTF-8 input line as an UTF-32 encoded []rune
or 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.