readline #

Readline

The readline module let 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.

Use readline.Readline if you want to include more advanced features such as history or simply use readline.read_line('Please confirm (y/n):') directly for one-off user interactions.

Usage

import readline { Readline }

Readline.read_line('Continue?: (y/n)')

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 ustring 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) ?ustring

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 input line as an UTF-8 encoded ustring 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 {
mut:
	is_raw            bool
	orig_termios      Termios
	current           ustring
	cursor            int
	overwrite         bool
	cursor_row_offset int
	prompt            string
	prompt_offset     int
	previous_lines    []ustring
	search_index      int
	is_tty            bool
}

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 ustring returned by read_line_utf8).

fn (Readline) read_line_utf8 #

fn (mut r Readline) read_line_utf8(prompt string) ?ustring

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 input line as an UTF-8 encoded ustring 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.