prompt

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2018 License: MIT Imports: 14 Imported by: 0

README ¶

go-prompt

Library for building a powerful interactive prompt, inspired by python-prompt-toolkit. Easy building a multi-platform binary of the command line tools because written in Golang.

package main

import (
	"fmt"
	"github.com/c-bata/go-prompt"
)

func completer(d prompt.Document) []prompt.Suggest {
	s := []prompt.Suggest{
		{Text: "users", Description: "Store the username and age"},
		{Text: "articles", Description: "Store the article text posted by user"},
		{Text: "comments", Description: "Store the text commented to articles"},
	}
	return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
}

func main() {
	fmt.Println("Please select table.")
	t := prompt.Input("> ", completer)
	fmt.Println("You selected " + t)
}
Projects using go-prompt

Features

Powerful auto-completion

demo

(This is a GIF animation of kube-prompt.)

Flexible options

go-prompt provides many options. All options are listed in Developer Guide.

options

Keyboard Shortcuts

Emacs-like keyboard shortcut is available by default (it's also default shortcuts in Bash shell). You can customize and expand these shortcuts.

keyboard shortcuts

KeyBinding Description
Ctrl + A Go to the beginning of the line (Home)
Ctrl + E Go to the End of the line (End)
Ctrl + P Previous command (Up arrow)
Ctrl + N Next command (Down arrow)
Ctrl + F Forward one character
Ctrl + B Backward one character
Ctrl + D Delete character under the cursor
Ctrl + H Delete character before the cursor (Backspace)
Ctrl + W Cut the Word before the cursor to the clipboard.
Ctrl + K Cut the Line after the cursor to the clipboard.
Ctrl + U Cut/delete the Line before the cursor to the clipboard.
History

You can use up-arrow and down-arrow to walk through the history of commands executed.

History

Author

Masashi Shibata

LICENSE

This software is licensed under the MIT License (See LICENSE ).

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

View Source
var SwitchKeyBindMode = OptionSwitchKeyBindMode

SwitchKeyBindMode to set a key bind mode. Deprecated: Please use OptionSwitchKeyBindMode.

Functions ¶

func BisectLeft ¶

func BisectLeft(a []int, v int) int

BisectLeft to Locate the insertion point for v in a to maintain sorted order.

func BisectRight ¶

func BisectRight(a []int, v int) int

BisectRight to Locate the insertion point for v in a to maintain sorted order.

func Choose ¶

func Choose(prefix string, choices []string, opts ...Option) string

Choose to the shortcut of input function to select from string array.

func Input ¶

func Input(prefix string, completer Completer, opts ...Option) string

Input get the input data from the user and return it.

Types ¶

type ASCIICode ¶

type ASCIICode struct {
	Key       Key
	ASCIICode []byte
}

ASCIICode is the type contains Key and it's ascii byte array.

type Buffer ¶

type Buffer struct {
	CursorPosition int
	// contains filtered or unexported fields
}

Buffer emulates the console buffer.

func NewBuffer ¶

func NewBuffer() (b *Buffer)

NewBuffer is constructor of Buffer struct.

func (*Buffer) CursorDown ¶

func (b *Buffer) CursorDown(count int)

CursorDown move cursor to the next line. (for multi-line edit).

func (*Buffer) CursorLeft ¶

func (b *Buffer) CursorLeft(count int)

CursorLeft move to left on the current line.

func (*Buffer) CursorRight ¶

func (b *Buffer) CursorRight(count int)

CursorRight move to right on the current line.

func (*Buffer) CursorUp ¶

func (b *Buffer) CursorUp(count int)

CursorUp move cursor to the previous line. (for multi-line edit).

func (*Buffer) Delete ¶

func (b *Buffer) Delete(count int) (deleted string)

Delete specified number of characters and Return the deleted text.

func (*Buffer) DeleteBeforeCursor ¶

func (b *Buffer) DeleteBeforeCursor(count int) (deleted string)

DeleteBeforeCursor delete specified number of characters before cursor and return the deleted text.

func (*Buffer) Document ¶

func (b *Buffer) Document() (d *Document)

Document method to return document instance from the current text and cursor position.

func (*Buffer) InsertText ¶

func (b *Buffer) InsertText(v string, overwrite bool, moveCursor bool)

InsertText insert string from current line.

func (*Buffer) JoinNextLine ¶

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 (*Buffer) NewLine ¶

func (b *Buffer) NewLine(copyMargin bool)

NewLine means CR.

func (*Buffer) SwapCharactersBeforeCursor ¶

func (b *Buffer) SwapCharactersBeforeCursor()

SwapCharactersBeforeCursor swaps the last two characters before the cursor.

func (*Buffer) Text ¶

func (b *Buffer) Text() string

Text returns string of the current line.

type Color ¶

type Color int
const (
	DefaultColor Color = iota

	// Low intensity
	Black
	DarkRed
	DarkGreen
	Brown
	DarkBlue
	Purple
	Cyan
	LightGray

	// High intensity
	DarkGray
	Red
	Green
	Yellow
	Blue
	Fuchsia
	Turquoise
	White
)

type Completer ¶

type Completer func(Document) []Suggest

Completer should return the suggest item from Document.

type CompletionManager ¶

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

CompletionManager manages which suggestion is now selected.

func NewCompletionManager ¶

func NewCompletionManager(completer Completer, max uint16) *CompletionManager

NewCompletionManager returns initialized CompletionManager object.

func (*CompletionManager) Completing ¶

func (c *CompletionManager) Completing() bool

Completing returns whether the CompletionManager selects something one.

func (*CompletionManager) GetSelectedSuggestion ¶

func (c *CompletionManager) GetSelectedSuggestion() (s Suggest, ok bool)

GetSelectedSuggestion returns the selected item.

func (*CompletionManager) GetSuggestions ¶

func (c *CompletionManager) GetSuggestions() []Suggest

GetSelectedSuggestion returns the list of suggestion.

func (*CompletionManager) Next ¶

func (c *CompletionManager) Next()

Next to select the next suggestion item.

func (*CompletionManager) Previous ¶

func (c *CompletionManager) Previous()

Previous to select the previous suggestion item.

func (*CompletionManager) Reset ¶

func (c *CompletionManager) Reset()

Reset to select nothing.

func (*CompletionManager) Update ¶

func (c *CompletionManager) Update(in Document)

Update to update the suggestions.

type ConsoleParser ¶

type ConsoleParser interface {
	// Setup
	Setup() error
	// TearDown
	TearDown() error
	// GetSCIICode returns ASCIICode correspond to input byte codes.
	GetKey(b []byte) Key
	// GetWinSize returns winsize struct which is the response of ioctl(2).
	GetWinSize() *WinSize
	// Read returns byte array.
	Read() ([]byte, error)
}

type ConsoleWriter ¶

type ConsoleWriter interface {
	WriteRaw(data []byte)
	Write(data []byte)
	WriteStr(data string)
	WriteRawStr(data string)
	Flush() error

	EraseScreen()
	EraseUp()
	EraseDown()
	EraseStartOfLine()
	EraseEndOfLine()
	EraseLine()

	ShowCursor()
	HideCursor()
	CursorGoTo(row, col int)
	CursorUp(n int)
	CursorDown(n int)
	CursorForward(n int)
	CursorBackward(n int)
	AskForCPR()
	SaveCursor()
	UnSaveCursor()

	ScrollDown()
	ScrollUp()

	SetTitle(title string)
	ClearTitle()

	SetColor(fg, bg Color, bold bool)
}

type Document ¶

type Document struct {
	Text           string
	CursorPosition int
}

Document has text displayed in terminal and cursor position.

func NewDocument ¶

func NewDocument() *Document

NewDocument return the new empty document.

func (*Document) CurrentLine ¶

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`.

func (*Document) CurrentLineAfterCursor ¶

func (d *Document) CurrentLineAfterCursor() string

CurrentLineAfterCursor returns the text from the cursor until the end of the line.

func (*Document) CurrentLineBeforeCursor ¶

func (d *Document) CurrentLineBeforeCursor() string

CurrentLineBeforeCursor returns the text from the start of the line until the cursor.

func (*Document) CursorPositionCol ¶

func (d *Document) CursorPositionCol() (col int)

CursorPositionCol returns the current column. (0-based.)

func (*Document) CursorPositionRow ¶

func (d *Document) CursorPositionRow() (row int)

CursorPositionRow returns the current row. (0-based.)

func (*Document) FindStartOfPreviousWord ¶

func (d *Document) FindStartOfPreviousWord() int

FindStartOfPreviousWord returns an index relative to the cursor position pointing to the start of the previous word. Return `None` if nothing was found.

func (*Document) FindStartOfPreviousWordWithSpace ¶ added in v0.1.1

func (d *Document) FindStartOfPreviousWordWithSpace() int

FindStartOfPreviousWordWithSpace is almost the same as FindStartOfPreviousWord. The only difference is to ignore contiguous spaces.

func (*Document) GetCharRelativeToCursor ¶

func (d *Document) GetCharRelativeToCursor(offset int) (r rune)

GetCharRelativeToCursor return character relative to cursor position, or empty string

func (*Document) GetCursorDownPosition ¶

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 (*Document) GetCursorLeftPosition ¶

func (d *Document) GetCursorLeftPosition(count int) int

GetCursorLeftPosition returns the relative position for cursor left.

func (*Document) GetCursorRightPosition ¶

func (d *Document) GetCursorRightPosition(count int) int

GetCursorRightPosition returns relative position for cursor right.

func (*Document) GetCursorUpPosition ¶

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 (*Document) GetEndOfLinePosition ¶

func (d *Document) GetEndOfLinePosition() int

GetEndOfLinePosition returns relative position for the end of this line.

func (*Document) GetWordBeforeCursor ¶

func (d *Document) GetWordBeforeCursor() string

GetWordBeforeCursor returns the word before the cursor. If we have whitespace before the cursor this returns an empty string.

func (*Document) GetWordBeforeCursorWithSpace ¶ added in v0.1.1

func (d *Document) GetWordBeforeCursorWithSpace() string

GetWordBeforeCursorWithSpace returns the word before the cursor. Unlike GetWordBeforeCursor, it returns string containing space

func (*Document) LineCount ¶

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 (*Document) Lines ¶

func (d *Document) Lines() []string

Lines returns the array of all the lines.

func (*Document) OnLastLine ¶

func (d *Document) OnLastLine() bool

OnLastLine returns true when we are at the last line.

func (*Document) TextAfterCursor ¶

func (d *Document) TextAfterCursor() string

TextAfterCursor returns the text after the cursor.

func (*Document) TextBeforeCursor ¶

func (d *Document) TextBeforeCursor() string

TextBeforeCursor returns the text before the cursor.

func (*Document) TranslateIndexToPosition ¶

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 (*Document) TranslateRowColToIndex ¶

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

type Exec ¶

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

Exec is the struct contains user input context.

type Executor ¶

type Executor func(string)

Executor is called when user input something text.

type Filter ¶

type Filter func([]Suggest, string, bool) []Suggest

Filter is the type to filter the prompt.Suggestion array.

type History ¶

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

func NewHistory ¶

func NewHistory() *History

func (*History) Add ¶

func (h *History) Add(input string)

func (*History) Clear ¶

func (h *History) Clear()

func (*History) Newer ¶

func (h *History) Newer(buf *Buffer) (new *Buffer, changed bool)

func (*History) Older ¶

func (h *History) Older(buf *Buffer) (new *Buffer, changed bool)

type Key ¶

type Key int

Key is the type express the key inserted from user.

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 (Key) String ¶

func (i Key) String() string

type KeyBind ¶

type KeyBind struct {
	Key Key
	Fn  KeyBindFunc
}

type KeyBindFunc ¶

type KeyBindFunc func(*Buffer)

type KeyBindMode ¶

type KeyBindMode string
const (
	CommonKeyBind KeyBindMode = "common"
	EmacsKeyBind  KeyBindMode = "emacs"
)

type Option ¶ added in v0.1.1

type Option func(prompt *Prompt) error

Option is the type to replace default parameters. prompt.New accepts any number of options (this is functional option pattern).

func OptionAddKeyBind ¶

func OptionAddKeyBind(b ...KeyBind) Option

OptionAddKeyBind to set a custom key bind.

func OptionDescriptionBGColor ¶

func OptionDescriptionBGColor(x Color) Option

func OptionDescriptionTextColor ¶

func OptionDescriptionTextColor(x Color) Option

func OptionHistory ¶

func OptionHistory(x []string) Option

OptionHistory to set history expressed by string array.

func OptionInputBGColor ¶

func OptionInputBGColor(x Color) Option

func OptionInputTextColor ¶

func OptionInputTextColor(x Color) Option

func OptionLivePrefix ¶ added in v0.2.0

func OptionLivePrefix(f func() (prefix string, useLivePrefix bool)) Option

OptionLivePrefix to change the prefix dynamically by callback function

func OptionMaxSuggestion ¶

func OptionMaxSuggestion(x uint16) Option

OptionMaxSuggestion specify the max number of displayed suggestions.

func OptionParser ¶

func OptionParser(x ConsoleParser) Option

OptionParser to set a custom ConsoleParser object. An argument should implement ConsoleParser interface.

func OptionPrefix ¶

func OptionPrefix(x string) Option

OptionPrefix to set prefix string.

func OptionPrefixBackgroundColor ¶

func OptionPrefixBackgroundColor(x Color) Option

func OptionPrefixTextColor ¶

func OptionPrefixTextColor(x Color) Option

func OptionPreviewSuggestionBGColor ¶

func OptionPreviewSuggestionBGColor(x Color) Option

func OptionPreviewSuggestionTextColor ¶

func OptionPreviewSuggestionTextColor(x Color) Option

func OptionScrollbarBGColor ¶ added in v0.2.0

func OptionScrollbarBGColor(x Color) Option

func OptionScrollbarThumbColor ¶ added in v0.2.0

func OptionScrollbarThumbColor(x Color) Option

func OptionSelectedDescriptionBGColor ¶

func OptionSelectedDescriptionBGColor(x Color) Option

func OptionSelectedDescriptionTextColor ¶

func OptionSelectedDescriptionTextColor(x Color) Option

func OptionSelectedSuggestionBGColor ¶

func OptionSelectedSuggestionBGColor(x Color) Option

func OptionSelectedSuggestionTextColor ¶

func OptionSelectedSuggestionTextColor(x Color) Option

func OptionSuggestionBGColor ¶

func OptionSuggestionBGColor(x Color) Option

func OptionSuggestionTextColor ¶

func OptionSuggestionTextColor(x Color) Option

func OptionSwitchKeyBindMode ¶ added in v0.1.1

func OptionSwitchKeyBindMode(m KeyBindMode) Option

OptionSwitchKeyBindMode set a key bind mode.

func OptionTitle ¶

func OptionTitle(x string) Option

OptionTitle to set title displayed at the header bar of terminal.

func OptionWriter ¶

func OptionWriter(x ConsoleWriter) Option

OptionWriter to set a custom ConsoleWriter object. An argument should implement ConsoleWriter interace.

type PosixParser ¶ added in v0.2.0

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

func NewStandardInputParser ¶ added in v0.2.0

func NewStandardInputParser() *PosixParser

func (*PosixParser) GetKey ¶ added in v0.2.0

func (t *PosixParser) GetKey(b []byte) Key

func (*PosixParser) GetWinSize ¶ added in v0.2.0

func (t *PosixParser) GetWinSize() *WinSize

GetWinSize returns winsize struct which is the response of ioctl(2).

func (*PosixParser) Read ¶ added in v0.2.0

func (t *PosixParser) Read() ([]byte, error)

func (*PosixParser) Setup ¶ added in v0.2.0

func (t *PosixParser) Setup() error

func (*PosixParser) TearDown ¶ added in v0.2.0

func (t *PosixParser) TearDown() error

type PosixWriter ¶ added in v0.2.0

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

func NewStandardOutputWriter ¶ added in v0.2.0

func NewStandardOutputWriter() *PosixWriter

func (*PosixWriter) AskForCPR ¶ added in v0.2.0

func (w *PosixWriter) AskForCPR()

func (*PosixWriter) ClearTitle ¶ added in v0.2.0

func (w *PosixWriter) ClearTitle()

func (*PosixWriter) CursorBackward ¶ added in v0.2.0

func (w *PosixWriter) CursorBackward(n int)

func (*PosixWriter) CursorDown ¶ added in v0.2.0

func (w *PosixWriter) CursorDown(n int)

func (*PosixWriter) CursorForward ¶ added in v0.2.0

func (w *PosixWriter) CursorForward(n int)

func (*PosixWriter) CursorGoTo ¶ added in v0.2.0

func (w *PosixWriter) CursorGoTo(row, col int)

func (*PosixWriter) CursorUp ¶ added in v0.2.0

func (w *PosixWriter) CursorUp(n int)

func (*PosixWriter) EraseDown ¶ added in v0.2.0

func (w *PosixWriter) EraseDown()

func (*PosixWriter) EraseEndOfLine ¶ added in v0.2.0

func (w *PosixWriter) EraseEndOfLine()

func (*PosixWriter) EraseLine ¶ added in v0.2.0

func (w *PosixWriter) EraseLine()

func (*PosixWriter) EraseScreen ¶ added in v0.2.0

func (w *PosixWriter) EraseScreen()

func (*PosixWriter) EraseStartOfLine ¶ added in v0.2.0

func (w *PosixWriter) EraseStartOfLine()

func (*PosixWriter) EraseUp ¶ added in v0.2.0

func (w *PosixWriter) EraseUp()

func (*PosixWriter) Flush ¶ added in v0.2.0

func (w *PosixWriter) Flush() error

func (*PosixWriter) HideCursor ¶ added in v0.2.0

func (w *PosixWriter) HideCursor()

func (*PosixWriter) SaveCursor ¶ added in v0.2.0

func (w *PosixWriter) SaveCursor()

func (*PosixWriter) ScrollDown ¶ added in v0.2.0

func (w *PosixWriter) ScrollDown()

func (*PosixWriter) ScrollUp ¶ added in v0.2.0

func (w *PosixWriter) ScrollUp()

func (*PosixWriter) SetColor ¶ added in v0.2.0

func (w *PosixWriter) SetColor(fg, bg Color, bold bool)

func (*PosixWriter) SetTitle ¶ added in v0.2.0

func (w *PosixWriter) SetTitle(title string)

func (*PosixWriter) ShowCursor ¶ added in v0.2.0

func (w *PosixWriter) ShowCursor()

func (*PosixWriter) UnSaveCursor ¶ added in v0.2.0

func (w *PosixWriter) UnSaveCursor()

func (*PosixWriter) Write ¶ added in v0.2.0

func (w *PosixWriter) Write(data []byte)

func (*PosixWriter) WriteRaw ¶ added in v0.2.0

func (w *PosixWriter) WriteRaw(data []byte)

func (*PosixWriter) WriteRawStr ¶ added in v0.2.0

func (w *PosixWriter) WriteRawStr(data string)

func (*PosixWriter) WriteStr ¶ added in v0.2.0

func (w *PosixWriter) WriteStr(data string)

type Prompt ¶

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

Prompt is core struct of go-prompt.

func New ¶

func New(executor Executor, completer Completer, opts ...Option) *Prompt

New returns a Prompt with powerful auto-completion.

func (*Prompt) Input ¶

func (p *Prompt) Input() string

Input just returns user input text.

func (*Prompt) Run ¶

func (p *Prompt) Run()

Run starts prompt.

type Render ¶

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

Render to render prompt information from state of Buffer.

func (*Render) BreakLine ¶

func (r *Render) BreakLine(buffer *Buffer)

BreakLine to break line.

func (*Render) Render ¶

func (r *Render) Render(buffer *Buffer, completion *CompletionManager)

Render renders to the console.

func (*Render) Setup ¶

func (r *Render) Setup()

Setup to initialize console output.

func (*Render) TearDown ¶

func (r *Render) TearDown()

TearDown to clear title and erasing.

func (*Render) UpdateWinSize ¶

func (r *Render) UpdateWinSize(ws *WinSize)

UpdateWinSize called when window size is changed.

type Suggest ¶

type Suggest struct {
	Text        string
	Description string
}

Suggest is printed when completing.

func FilterContains ¶

func FilterContains(completions []Suggest, sub string, ignoreCase bool) []Suggest

FilterContains checks whether the completion.Text contains sub.

func FilterHasPrefix ¶

func FilterHasPrefix(completions []Suggest, sub string, ignoreCase bool) []Suggest

FilterHasPrefix checks whether the string completions.Text begins with sub.

func FilterHasSuffix ¶

func FilterHasSuffix(completions []Suggest, sub string, ignoreCase bool) []Suggest

FilterHasSuffix checks whether the completion.Text ends with sub.

type WinSize ¶

type WinSize struct {
	Row uint16
	Col uint16
}

Directories ¶

Path Synopsis
_example
echo command
exec-command command
input command
live-prefix command
sleep command

Jump to

Keyboard shortcuts

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