readline

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2015 License: Apache-2.0 Imports: 12 Imported by: 0

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

View Source
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

View Source
const (
	PS1 = "$ "
	PS2 = "> "
)

Default values for prompts.

Variables

View Source
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

View Source
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

View Source
var (
	BufferCap = 4096
	BufferLen = 64 // Initial length
)

Buffer size

View Source
var (
	HistoryCap              = 500  // Capacity
	HistoryPerm os.FileMode = 0600 // History file permission
)

Values by default

View Source
var (
	ErrEmptyHist  = errors.New("history: empty")
	ErrNilElement = errors.New("history: no more elements")
)
View Source
var ChanCtrlC = make(chan byte)

To detect if has been pressed CTRL-C

View Source
var ChanCtrlD = make(chan byte)

To detect if has been pressed CTRL-D

View Source
var ErrCtrlD = errors.New("Interrumpted (Ctrl+d)")

Functions

func NewHistory

func NewHistory(filename string) (*history, error)

NewHistory creates a new history using the maximum length by default.

func NewHistoryOfSize

func NewHistoryOfSize(filename string, size int) (*history, error)

NewHistoryOfSize creates a new history whose buffer has the specified size, which must be greater than zero.

Types

type Line

type Line struct {
	// contains filtered or unexported fields
}

A Line represents a line in the term.

func NewDefaultLine

func NewDefaultLine(hist *history) (*Line, error)

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

func NewLine(ter *term.Terminal, ps1, ps2 string, lenAnsi int, hist *history) (*Line, error)

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) Prompt

func (ln *Line) Prompt() (err error)

Prompt prints the primary prompt.

func (*Line) Read

func (ln *Line) Read() (line string, err error)

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.

func (*Line) Restore

func (ln *Line) Restore() error

Restore restores the terminal settings, so it is disabled the raw mode.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL