mirror of https://github.com/chzyer/readline.git
example: improve usage content
This commit is contained in:
parent
839c0013a8
commit
c1adc97620
|
@ -1,12 +1,39 @@
|
|||
package readline
|
||||
|
||||
import "github.com/chzyer/readline/runes"
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
|
||||
"github.com/chzyer/readline/runes"
|
||||
)
|
||||
|
||||
type PrefixCompleter struct {
|
||||
Name []rune
|
||||
Children []*PrefixCompleter
|
||||
}
|
||||
|
||||
func (p *PrefixCompleter) Tree(prefix string) string {
|
||||
buf := bytes.NewBuffer(nil)
|
||||
p.Print(prefix, 0, buf)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func NewPrefixCompleter(pc ...*PrefixCompleter) *PrefixCompleter {
|
||||
return PcItem("", pc...)
|
||||
}
|
||||
|
|
|
@ -12,12 +12,8 @@ import (
|
|||
)
|
||||
|
||||
func usage(w io.Writer) {
|
||||
io.WriteString(w, `
|
||||
login
|
||||
setprompt <prompt>
|
||||
say <hello>
|
||||
bye
|
||||
`[1:])
|
||||
io.WriteString(w, "commands:\n")
|
||||
io.WriteString(w, completer.Tree(" "))
|
||||
}
|
||||
|
||||
var completer = readline.NewPrefixCompleter(
|
||||
|
|
Loading…
Reference in New Issue