From 9364259fb129ed16164189f89518fbbf6851fd4f Mon Sep 17 00:00:00 2001 From: Cheney Date: Sat, 14 Nov 2015 10:21:55 +0800 Subject: [PATCH] #15: fix prefix completer --- complete_helper.go | 10 ++++++---- example/main.go | 8 ++++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/complete_helper.go b/complete_helper.go index b153bc5..114190e 100644 --- a/complete_helper.go +++ b/complete_helper.go @@ -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 diff --git a/example/main.go b/example/main.go index 69fef4b..cc58e57 100644 --- a/example/main.go +++ b/example/main.go @@ -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"),