Skip to content

Commit

Permalink
Improve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Verseth committed Jul 1, 2023
1 parent f63c4c8 commit 42516e5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
17 changes: 10 additions & 7 deletions completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ 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
}

// 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
Expand Down Expand Up @@ -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--
Expand All @@ -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
}
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
17 changes: 10 additions & 7 deletions prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 42516e5

Please sign in to comment.