Add some doc comments
This commit is contained in:
parent
a8daf831aa
commit
f8cde79229
@ -16,11 +16,13 @@ const (
|
||||
completionMargin = leftMargin + rightMargin
|
||||
)
|
||||
|
||||
// Suggest is printed when completing.
|
||||
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.
|
||||
tmp []Suggest
|
||||
@ -28,6 +30,7 @@ type CompletionManager struct {
|
||||
completer Completer
|
||||
}
|
||||
|
||||
// GetSelectedSuggestion returns the selected item.
|
||||
func (c *CompletionManager) GetSelectedSuggestion() (s Suggest, ok bool) {
|
||||
if c.selected == -1 {
|
||||
return Suggest{}, false
|
||||
@ -39,33 +42,39 @@ func (c *CompletionManager) GetSelectedSuggestion() (s Suggest, ok bool) {
|
||||
return c.tmp[c.selected], true
|
||||
}
|
||||
|
||||
// GetSelectedSuggestion returns the list of suggestion.
|
||||
func (c *CompletionManager) GetSuggestions() []Suggest {
|
||||
return c.tmp
|
||||
}
|
||||
|
||||
// Reset to select nothing.
|
||||
func (c *CompletionManager) Reset() {
|
||||
c.selected = -1
|
||||
c.Update(*NewDocument())
|
||||
return
|
||||
}
|
||||
|
||||
// Update to update the suggestions.
|
||||
func (c *CompletionManager) Update(in Document) {
|
||||
c.tmp = c.completer(in)
|
||||
return
|
||||
}
|
||||
|
||||
// Previous to select the previous suggestion item.
|
||||
func (c *CompletionManager) Previous() {
|
||||
c.selected--
|
||||
c.update()
|
||||
return
|
||||
}
|
||||
|
||||
// Next to select the next suggestion item.
|
||||
func (c *CompletionManager) Next() {
|
||||
c.selected++
|
||||
c.update()
|
||||
return
|
||||
}
|
||||
|
||||
// Completing returns whether the CompletionManager selects something one.
|
||||
func (c *CompletionManager) Completing() bool {
|
||||
return c.selected != -1
|
||||
}
|
||||
@ -145,6 +154,7 @@ func formatSuggestions(suggests []Suggest, max int) (new []Suggest, width int) {
|
||||
return new, leftWidth + rightWidth
|
||||
}
|
||||
|
||||
// NewCompletionManager returns initialized CompletionManager object.
|
||||
func NewCompletionManager(completer Completer, max uint16) *CompletionManager {
|
||||
return &CompletionManager{
|
||||
selected: -1,
|
||||
|
3
key.go
3
key.go
@ -1,6 +1,9 @@
|
||||
package prompt
|
||||
|
||||
// Key is the type express the key inserted from user.
|
||||
type Key int
|
||||
|
||||
// ASCIICode is the type contains Key and it's ascii byte array.
|
||||
type ASCIICode struct {
|
||||
Key Key
|
||||
ASCIICode []byte
|
||||
|
@ -14,9 +14,13 @@ const (
|
||||
envEnableLog = "GO_PROMPT_ENABLE_LOG"
|
||||
)
|
||||
|
||||
// Executor is called when user input something text.
|
||||
type Executor func(string)
|
||||
|
||||
// Completer should return the suggest item from Document.
|
||||
type Completer func(Document) []Suggest
|
||||
|
||||
// Prompt is core struct of go-prompt.
|
||||
type Prompt struct {
|
||||
in ConsoleParser
|
||||
buf *Buffer
|
||||
@ -28,10 +32,12 @@ type Prompt struct {
|
||||
keyBindMode KeyBindMode
|
||||
}
|
||||
|
||||
// Exec is the struct contains user input context.
|
||||
type Exec struct {
|
||||
input string
|
||||
}
|
||||
|
||||
// Run starts prompt.
|
||||
func (p *Prompt) Run() {
|
||||
// Logging
|
||||
if os.Getenv(envEnableLog) != "true" {
|
||||
@ -187,6 +193,7 @@ func (p *Prompt) feed(b []byte) (shouldExit bool, exec *Exec) {
|
||||
return
|
||||
}
|
||||
|
||||
// Input just returns user input text.
|
||||
func (p *Prompt) Input() string {
|
||||
// Logging
|
||||
if os.Getenv(envEnableLog) != "true" {
|
||||
|
@ -1,5 +1,6 @@
|
||||
package prompt
|
||||
|
||||
// Render to render prompt information from state of Buffer.
|
||||
type Render struct {
|
||||
out ConsoleWriter
|
||||
prefix string
|
||||
@ -23,6 +24,7 @@ type Render struct {
|
||||
selectedDescriptionBGColor Color
|
||||
}
|
||||
|
||||
// Setup to initialize console output.
|
||||
func (r *Render) Setup() {
|
||||
if r.title != "" {
|
||||
r.out.SetTitle(r.title)
|
||||
@ -36,6 +38,7 @@ func (r *Render) renderPrefix() {
|
||||
r.out.SetColor(DefaultColor, DefaultColor, false)
|
||||
}
|
||||
|
||||
// TearDown to clear title and erasing.
|
||||
func (r *Render) TearDown() {
|
||||
r.out.ClearTitle()
|
||||
r.out.EraseDown()
|
||||
@ -52,6 +55,7 @@ func (r *Render) prepareArea(lines int) {
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateWinSize called when window size is changed.
|
||||
func (r *Render) UpdateWinSize(ws *WinSize) {
|
||||
r.row = ws.Row
|
||||
r.col = ws.Col
|
||||
@ -124,6 +128,7 @@ func (r *Render) renderCompletion(buf *Buffer, completions *CompletionManager) {
|
||||
return
|
||||
}
|
||||
|
||||
// Render renders to the console.
|
||||
func (r *Render) Render(buffer *Buffer, completion *CompletionManager) {
|
||||
// Erasing
|
||||
r.out.CursorBackward(int(r.col) + len(buffer.Text()) + len(r.prefix))
|
||||
@ -154,6 +159,7 @@ func (r *Render) Render(buffer *Buffer, completion *CompletionManager) {
|
||||
r.out.Flush()
|
||||
}
|
||||
|
||||
// BreakLine to break line.
|
||||
func (r *Render) BreakLine(buffer *Buffer) {
|
||||
// CR
|
||||
r.out.CursorBackward(int(r.col) + len(buffer.Text()) + len(r.prefix))
|
||||
|
Loading…
Reference in New Issue
Block a user