ASCIISequences holds mappings of the key and byte array.
var ASCIISequences = []*ASCIICode{ {Key: Escape, ASCIICode: []byte{0x1b}}, {Key: ControlSpace, ASCIICode: []byte{0x00}}, {Key: ControlA, ASCIICode: []byte{0x1}}, {Key: ControlB, ASCIICode: []byte{0x2}}, {Key: ControlC, ASCIICode: []byte{0x3}}, {Key: ControlD, ASCIICode: []byte{0x4}}, {Key: ControlE, ASCIICode: []byte{0x5}}, {Key: ControlF, ASCIICode: []byte{0x6}}, {Key: ControlG, ASCIICode: []byte{0x7}}, {Key: ControlH, ASCIICode: []byte{0x8}}, {Key: ControlK, ASCIICode: []byte{0xb}}, {Key: ControlL, ASCIICode: []byte{0xc}}, {Key: ControlM, ASCIICode: []byte{0xd}}, {Key: ControlN, ASCIICode: []byte{0xe}}, {Key: ControlO, ASCIICode: []byte{0xf}}, {Key: ControlP, ASCIICode: []byte{0x10}}, {Key: ControlQ, ASCIICode: []byte{0x11}}, {Key: ControlR, ASCIICode: []byte{0x12}}, {Key: ControlS, ASCIICode: []byte{0x13}}, {Key: ControlT, ASCIICode: []byte{0x14}}, {Key: ControlU, ASCIICode: []byte{0x15}}, {Key: ControlV, ASCIICode: []byte{0x16}}, {Key: ControlW, ASCIICode: []byte{0x17}}, {Key: ControlX, ASCIICode: []byte{0x18}}, {Key: ControlY, ASCIICode: []byte{0x19}}, {Key: ControlZ, ASCIICode: []byte{0x1a}}, {Key: ControlBackslash, ASCIICode: []byte{0x1c}}, {Key: ControlSquareClose, ASCIICode: []byte{0x1d}}, {Key: ControlCircumflex, ASCIICode: []byte{0x1e}}, {Key: ControlUnderscore, ASCIICode: []byte{0x1f}}, {Key: Backspace, ASCIICode: []byte{0x7f}}, {Key: Up, ASCIICode: []byte{0x1b, 0x5b, 0x41}}, {Key: Down, ASCIICode: []byte{0x1b, 0x5b, 0x42}}, {Key: Right, ASCIICode: []byte{0x1b, 0x5b, 0x43}}, {Key: Left, ASCIICode: []byte{0x1b, 0x5b, 0x44}}, {Key: Home, ASCIICode: []byte{0x1b, 0x5b, 0x48}}, {Key: Home, ASCIICode: []byte{0x1b, 0x30, 0x48}}, {Key: End, ASCIICode: []byte{0x1b, 0x5b, 0x46}}, {Key: End, ASCIICode: []byte{0x1b, 0x30, 0x46}}, {Key: Enter, ASCIICode: []byte{0xa}}, {Key: Delete, ASCIICode: []byte{0x1b, 0x5b, 0x33, 0x7e}}, {Key: ShiftDelete, ASCIICode: []byte{0x1b, 0x5b, 0x33, 0x3b, 0x32, 0x7e}}, {Key: ControlDelete, ASCIICode: []byte{0x1b, 0x5b, 0x33, 0x3b, 0x35, 0x7e}}, {Key: Home, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x7e}}, {Key: End, ASCIICode: []byte{0x1b, 0x5b, 0x34, 0x7e}}, {Key: PageUp, ASCIICode: []byte{0x1b, 0x5b, 0x35, 0x7e}}, {Key: PageDown, ASCIICode: []byte{0x1b, 0x5b, 0x36, 0x7e}}, {Key: Home, ASCIICode: []byte{0x1b, 0x5b, 0x37, 0x7e}}, {Key: End, ASCIICode: []byte{0x1b, 0x5b, 0x38, 0x7e}}, {Key: Tab, ASCIICode: []byte{0x9}}, {Key: BackTab, ASCIICode: []byte{0x1b, 0x5b, 0x5a}}, {Key: Insert, ASCIICode: []byte{0x1b, 0x5b, 0x32, 0x7e}}, {Key: F1, ASCIICode: []byte{0x1b, 0x4f, 0x50}}, {Key: F2, ASCIICode: []byte{0x1b, 0x4f, 0x51}}, {Key: F3, ASCIICode: []byte{0x1b, 0x4f, 0x52}}, {Key: F4, ASCIICode: []byte{0x1b, 0x4f, 0x53}}, {Key: F1, ASCIICode: []byte{0x1b, 0x4f, 0x50, 0x41}}, {Key: F2, ASCIICode: []byte{0x1b, 0x5b, 0x5b, 0x42}}, {Key: F3, ASCIICode: []byte{0x1b, 0x5b, 0x5b, 0x43}}, {Key: F4, ASCIICode: []byte{0x1b, 0x5b, 0x5b, 0x44}}, {Key: F5, ASCIICode: []byte{0x1b, 0x5b, 0x5b, 0x45}}, {Key: F1, ASCIICode: []byte{0x1b, 0x5b, 0x11, 0x7e}}, {Key: F2, ASCIICode: []byte{0x1b, 0x5b, 0x12, 0x7e}}, {Key: F3, ASCIICode: []byte{0x1b, 0x5b, 0x13, 0x7e}}, {Key: F4, ASCIICode: []byte{0x1b, 0x5b, 0x14, 0x7e}}, {Key: F5, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x35, 0x7e}}, {Key: F6, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x37, 0x7e}}, {Key: F7, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x38, 0x7e}}, {Key: F8, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x39, 0x7e}}, {Key: F9, ASCIICode: []byte{0x1b, 0x5b, 0x32, 0x30, 0x7e}}, {Key: F10, ASCIICode: []byte{0x1b, 0x5b, 0x32, 0x31, 0x7e}}, {Key: F11, ASCIICode: []byte{0x1b, 0x5b, 0x32, 0x32, 0x7e}}, {Key: F12, ASCIICode: []byte{0x1b, 0x5b, 0x32, 0x34, 0x7e, 0x8}}, {Key: F13, ASCIICode: []byte{0x1b, 0x5b, 0x25, 0x7e}}, {Key: F14, ASCIICode: []byte{0x1b, 0x5b, 0x26, 0x7e}}, {Key: F15, ASCIICode: []byte{0x1b, 0x5b, 0x28, 0x7e}}, {Key: F16, ASCIICode: []byte{0x1b, 0x5b, 0x29, 0x7e}}, {Key: F17, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x7e}}, {Key: F18, ASCIICode: []byte{0x1b, 0x5b, 0x32, 0x7e}}, {Key: F19, ASCIICode: []byte{0x1b, 0x5b, 0x33, 0x7e}}, {Key: F20, ASCIICode: []byte{0x1b, 0x5b, 0x34, 0x7e}}, {Key: F13, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x50}}, {Key: F14, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x51}}, {Key: F16, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x52}}, {Key: F17, ASCIICode: []byte{0x1b, 0x5b, 0x15, 0x3b, 0x32, 0x7e}}, {Key: F18, ASCIICode: []byte{0x1b, 0x5b, 0x17, 0x3b, 0x32, 0x7e}}, {Key: F19, ASCIICode: []byte{0x1b, 0x5b, 0x18, 0x3b, 0x32, 0x7e}}, {Key: F20, ASCIICode: []byte{0x1b, 0x5b, 0x19, 0x3b, 0x32, 0x7e}}, {Key: F21, ASCIICode: []byte{0x1b, 0x5b, 0x20, 0x3b, 0x32, 0x7e}}, {Key: F22, ASCIICode: []byte{0x1b, 0x5b, 0x21, 0x3b, 0x32, 0x7e}}, {Key: F23, ASCIICode: []byte{0x1b, 0x5b, 0x23, 0x3b, 0x32, 0x7e}}, {Key: F24, ASCIICode: []byte{0x1b, 0x5b, 0x24, 0x3b, 0x32, 0x7e}}, {Key: ControlUp, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x35, 0x41}}, {Key: ControlDown, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x35, 0x42}}, {Key: ControlRight, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x35, 0x43}}, {Key: ControlLeft, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x35, 0x44}}, {Key: ShiftUp, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x41}}, {Key: ShiftDown, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x42}}, {Key: ShiftRight, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x43}}, {Key: ShiftLeft, ASCIICode: []byte{0x1b, 0x5b, 0x31, 0x3b, 0x32, 0x44}}, {Key: Up, ASCIICode: []byte{0x1b, 0x4f, 0x41}}, {Key: Down, ASCIICode: []byte{0x1b, 0x4f, 0x42}}, {Key: Right, ASCIICode: []byte{0x1b, 0x4f, 0x43}}, {Key: Left, ASCIICode: []byte{0x1b, 0x4f, 0x44}}, {Key: ControlUp, ASCIICode: []byte{0x1b, 0x5b, 0x35, 0x41}}, {Key: ControlDown, ASCIICode: []byte{0x1b, 0x5b, 0x35, 0x42}}, {Key: ControlRight, ASCIICode: []byte{0x1b, 0x5b, 0x35, 0x43}}, {Key: ControlLeft, ASCIICode: []byte{0x1b, 0x5b, 0x35, 0x44}}, {Key: ControlRight, ASCIICode: []byte{0x1b, 0x5b, 0x4f, 0x63}}, {Key: ControlLeft, ASCIICode: []byte{0x1b, 0x5b, 0x4f, 0x64}}, {Key: Ignore, ASCIICode: []byte{0x1b, 0x5b, 0x45}}, {Key: Ignore, ASCIICode: []byte{0x1b, 0x5b, 0x46}}, }
var ( // NewStandardOutputWriter returns ConsoleWriter object to write to stdout. // This generates VT100 escape sequences because almost terminal emulators // in POSIX OS built on top of a VT100 specification. // Deprecated: Please use NewStdoutWriter NewStandardOutputWriter = NewStdoutWriter )
SwitchKeyBindMode to set a key bind mode. Deprecated: Please use OptionSwitchKeyBindMode.
var SwitchKeyBindMode = OptionSwitchKeyBindMode
func Choose(prefix string, choices []string, opts ...Option) string
Choose to the shortcut of input function to select from string array. Deprecated: Maybe anyone want to use this.
func DeleteBeforeChar(buf *Buffer)
DeleteBeforeChar Go to Backspace
func DeleteChar(buf *Buffer)
DeleteChar Delete character under the cursor
func DeleteWord(buf *Buffer)
DeleteWord Delete word before the cursor
func GoLeftChar(buf *Buffer)
GoLeftChar Backward one character
func GoLeftWord(buf *Buffer)
GoLeftWord Backward one word
func GoLineBeginning(buf *Buffer)
GoLineBeginning Go to the beginning of the line
func GoLineEnd(buf *Buffer)
GoLineEnd Go to the End of the line
func GoRightChar(buf *Buffer)
GoRightChar Forward one character
func GoRightWord(buf *Buffer)
GoRightWord Forward one word
func Input(prefix string, completer Completer, opts ...Option) string
Input get the input data from the user and return it.
ASCIICode is the type contains Key and it's ascii byte array.
type ASCIICode struct { Key Key ASCIICode []byte }
ASCIICodeBind represents which []byte should do what operation
type ASCIICodeBind struct { ASCIICode []byte Fn KeyBindFunc }
Buffer emulates the console buffer.
type Buffer struct {
// contains filtered or unexported fields
}
func NewBuffer() (b *Buffer)
NewBuffer is constructor of Buffer struct.
func (b *Buffer) CursorDown(count int)
CursorDown move cursor to the next line. (for multi-line edit).
func (b *Buffer) CursorLeft(count int)
CursorLeft move to left on the current line.
func (b *Buffer) CursorRight(count int)
CursorRight move to right on the current line.
func (b *Buffer) CursorUp(count int)
CursorUp move cursor to the previous line. (for multi-line edit).
func (b *Buffer) Delete(count int) (deleted string)
Delete specified number of characters and Return the deleted text.
func (b *Buffer) DeleteBeforeCursor(count int) (deleted string)
DeleteBeforeCursor delete specified number of characters before cursor and return the deleted text.
func (b *Buffer) DisplayCursorPosition() int
DisplayCursorPosition returns the cursor position on rendered text on terminal emulators. So if Document is "日本(cursor)語", DisplayedCursorPosition returns 4 because '日' and '本' are double width characters.
func (b *Buffer) Document() (d *Document)
Document method to return document instance from the current text and cursor position.
func (b *Buffer) InsertText(v string, overwrite bool, moveCursor bool)
InsertText insert string from current line.
func (b *Buffer) JoinNextLine(separator string)
JoinNextLine joins the next line to the current one by deleting the line ending after the current line.
func (b *Buffer) NewLine(copyMargin bool)
NewLine means CR.
func (b *Buffer) SwapCharactersBeforeCursor()
SwapCharactersBeforeCursor swaps the last two characters before the cursor.
func (b *Buffer) Text() string
Text returns string of the current line.
Color represents color on terminal.
type Color int
const ( // DefaultColor represents a default color. DefaultColor Color = iota // Black represents a black. Black // DarkRed represents a dark red. DarkRed // DarkGreen represents a dark green. DarkGreen // Brown represents a brown. Brown // DarkBlue represents a dark blue. DarkBlue // Purple represents a purple. Purple // Cyan represents a cyan. Cyan // LightGray represents a light gray. LightGray // DarkGray represents a dark gray. DarkGray // Red represents a red. Red // Green represents a green. Green // Yellow represents a yellow. Yellow // Blue represents a blue. Blue // Fuchsia represents a fuchsia. Fuchsia // Turquoise represents a turquoise. Turquoise // White represents a white. White )
Completer should return the suggest item from Document.
type Completer func(Document) []Suggest
CompletionManager manages which suggestion is now selected.
type CompletionManager struct {
// contains filtered or unexported fields
}
func NewCompletionManager(completer Completer, max uint16) *CompletionManager
NewCompletionManager returns initialized CompletionManager object.
func (c *CompletionManager) Completing() bool
Completing returns whether the CompletionManager selects something one.
func (c *CompletionManager) GetSelectedSuggestion() (s Suggest, ok bool)
GetSelectedSuggestion returns the selected item.
func (c *CompletionManager) GetSuggestions() []Suggest
GetSuggestions returns the list of suggestion.
func (c *CompletionManager) Next()
Next to select the next suggestion item.
func (c *CompletionManager) Previous()
Previous to select the previous suggestion item.
func (c *CompletionManager) Reset()
Reset to select nothing.
func (c *CompletionManager) Update(in Document)
Update to update the suggestions.
ConsoleParser is an interface to abstract input layer.
type ConsoleParser interface { // Setup should be called before starting input Setup() error // TearDown should be called after stopping input TearDown() error // GetWinSize returns WinSize object to represent width and height of terminal. GetWinSize() *WinSize // Read returns byte array. Read() ([]byte, error) }
ConsoleWriter is an interface to abstract output layer.
type ConsoleWriter interface { // WriteRaw to write raw byte array. WriteRaw(data []byte) // Write to write safety byte array by removing control sequences. Write(data []byte) // WriteStr to write raw string. WriteRawStr(data string) // WriteStr to write safety string by removing control sequences. WriteStr(data string) // Flush to flush buffer. Flush() error // EraseScreen erases the screen with the background colour and moves the cursor to home. EraseScreen() // EraseUp erases the screen from the current line up to the top of the screen. EraseUp() // EraseDown erases the screen from the current line down to the bottom of the screen. EraseDown() // EraseStartOfLine erases from the current cursor position to the start of the current line. EraseStartOfLine() // EraseEndOfLine erases from the current cursor position to the end of the current line. EraseEndOfLine() // EraseLine erases the entire current line. EraseLine() // ShowCursor stops blinking cursor and show. ShowCursor() // HideCursor hides cursor. HideCursor() // CursorGoTo sets the cursor position where subsequent text will begin. CursorGoTo(row, col int) // CursorUp moves the cursor up by 'n' rows; the default count is 1. CursorUp(n int) // CursorDown moves the cursor down by 'n' rows; the default count is 1. CursorDown(n int) // CursorForward moves the cursor forward by 'n' columns; the default count is 1. CursorForward(n int) // CursorBackward moves the cursor backward by 'n' columns; the default count is 1. CursorBackward(n int) // AskForCPR asks for a cursor position report (CPR). AskForCPR() // SaveCursor saves current cursor position. SaveCursor() // UnSaveCursor restores cursor position after a Save Cursor. UnSaveCursor() // ScrollDown scrolls display down one line. ScrollDown() // ScrollUp scroll display up one line. ScrollUp() // SetTitle sets a title of terminal window. SetTitle(title string) // ClearTitle clears a title of terminal window. ClearTitle() // SetColor sets text and background colors. and specify whether text is bold. SetColor(fg, bg Color, bold bool) }
func NewStderrWriter() ConsoleWriter
NewStderrWriter returns ConsoleWriter object to write to stderr. This generates VT100 escape sequences because almost terminal emulators in POSIX OS built on top of a VT100 specification.
func NewStdoutWriter() ConsoleWriter
NewStdoutWriter returns ConsoleWriter object to write to stdout. This generates VT100 escape sequences because almost terminal emulators in POSIX OS built on top of a VT100 specification.
DisplayAttribute represents display attributes like Blinking, Bold, Italic and so on.
type DisplayAttribute int
const ( // DisplayReset reset all display attributes. DisplayReset DisplayAttribute = iota // DisplayBold set bold or increases intensity. DisplayBold // DisplayLowIntensity decreases intensity. Not widely supported. DisplayLowIntensity // DisplayItalic set italic. Not widely supported. DisplayItalic // DisplayUnderline set underline DisplayUnderline // DisplayBlink set blink (less than 150 per minute). DisplayBlink // DisplayRapidBlink set blink (more than 150 per minute). Not widely supported. DisplayRapidBlink // DisplayReverse swap foreground and background colors. DisplayReverse // DisplayInvisible set invisible. Not widely supported. DisplayInvisible // DisplayCrossedOut set characters legible, but marked for deletion. Not widely supported. DisplayCrossedOut // DisplayDefaultFont set primary(default) font DisplayDefaultFont )
Document has text displayed in terminal and cursor position.
type Document struct { Text string // contains filtered or unexported fields }
func NewDocument() *Document
NewDocument return the new empty document.
func (d *Document) CurrentLine() string
CurrentLine return the text on the line where the cursor is. (when the input consists of just one line, it equals `text`.
▹ Example
func (d *Document) CurrentLineAfterCursor() string
CurrentLineAfterCursor returns the text from the cursor until the end of the line.
▹ Example
func (d *Document) CurrentLineBeforeCursor() string
CurrentLineBeforeCursor returns the text from the start of the line until the cursor.
▹ Example
func (d *Document) CursorPositionCol() (col int)
CursorPositionCol returns the current column. (0-based.)
▹ Example
func (d *Document) CursorPositionRow() (row int)
CursorPositionRow returns the current row. (0-based.)
▹ Example
func (d *Document) DisplayCursorPosition() int
DisplayCursorPosition returns the cursor position on rendered text on terminal emulators. So if Document is "日本(cursor)語", DisplayedCursorPosition returns 4 because '日' and '本' are double width characters.
▹ Example
▹ Example (WithJapanese)
func (d *Document) FindEndOfCurrentWord() int
FindEndOfCurrentWord returns an index relative to the cursor position. pointing to the end of the current word. Return 0 if nothing was found.
func (d *Document) FindEndOfCurrentWordUntilSeparator(sep string) int
FindEndOfCurrentWordUntilSeparator is almost the same as FindEndOfCurrentWord. But this can specify Separator. Return 0 if nothing was found.
func (d *Document) FindEndOfCurrentWordUntilSeparatorIgnoreNextToCursor(sep string) int
FindEndOfCurrentWordUntilSeparatorIgnoreNextToCursor is almost the same as FindEndOfCurrentWordWithSpace. But this can specify Separator. Return 0 if nothing was found.
func (d *Document) FindEndOfCurrentWordWithSpace() int
FindEndOfCurrentWordWithSpace is almost the same as FindEndOfCurrentWord. The only difference is to ignore contiguous spaces.
func (d *Document) FindStartOfPreviousWord() int
FindStartOfPreviousWord returns an index relative to the cursor position pointing to the start of the previous word. Return 0 if nothing was found.
func (d *Document) FindStartOfPreviousWordUntilSeparator(sep string) int
FindStartOfPreviousWordUntilSeparator is almost the same as FindStartOfPreviousWord. But this can specify Separator. Return 0 if nothing was found.
func (d *Document) FindStartOfPreviousWordUntilSeparatorIgnoreNextToCursor(sep string) int
FindStartOfPreviousWordUntilSeparatorIgnoreNextToCursor is almost the same as FindStartOfPreviousWordWithSpace. But this can specify Separator. Return 0 if nothing was found.
func (d *Document) FindStartOfPreviousWordWithSpace() int
FindStartOfPreviousWordWithSpace is almost the same as FindStartOfPreviousWord. The only difference is to ignore contiguous spaces.
func (d *Document) GetCharRelativeToCursor(offset int) (r rune)
GetCharRelativeToCursor return character relative to cursor position, or empty string
func (d *Document) GetCursorDownPosition(count int, preferredColumn int) int
GetCursorDownPosition return the relative cursor position (character index) where we would be if the user pressed the arrow-down button.
func (d *Document) GetCursorLeftPosition(count int) int
GetCursorLeftPosition returns the relative position for cursor left.
func (d *Document) GetCursorRightPosition(count int) int
GetCursorRightPosition returns relative position for cursor right.
func (d *Document) GetCursorUpPosition(count int, preferredColumn int) int
GetCursorUpPosition return the relative cursor position (character index) where we would be if the user pressed the arrow-up button.
func (d *Document) GetEndOfLinePosition() int
GetEndOfLinePosition returns relative position for the end of this line.
func (d *Document) GetWordAfterCursor() string
GetWordAfterCursor returns the word after the cursor. If we have whitespace after the cursor this returns an empty string.
▹ Example
func (d *Document) GetWordAfterCursorUntilSeparator(sep string) string
GetWordAfterCursorUntilSeparator returns the text after the cursor until next separator.
▹ Example
func (d *Document) GetWordAfterCursorUntilSeparatorIgnoreNextToCursor(sep string) string
GetWordAfterCursorUntilSeparatorIgnoreNextToCursor returns the word after the cursor. Unlike GetWordAfterCursor, it returns string containing space
▹ Example
func (d *Document) GetWordAfterCursorWithSpace() string
GetWordAfterCursorWithSpace returns the word after the cursor. Unlike GetWordAfterCursor, it returns string containing space
▹ Example
func (d *Document) GetWordBeforeCursor() string
GetWordBeforeCursor returns the word before the cursor. If we have whitespace before the cursor this returns an empty string.
▹ Example
func (d *Document) GetWordBeforeCursorUntilSeparator(sep string) string
GetWordBeforeCursorUntilSeparator returns the text before the cursor until next separator.
▹ Example
func (d *Document) GetWordBeforeCursorUntilSeparatorIgnoreNextToCursor(sep string) string
GetWordBeforeCursorUntilSeparatorIgnoreNextToCursor returns the word before the cursor. Unlike GetWordBeforeCursor, it returns string containing space
▹ Example
func (d *Document) GetWordBeforeCursorWithSpace() string
GetWordBeforeCursorWithSpace returns the word before the cursor. Unlike GetWordBeforeCursor, it returns string containing space
▹ Example
func (d *Document) LastKeyStroke() Key
LastKeyStroke return the last key pressed in this document.
func (d *Document) LineCount() int
LineCount return the number of lines in this document. If the document ends with a trailing \n, that counts as the beginning of a new line.
func (d *Document) Lines() []string
Lines returns the array of all the lines.
func (d *Document) OnLastLine() bool
OnLastLine returns true when we are at the last line.
func (d *Document) TextAfterCursor() string
TextAfterCursor returns the text after the cursor.
▹ Example
func (d *Document) TextBeforeCursor() string
TextBeforeCursor returns the text before the cursor.
▹ Example
func (d *Document) TranslateIndexToPosition(index int) (row int, col int)
TranslateIndexToPosition given an index for the text, return the corresponding (row, col) tuple. (0-based. Returns (0, 0) for index=0.)
func (d *Document) TranslateRowColToIndex(row int, column int) (index int)
TranslateRowColToIndex given a (row, col), return the corresponding index. (Row and col params are 0-based.)
Exec is the struct contains user input context.
type Exec struct {
// contains filtered or unexported fields
}
Executor is called when user input something text.
type Executor func(string)
ExitChecker is called after user input to check if prompt must stop and exit go-prompt Run loop. User input means: selecting/typing an entry, then, if said entry content matches the ExitChecker function criteria: - immediate exit (if breakline is false) without executor called - exit after typing <return> (meaning breakline is true), and the executor is called first, before exit. Exit means exit go-prompt (not the overall Go program)
type ExitChecker func(in string, breakline bool) bool
Filter is the type to filter the prompt.Suggestion array.
type Filter func([]Suggest, string, bool) []Suggest
History stores the texts that are entered.
type History struct {
// contains filtered or unexported fields
}
func NewHistory() *History
NewHistory returns new history object.
func (h *History) Add(input string)
Add to add text in history.
func (h *History) Clear()
Clear to clear the history.
func (h *History) Newer(buf *Buffer) (new *Buffer, changed bool)
Newer saves a buffer of current line and get a buffer of next line by up-arrow. The changes of line buffers are stored until new history is created.
func (h *History) Older(buf *Buffer) (new *Buffer, changed bool)
Older saves a buffer of current line and get a buffer of previous line by up-arrow. The changes of line buffers are stored until new history is created.
Key is the type express the key inserted from user.
type Key int
const ( Escape Key = iota ControlA ControlB ControlC ControlD ControlE ControlF ControlG ControlH ControlI ControlJ ControlK ControlL ControlM ControlN ControlO ControlP ControlQ ControlR ControlS ControlT ControlU ControlV ControlW ControlX ControlY ControlZ ControlSpace ControlBackslash ControlSquareClose ControlCircumflex ControlUnderscore ControlLeft ControlRight ControlUp ControlDown Up Down Right Left ShiftLeft ShiftUp ShiftDown ShiftRight Home End Delete ShiftDelete ControlDelete PageUp PageDown BackTab Insert Backspace // Aliases. Tab Enter F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 F13 F14 F15 F16 F17 F18 F19 F20 F21 F22 F23 F24 // Matches any key. Any // Special CPRResponse Vt100MouseEvent WindowsMouseEvent BracketedPaste // Key which is ignored. (The key binding for this key should not do anything.) Ignore // Key is not defined NotDefined )
func GetKey(b []byte) Key
GetKey returns Key correspond to input byte codes.
func (i Key) String() string
KeyBind represents which key should do what operation.
type KeyBind struct { Key Key Fn KeyBindFunc }
KeyBindFunc receives buffer and processed it.
type KeyBindFunc func(*Buffer)
KeyBindMode to switch a key binding flexibly.
type KeyBindMode string
const ( // CommonKeyBind is a mode without any keyboard shortcut CommonKeyBind KeyBindMode = "common" // EmacsKeyBind is a mode to use emacs-like keyboard shortcut EmacsKeyBind KeyBindMode = "emacs" )
Option is the type to replace default parameters. prompt.New accepts any number of options (this is functional option pattern).
type Option func(prompt *Prompt) error
func OptionAddASCIICodeBind(b ...ASCIICodeBind) Option
OptionAddASCIICodeBind to set a custom key bind.
func OptionAddKeyBind(b ...KeyBind) Option
OptionAddKeyBind to set a custom key bind.
func OptionBreakLineCallback(fn func(*Document)) Option
OptionBreakLineCallback to run a callback at every break line
func OptionCompletionOnDown() Option
OptionCompletionOnDown allows for Down arrow key to trigger completion.
func OptionCompletionWordSeparator(x string) Option
OptionCompletionWordSeparator to set word separators. Enable only ' ' if empty.
func OptionDescriptionBGColor(x Color) Option
OptionDescriptionBGColor to change a background color of description text in drop down suggestions.
func OptionDescriptionTextColor(x Color) Option
OptionDescriptionTextColor to change a background color of description text in drop down suggestions.
func OptionHistory(x []string) Option
OptionHistory to set history expressed by string array.
func OptionInitialBufferText(x string) Option
OptionInitialBufferText to set the initial buffer text
func OptionInputBGColor(x Color) Option
OptionInputBGColor to change a color of background which is input by user
func OptionInputTextColor(x Color) Option
OptionInputTextColor to change a color of text which is input by user
func OptionLivePrefix(f func() (prefix string, useLivePrefix bool)) Option
OptionLivePrefix to change the prefix dynamically by callback function
func OptionMaxSuggestion(x uint16) Option
OptionMaxSuggestion specify the max number of displayed suggestions.
func OptionParser(x ConsoleParser) Option
OptionParser to set a custom ConsoleParser object. An argument should implement ConsoleParser interface.
func OptionPrefix(x string) Option
OptionPrefix to set prefix string.
func OptionPrefixBackgroundColor(x Color) Option
OptionPrefixBackgroundColor to change a background color of prefix string
func OptionPrefixTextColor(x Color) Option
OptionPrefixTextColor change a text color of prefix string
func OptionPreviewSuggestionBGColor(x Color) Option
OptionPreviewSuggestionBGColor to change a background color which is completed
func OptionPreviewSuggestionTextColor(x Color) Option
OptionPreviewSuggestionTextColor to change a text color which is completed
func OptionScrollbarBGColor(x Color) Option
OptionScrollbarBGColor to change a background color of scrollbar.
func OptionScrollbarThumbColor(x Color) Option
OptionScrollbarThumbColor to change a thumb color on scrollbar.
func OptionSelectedDescriptionBGColor(x Color) Option
OptionSelectedDescriptionBGColor to change a background color of description which is selected inside suggestions drop down box.
func OptionSelectedDescriptionTextColor(x Color) Option
OptionSelectedDescriptionTextColor to change a text color of description which is selected inside suggestions drop down box.
func OptionSelectedSuggestionBGColor(x Color) Option
OptionSelectedSuggestionBGColor to change a background color for completed text which is selected inside suggestions drop down box.
func OptionSelectedSuggestionTextColor(x Color) Option
OptionSelectedSuggestionTextColor to change a text color for completed text which is selected inside suggestions drop down box.
func OptionSetExitCheckerOnInput(fn ExitChecker) Option
OptionSetExitCheckerOnInput set an exit function which checks if go-prompt exits its Run loop
func OptionShowCompletionAtStart() Option
OptionShowCompletionAtStart to set completion window is open at start.
func OptionSuggestionBGColor(x Color) Option
OptionSuggestionBGColor change a background color in drop down suggestions.
func OptionSuggestionTextColor(x Color) Option
OptionSuggestionTextColor to change a text color in drop down suggestions.
func OptionSwitchKeyBindMode(m KeyBindMode) Option
OptionSwitchKeyBindMode set a key bind mode.
func OptionTitle(x string) Option
OptionTitle to set title displayed at the header bar of terminal.
func OptionWriter(x ConsoleWriter) Option
OptionWriter to set a custom ConsoleWriter object. An argument should implement ConsoleWriter interface.
PosixParser is a ConsoleParser implementation for POSIX environment.
type PosixParser struct {
// contains filtered or unexported fields
}
func NewStandardInputParser() *PosixParser
NewStandardInputParser returns ConsoleParser object to read from stdin.
func (t *PosixParser) GetWinSize() *WinSize
GetWinSize returns WinSize object to represent width and height of terminal.
func (t *PosixParser) Read() ([]byte, error)
Read returns byte array.
func (t *PosixParser) Setup() error
Setup should be called before starting input
func (t *PosixParser) TearDown() error
TearDown should be called after stopping input
PosixWriter is a ConsoleWriter implementation for POSIX environment. To control terminal emulator, this outputs VT100 escape sequences.
type PosixWriter struct { VT100Writer // contains filtered or unexported fields }
func (w *PosixWriter) Flush() error
Flush to flush buffer
Prompt is core struct of go-prompt.
type Prompt struct { ASCIICodeBindings []ASCIICodeBind // contains filtered or unexported fields }
func New(executor Executor, completer Completer, opts ...Option) *Prompt
New returns a Prompt with powerful auto-completion.
func (p *Prompt) Input() string
Input just returns user input text.
func (p *Prompt) Run()
Run starts prompt.
Render to render prompt information from state of Buffer.
type Render struct {
// contains filtered or unexported fields
}
func (r *Render) BreakLine(buffer *Buffer)
BreakLine to break line.
func (r *Render) Render(buffer *Buffer, completion *CompletionManager)
Render renders to the console.
func (r *Render) Setup()
Setup to initialize console output.
func (r *Render) TearDown()
TearDown to clear title and erasing.
func (r *Render) UpdateWinSize(ws *WinSize)
UpdateWinSize called when window size is changed.
Suggest is printed when completing.
type Suggest struct { Text string Description string }
func FilterContains(completions []Suggest, sub string, ignoreCase bool) []Suggest
FilterContains checks whether the completion.Text contains sub.
func FilterFuzzy(completions []Suggest, sub string, ignoreCase bool) []Suggest
FilterFuzzy checks whether the completion.Text fuzzy matches sub. Fuzzy searching for "dog" is equivalent to "*d*o*g*". This search term would match, for example, "Good food is gone"
^ ^ ^
func FilterHasPrefix(completions []Suggest, sub string, ignoreCase bool) []Suggest
FilterHasPrefix checks whether the string completions.Text begins with sub.
func FilterHasSuffix(completions []Suggest, sub string, ignoreCase bool) []Suggest
FilterHasSuffix checks whether the completion.Text ends with sub.
VT100Writer generates VT100 escape sequences.
type VT100Writer struct {
// contains filtered or unexported fields
}
func (w *VT100Writer) AskForCPR()
AskForCPR asks for a cursor position report (CPR).
func (w *VT100Writer) ClearTitle()
ClearTitle clears a title of terminal window.
func (w *VT100Writer) CursorBackward(n int)
CursorBackward moves the cursor backward by 'n' columns; the default count is 1.
func (w *VT100Writer) CursorDown(n int)
CursorDown moves the cursor down by 'n' rows; the default count is 1.
func (w *VT100Writer) CursorForward(n int)
CursorForward moves the cursor forward by 'n' columns; the default count is 1.
func (w *VT100Writer) CursorGoTo(row, col int)
CursorGoTo sets the cursor position where subsequent text will begin.
func (w *VT100Writer) CursorUp(n int)
CursorUp moves the cursor up by 'n' rows; the default count is 1.
func (w *VT100Writer) EraseDown()
EraseDown erases the screen from the current line down to the bottom of the screen.
func (w *VT100Writer) EraseEndOfLine()
EraseEndOfLine erases from the current cursor position to the end of the current line.
func (w *VT100Writer) EraseLine()
EraseLine erases the entire current line.
func (w *VT100Writer) EraseScreen()
EraseScreen erases the screen with the background colour and moves the cursor to home.
func (w *VT100Writer) EraseStartOfLine()
EraseStartOfLine erases from the current cursor position to the start of the current line.
func (w *VT100Writer) EraseUp()
EraseUp erases the screen from the current line up to the top of the screen.
func (w *VT100Writer) HideCursor()
HideCursor hides cursor.
func (w *VT100Writer) SaveCursor()
SaveCursor saves current cursor position.
func (w *VT100Writer) ScrollDown()
ScrollDown scrolls display down one line.
func (w *VT100Writer) ScrollUp()
ScrollUp scroll display up one line.
func (w *VT100Writer) SetColor(fg, bg Color, bold bool)
SetColor sets text and background colors. and specify whether text is bold.
func (w *VT100Writer) SetDisplayAttributes(fg, bg Color, attrs ...DisplayAttribute)
SetDisplayAttributes to set VT100 display attributes.
func (w *VT100Writer) SetTitle(title string)
SetTitle sets a title of terminal window.
func (w *VT100Writer) ShowCursor()
ShowCursor stops blinking cursor and show.
func (w *VT100Writer) UnSaveCursor()
UnSaveCursor restores cursor position after a Save Cursor.
func (w *VT100Writer) Write(data []byte)
Write to write safety byte array by removing control sequences.
func (w *VT100Writer) WriteRaw(data []byte)
WriteRaw to write raw byte array
func (w *VT100Writer) WriteRawStr(data string)
WriteRawStr to write raw string
func (w *VT100Writer) WriteStr(data string)
WriteStr to write safety string by removing control sequences.
WinSize represents the width and height of terminal.
type WinSize struct { Row uint16 Col uint16 }