readline/example/main.go

56 lines
825 B
Go
Raw Normal View History

2015-09-20 18:14:29 +03:00
package main
import (
"fmt"
2015-09-20 18:14:29 +03:00
"io"
"log"
"strconv"
"time"
"github.com/traetox/readline"
2015-09-20 18:14:29 +03:00
)
2015-09-21 17:27:40 +03:00
func usage(w io.Writer) {
io.WriteString(w, `
sayhello: start to display oneline log per second
bye: quit
`[1:])
}
2015-09-20 18:14:29 +03:00
func main() {
2015-09-22 13:16:24 +03:00
l, err := readline.NewEx(&readline.Config{
2015-09-23 09:52:45 +03:00
Prompt: "home » ",
2015-09-22 13:16:24 +03:00
HistoryFile: "/tmp/readline.tmp",
})
2015-09-20 18:14:29 +03:00
if err != nil {
panic(err)
}
2015-09-21 08:30:10 +03:00
defer l.Close()
2015-09-20 18:14:29 +03:00
log.SetOutput(l.Stderr())
for {
line, err := l.Readline()
if err != nil {
break
}
switch line {
case "help":
2015-09-21 17:27:40 +03:00
usage(l.Stderr())
2015-09-20 18:14:29 +03:00
case "sayhello":
go func() {
for _ = range time.Tick(time.Second) {
log.Println("hello")
}
}()
case "bye":
goto exit
case "write":
fmt.Fprintf(l, "look, i am writer too\n")
2015-09-21 17:27:40 +03:00
case "":
2015-09-20 18:14:29 +03:00
default:
log.Println("you said:", strconv.Quote(line))
}
}
exit:
}