#15: fix prefix completer

This commit is contained in:
Cheney 2015-11-14 10:21:55 +08:00
parent 5ee706df9b
commit 9364259fb1
2 changed files with 12 additions and 6 deletions

View File

@ -12,9 +12,7 @@ func NewPrefixCompleter(pc ...*PrefixCompleter) *PrefixCompleter {
}
func PcItem(name string, pc ...*PrefixCompleter) *PrefixCompleter {
if len(pc) != 0 {
name += " "
}
name += " "
return &PrefixCompleter{
Name: []rune(name),
Children: pc,
@ -28,7 +26,11 @@ func (p *PrefixCompleter) Do(line []rune, pos int) (newLine [][]rune, offset int
for _, child := range p.Children {
if len(line) >= len(child.Name) {
if runes.HasPrefix(line, child.Name) {
newLine = append(newLine, 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

View File

@ -33,8 +33,12 @@ var completer = readline.NewPrefixCompleter(
readline.PcItem("bye"),
readline.PcItem("help"),
readline.PcItem("go",
readline.PcItem("build"),
readline.PcItem("install"),
readline.PcItem("build", readline.PcItem("-o"), readline.PcItem("-v")),
readline.PcItem("install",
readline.PcItem("-v"),
readline.PcItem("-vv"),
readline.PcItem("-vvv"),
),
readline.PcItem("test"),
),
readline.PcItem("sleep"),