go-prompt/completion.go

58 lines
948 B
Go
Raw Normal View History

2017-08-04 11:19:12 +00:00
package prompt
2017-08-04 11:30:50 +00:00
type Suggest struct {
2017-08-04 11:19:12 +00:00
Text string
Description string
}
type CompletionManager struct {
selected int // -1 means nothing one is selected.
2017-08-04 11:30:50 +00:00
tmp []*Suggest
2017-08-04 11:19:12 +00:00
Max uint16
}
func (c *CompletionManager) Reset() {
c.selected = -1
2017-08-09 12:28:34 +00:00
c.Update([]*Suggest{})
2017-08-04 11:19:12 +00:00
return
}
2017-08-04 11:30:50 +00:00
func (c *CompletionManager) Update(new []*Suggest) {
2017-08-04 11:19:12 +00:00
c.selected = -1
c.tmp = new
return
}
func (c *CompletionManager) Previous() {
c.selected--
return
}
func (c *CompletionManager) Next() {
c.selected++
return
}
func (c *CompletionManager) Completing() bool {
return c.selected != -1
}
2017-08-04 11:30:50 +00:00
func (c *CompletionManager) update(completions []Suggest) {
2017-08-04 11:19:12 +00:00
max := int(c.Max)
if len(completions) < max {
max = len(completions)
}
if c.selected >= max {
c.Reset()
} else if c.selected < -1 {
c.selected = max - 1
}
}
func NewCompletionManager(max uint16) *CompletionManager {
return &CompletionManager{
selected: -1,
Max: max,
}
}