2015-09-25 19:31:09 +03:00
|
|
|
package readline
|
|
|
|
|
2016-01-07 03:46:43 +03:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/chzyer/readline/runes"
|
|
|
|
)
|
|
|
|
|
2015-09-25 19:31:09 +03:00
|
|
|
type PrefixCompleter struct {
|
2016-01-07 02:39:08 +03:00
|
|
|
Name []rune
|
2016-01-07 03:46:43 +03:00
|
|
|
Children []*PrefixCompleter
|
2016-01-07 00:41:13 +03:00
|
|
|
}
|
|
|
|
|
2016-01-07 03:46:43 +03:00
|
|
|
func (p *PrefixCompleter) Tree(prefix string) string {
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
p.Print(prefix, 0, buf)
|
|
|
|
return buf.String()
|
2015-09-25 19:31:09 +03:00
|
|
|
}
|
|
|
|
|
2016-01-07 03:46:43 +03:00
|
|
|
func (p *PrefixCompleter) Print(prefix string, level int, buf *bytes.Buffer) {
|
|
|
|
if strings.TrimSpace(string(p.Name)) != "" {
|
|
|
|
buf.WriteString(prefix)
|
|
|
|
if level > 0 {
|
|
|
|
buf.WriteString("├")
|
|
|
|
buf.WriteString(strings.Repeat("─", (level*4)-2))
|
|
|
|
buf.WriteString(" ")
|
|
|
|
}
|
|
|
|
buf.WriteString(string(p.Name) + "\n")
|
|
|
|
level++
|
|
|
|
}
|
|
|
|
for _, ch := range p.Children {
|
|
|
|
ch.Print(prefix, level, buf)
|
|
|
|
}
|
2015-11-20 16:32:53 +03:00
|
|
|
}
|
|
|
|
|
2016-01-07 03:46:43 +03:00
|
|
|
func NewPrefixCompleter(pc ...*PrefixCompleter) *PrefixCompleter {
|
2015-09-25 19:31:09 +03:00
|
|
|
return PcItem("", pc...)
|
|
|
|
}
|
|
|
|
|
2016-01-07 03:46:43 +03:00
|
|
|
func PcItem(name string, pc ...*PrefixCompleter) *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),
|
|
|
|
Children: pc,
|
2016-01-07 03:46:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PrefixCompleter) Do(line []rune, pos int) (newLine [][]rune, offset int) {
|
|
|
|
line = line[:pos]
|
|
|
|
goNext := false
|
|
|
|
var lineCompleter *PrefixCompleter
|
|
|
|
for _, child := range p.Children {
|
|
|
|
if len(line) >= len(child.Name) {
|
|
|
|
if runes.HasPrefix(line, child.Name) {
|
|
|
|
if len(line) == len(child.Name) {
|
|
|
|
newLine = append(newLine, []rune{' '})
|
|
|
|
} else {
|
|
|
|
newLine = append(newLine, child.Name)
|
|
|
|
}
|
|
|
|
offset = len(child.Name)
|
|
|
|
lineCompleter = child
|
|
|
|
goNext = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if runes.HasPrefix(child.Name, line) {
|
|
|
|
newLine = append(newLine, child.Name[len(line):])
|
|
|
|
offset = len(line)
|
|
|
|
lineCompleter = child
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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:]...)
|
|
|
|
return lineCompleter.Do(tmpLine, len(tmpLine))
|
|
|
|
}
|
|
|
|
|
|
|
|
if goNext {
|
|
|
|
return lineCompleter.Do(nil, 0)
|
|
|
|
}
|
|
|
|
return
|
2015-09-25 19:31:09 +03:00
|
|
|
}
|