diff --git a/completion.go b/completion.go index 2ae49354..0405b90d 100644 --- a/completion.go +++ b/completion.go @@ -21,7 +21,8 @@ var ( completionMargin = leftMargin + rightMargin ) -// Suggest is printed when completing. +// Suggest represents a single suggestion +// in the auto-complete box. type Suggest struct { Text string Description string @@ -29,7 +30,7 @@ type Suggest struct { // CompletionManager manages which suggestion is now selected. type CompletionManager struct { - selected int // -1 means nothing one is selected. + selected int // -1 means nothing is selected. tmp []Suggest max uint16 completer Completer @@ -58,19 +59,19 @@ func (c *CompletionManager) GetSuggestions() []Suggest { return c.tmp } -// Reset to select nothing. +// Unselect the currently selected suggestion. func (c *CompletionManager) Reset() { c.selected = -1 c.verticalScroll = 0 c.Update(*NewDocument()) } -// Update to update the suggestions. +// Update the suggestions. func (c *CompletionManager) Update(in Document) { c.tmp = c.completer(in) } -// Previous to select the previous suggestion item. +// Select the previous suggestion item. func (c *CompletionManager) Previous() { if c.verticalScroll == c.selected && c.selected > 0 { c.verticalScroll-- @@ -88,7 +89,7 @@ func (c *CompletionManager) Next() { c.update() } -// Completing returns whether the CompletionManager selects something one. +// Completing returns true when the CompletionManager selects something. func (c *CompletionManager) Completing() bool { return c.selected != -1 } @@ -180,7 +181,7 @@ func formatSuggestions(suggests []Suggest, max int) (new []Suggest, width int) { return new, leftWidth + rightWidth } -// NewCompletionManager returns initialized CompletionManager object. +// NewCompletionManager returns an initialized CompletionManager object. func NewCompletionManager(completer Completer, max uint16) *CompletionManager { return &CompletionManager{ selected: -1, @@ -193,6 +194,8 @@ func NewCompletionManager(completer Completer, max uint16) *CompletionManager { var _ Completer = NoopCompleter +// NoopCompleter implements a Completer function +// that always returns no suggestions. func NoopCompleter(_ Document) []Suggest { return nil } diff --git a/prompt.go b/prompt.go index 9fa70c3b..7a3fae3f 100644 --- a/prompt.go +++ b/prompt.go @@ -8,7 +8,8 @@ import ( "github.com/c-bata/go-prompt/internal/debug" ) -// Executor is called when user input something text. +// Executor is called when the user +// inputs a line of text. type Executor func(string) // ExitChecker is called after user input to check if prompt must stop and exit go-prompt Run loop. @@ -18,17 +19,18 @@ type Executor func(string) // Exit means exit go-prompt (not the overall Go program) type ExitChecker func(in string, breakline bool) bool -// Completer should return the suggest item from Document. +// Completer is a function that returns +// a slice of suggestions for the given Document. type Completer func(Document) []Suggest -// Prompt is core struct of go-prompt. +// Prompt is a core struct of go-prompt. type Prompt struct { in ConsoleParser buf *Buffer renderer *Render executor Executor history *History - lexer *Lexer + lexer *Lexer completion *CompletionManager keyBindings []KeyBind ASCIICodeBindings []ASCIICodeBind @@ -38,12 +40,12 @@ type Prompt struct { skipTearDown bool } -// Exec is the struct contains user input context. +// Exec is the struct that contains the user input context. type Exec struct { input string } -// Run starts prompt. +// Run starts the prompt. func (p *Prompt) Run() { p.skipTearDown = false defer debug.Teardown() @@ -230,7 +232,8 @@ func (p *Prompt) handleASCIICodeBinding(b []byte) bool { return checked } -// Input just returns user input text. +// Input starts the prompt, lets the user +// input a single line and returns this line as a string. func (p *Prompt) Input() string { defer debug.Teardown() debug.Log("start prompt")