Documentation ¶
Overview ¶
Package readline provides simple functions for both line and screen editing.
Features:
Unicode support History Multi-line editing
List of key sequences enabled (just like in GNU Readline):
Backspace / Ctrl+h Delete Home / Ctrl+a End / Ctrl+e Left arrow / Ctrl+b Right arrow / Ctrl+f Up arrow / Ctrl+p Down arrow / Ctrl+n Ctrl+left arrow Ctrl+right arrow Ctrl+t : swap actual character by the previous one Ctrl+k : delete from current to end of line Ctrl+u : delete the whole line Ctrl+l : clear screen Ctrl+c Ctrl+d : exit
Note that There are several default values:
+ For the buffer: BufferCap, BufferLen.
+ For the history file: HistoryCap, HistoryPerm.
Important: the TTY is set in "raw mode" so there is to use CR+LF ("\r\n") for writing a new line.
Note: the values for the input and output are got from the package base "term".
Index ¶
Constants ¶
const ( // Cursor control ANSI_CURSOR_UP = "\033[A" // Up ANSI_CURSOR_DOWN = "\033[B" // Down ANSI_CURSOR_FORWARD = "\033[C" // Forward ANSI_CURSOR_BACKWARD = "\033[D" // Backward ANSI_NEXT_LINE = "\033[E" // To next line ANSI_PREV_LINE = "\033[F" // To previous line // Erase ANSI_DEL_LINE = "\033[2K" // Erase line // Graphics mode ANSI_SET_BOLD = "\033[1m" // Bold on ANSI_SET_OFF = "\033[0m" // All attributes off )
ANSI terminal escape controls
const ( PS1 = "$ " PS2 = "> " )
Default values for prompts.
Variables ¶
var ( // Cursor control CursorUp = []byte(ANSI_CURSOR_UP) CursorDown = []byte(ANSI_CURSOR_DOWN) CursorForward = []byte(ANSI_CURSOR_FORWARD) CursorBackward = []byte(ANSI_CURSOR_BACKWARD) ToNextLine = []byte(ANSI_NEXT_LINE) ToPreviousLine = []byte(ANSI_PREV_LINE) // Erase Text DelScreenToUpper = []byte("\033[2J\033[0;0H") // Erase the screen; move upper DelToRight = []byte("\033[0K") // Erase to right DelLine_CR = []byte("\033[2K\r") // Erase line; carriage return DelLine_cursorUp = []byte("\033[2K\033[A") // Erase line; cursor up //DelChar = []byte("\033[1X") // Erase character DelChar = []byte("\033[P") // Delete character, from current position DelBackspace = []byte("\033[D\033[P") )
ANSI terminal escape controls
var ( CR = []byte{13} // Carriage return -- \r CRLF = []byte{13, 10} // CR+LF is used for a new line in raw mode -- \r\n CtrlC = []rune("^C") CtrlD = []rune("^D") )
Characters
var ( BufferCap = 4096 BufferLen = 64 // Initial length )
Buffer size
var ( HistoryCap = 500 // Capacity HistoryPerm os.FileMode = 0600 // History file permission )
Values by default
var ( ErrEmptyHist = errors.New("history: empty") ErrNilElement = errors.New("history: no more elements") )
To detect if has been pressed CTRL-C
To detect if has been pressed CTRL-D
Functions ¶
func NewHistory ¶
NewHistory creates a new history using the maximum length by default.
Types ¶
type Line ¶
type Line struct {
// contains filtered or unexported fields
}
A Line represents a line in the term.
func NewDefaultLine ¶
NewDefaultLine returns a line type using the prompt by default, and setting the terminal to raw mode. If the history is nil then it is not used.
func NewLine ¶
NewLine returns a line using both prompts ps1 and ps2, and setting the given terminal to raw mode, if were necessary. lenAnsi is the length of ANSI codes that the prompt ps1 could have. If the history is nil then it is not used.
func (*Line) Read ¶
Read reads charactes from input to write them to output, enabling line editing. The errors that could return are to indicate if Ctrl+D was pressed, and for both input/output errors.