2015-09-25 19:31:09 +03:00
|
|
|
package readline
|
|
|
|
|
2016-01-07 03:46:43 +03:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2016-07-26 16:39:09 +03:00
|
|
|
// Caller type for dynamic completion
|
|
|
|
type DynamicCompleteFunc func(string) []string
|
|
|
|
|
2016-01-07 18:42:12 +03:00
|
|
|
type PrefixCompleterInterface interface {
|
|
|
|
Print(prefix string, level int, buf *bytes.Buffer)
|
|
|
|
Do(line []rune, pos int) (newLine [][]rune, length int)
|
|
|
|
GetName() []rune
|
|
|
|
GetChildren() []PrefixCompleterInterface
|
2016-03-16 13:03:13 +03:00
|
|
|
SetChildren(children []PrefixCompleterInterface)
|
2016-01-07 00:41:13 +03:00
|
|
|
}
|
|
|
|
|
2016-07-26 16:39:09 +03:00
|
|
|
type DynamicPrefixCompleterInterface interface {
|
|
|
|
PrefixCompleterInterface
|
|
|
|
IsDynamic() bool
|
|
|
|
GetDynamicNames(line []rune) [][]rune
|
|
|
|
}
|
|
|
|
|
2016-01-07 18:42:12 +03:00
|
|
|
type PrefixCompleter struct {
|
|
|
|
Name []rune
|
2016-07-26 16:39:09 +03:00
|
|
|
Dynamic bool
|
|
|
|
Callback DynamicCompleteFunc
|
2016-01-07 18:42:12 +03:00
|
|
|
Children []PrefixCompleterInterface
|
2015-09-25 19:31:09 +03:00
|
|
|
}
|
|
|
|
|
2016-01-07 18:54:10 +03:00
|
|
|
func (p *PrefixCompleter) Tree(prefix string) string {
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
p.Print(prefix, 0, buf)
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2016-01-07 18:42:12 +03:00
|
|
|
func Print(p PrefixCompleterInterface, prefix string, level int, buf *bytes.Buffer) {
|
|
|
|
if strings.TrimSpace(string(p.GetName())) != "" {
|
2016-01-07 03:46:43 +03:00
|
|
|
buf.WriteString(prefix)
|
|
|
|
if level > 0 {
|
|
|
|
buf.WriteString("├")
|
|
|
|
buf.WriteString(strings.Repeat("─", (level*4)-2))
|
|
|
|
buf.WriteString(" ")
|
|
|
|
}
|
2016-01-07 18:42:12 +03:00
|
|
|
buf.WriteString(string(p.GetName()) + "\n")
|
2016-01-07 03:46:43 +03:00
|
|
|
level++
|
|
|
|
}
|
2016-01-07 18:42:12 +03:00
|
|
|
for _, ch := range p.GetChildren() {
|
2016-01-07 03:46:43 +03:00
|
|
|
ch.Print(prefix, level, buf)
|
|
|
|
}
|
2015-11-20 16:32:53 +03:00
|
|
|
}
|
|
|
|
|
2016-01-07 18:42:12 +03:00
|
|
|
func (p *PrefixCompleter) Print(prefix string, level int, buf *bytes.Buffer) {
|
|
|
|
Print(p, prefix, level, buf)
|
|
|
|
}
|
|
|
|
|
2016-07-26 16:39:09 +03:00
|
|
|
func (p *PrefixCompleter) IsDynamic() bool {
|
|
|
|
return p.Dynamic
|
|
|
|
}
|
|
|
|
|
2016-01-07 18:42:12 +03:00
|
|
|
func (p *PrefixCompleter) GetName() []rune {
|
|
|
|
return p.Name
|
|
|
|
}
|
|
|
|
|
2016-07-26 16:39:09 +03:00
|
|
|
func (p *PrefixCompleter) GetDynamicNames(line []rune) [][]rune {
|
|
|
|
var names = [][]rune{}
|
|
|
|
for _, name := range p.Callback(string(line)) {
|
|
|
|
names = append(names, []rune(name+" "))
|
|
|
|
}
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
2016-01-07 18:42:12 +03:00
|
|
|
func (p *PrefixCompleter) GetChildren() []PrefixCompleterInterface {
|
|
|
|
return p.Children
|
|
|
|
}
|
|
|
|
|
2016-03-16 13:03:13 +03:00
|
|
|
func (p *PrefixCompleter) SetChildren(children []PrefixCompleterInterface) {
|
|
|
|
p.Children = children
|
|
|
|
}
|
|
|
|
|
2016-01-07 18:42:12 +03:00
|
|
|
func NewPrefixCompleter(pc ...PrefixCompleterInterface) *PrefixCompleter {
|
2015-09-25 19:31:09 +03:00
|
|
|
return PcItem("", pc...)
|
|
|
|
}
|
|
|
|
|
2016-01-07 18:42:12 +03:00
|
|
|
func PcItem(name string, pc ...PrefixCompleterInterface) *PrefixCompleter {
|
2015-11-14 05:21:55 +03:00
|
|
|
name += " "
|
2016-01-07 03:46:43 +03:00
|
|
|
return &PrefixCompleter{
|
2016-01-07 02:39:08 +03:00
|
|
|
Name: []rune(name),
|
2016-07-26 16:39:09 +03:00
|
|
|
Dynamic: false,
|
|
|
|
Children: pc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func PcItemDynamic(callback DynamicCompleteFunc, pc ...PrefixCompleterInterface) *PrefixCompleter {
|
|
|
|
return &PrefixCompleter{
|
|
|
|
Callback: callback,
|
|
|
|
Dynamic: true,
|
2016-01-07 02:39:08 +03:00
|
|
|
Children: pc,
|
2016-01-07 03:46:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PrefixCompleter) Do(line []rune, pos int) (newLine [][]rune, offset int) {
|
2016-07-26 16:39:09 +03:00
|
|
|
return doInternal(p, line, pos, line)
|
2016-01-07 18:42:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func Do(p PrefixCompleterInterface, line []rune, pos int) (newLine [][]rune, offset int) {
|
2016-07-26 16:39:09 +03:00
|
|
|
return doInternal(p, line, pos, line)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doInternal(p PrefixCompleterInterface, line []rune, pos int, origLine []rune) (newLine [][]rune, offset int) {
|
2016-04-08 05:01:57 +03:00
|
|
|
line = runes.TrimSpaceLeft(line[:pos])
|
2016-01-07 03:46:43 +03:00
|
|
|
goNext := false
|
2016-01-07 18:42:12 +03:00
|
|
|
var lineCompleter PrefixCompleterInterface
|
|
|
|
for _, child := range p.GetChildren() {
|
2016-07-26 16:39:09 +03:00
|
|
|
childNames := make([][]rune, 1)
|
|
|
|
|
|
|
|
childDynamic, ok := child.(DynamicPrefixCompleterInterface)
|
|
|
|
if ok && childDynamic.IsDynamic() {
|
|
|
|
childNames = childDynamic.GetDynamicNames(origLine)
|
2016-01-07 03:46:43 +03:00
|
|
|
} else {
|
2016-07-26 16:39:09 +03:00
|
|
|
childNames[0] = child.GetName()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, childName := range childNames {
|
|
|
|
if len(line) >= len(childName) {
|
|
|
|
if runes.HasPrefix(line, childName) {
|
|
|
|
if len(line) == len(childName) {
|
|
|
|
newLine = append(newLine, []rune{' '})
|
|
|
|
} else {
|
|
|
|
newLine = append(newLine, childName)
|
|
|
|
}
|
|
|
|
offset = len(childName)
|
|
|
|
lineCompleter = child
|
|
|
|
goNext = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if runes.HasPrefix(childName, line) {
|
|
|
|
newLine = append(newLine, childName[len(line):])
|
|
|
|
offset = len(line)
|
|
|
|
lineCompleter = child
|
|
|
|
}
|
2016-01-07 03:46:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(newLine) != 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpLine := make([]rune, 0, len(line))
|
|
|
|
for i := offset; i < len(line); i++ {
|
|
|
|
if line[i] == ' ' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpLine = append(tmpLine, line[i:]...)
|
2016-07-26 16:39:09 +03:00
|
|
|
return doInternal(lineCompleter, tmpLine, len(tmpLine), origLine)
|
2016-01-07 03:46:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if goNext {
|
2016-07-26 16:39:09 +03:00
|
|
|
return doInternal(lineCompleter, nil, 0, origLine)
|
2016-01-07 03:46:43 +03:00
|
|
|
}
|
|
|
|
return
|
2015-09-25 19:31:09 +03:00
|
|
|
}
|