mirror of https://github.com/chzyer/readline.git
92 lines
1.6 KiB
Go
92 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"io"
|
|
"log"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/chzyer/readline"
|
|
)
|
|
|
|
func usage(w io.Writer) {
|
|
io.WriteString(w, `
|
|
login
|
|
setprompt <prompt>
|
|
say <hello>
|
|
bye
|
|
`[1:])
|
|
}
|
|
|
|
var completer = readline.NewPrefixCompleter(
|
|
readline.PcItem("login"),
|
|
readline.PcItem("say",
|
|
readline.PcItem("hello"),
|
|
readline.PcItem("bye"),
|
|
),
|
|
readline.PcItem("setprompt"),
|
|
readline.PcItem("bye"),
|
|
readline.PcItem("help"),
|
|
readline.PcItem("go",
|
|
readline.PcItem("build"),
|
|
readline.PcItem("install"),
|
|
readline.PcItem("test"),
|
|
),
|
|
)
|
|
|
|
func main() {
|
|
l, err := readline.NewEx(&readline.Config{
|
|
Prompt: "\033[31m»\033[0m ",
|
|
HistoryFile: "/tmp/readline.tmp",
|
|
AutoComplete: completer,
|
|
})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer l.Close()
|
|
|
|
log.SetOutput(l.Stderr())
|
|
for {
|
|
line, err := l.Readline()
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
switch {
|
|
case line == "login":
|
|
pswd, err := l.ReadPassword("please enter your password: ")
|
|
if err != nil {
|
|
break
|
|
}
|
|
println("you enter:", strconv.Quote(string(pswd)))
|
|
case line == "help":
|
|
usage(l.Stderr())
|
|
case strings.HasPrefix(line, "setprompt"):
|
|
prompt := line[10:]
|
|
if prompt == "" {
|
|
log.Println("setprompt <prompt>")
|
|
break
|
|
}
|
|
l.SetPrompt(prompt)
|
|
case strings.HasPrefix(line, "say"):
|
|
line := strings.TrimSpace(line[3:])
|
|
if len(line) == 0 {
|
|
log.Println("say what?")
|
|
break
|
|
}
|
|
go func() {
|
|
for _ = range time.Tick(time.Second) {
|
|
log.Println(line)
|
|
}
|
|
}()
|
|
case line == "bye":
|
|
goto exit
|
|
case line == "":
|
|
default:
|
|
log.Println("you said:", strconv.Quote(line))
|
|
}
|
|
}
|
|
exit:
|
|
}
|