readme: add example

This commit is contained in:
Cheney 2015-09-23 13:32:43 +08:00
parent 97dbc9e329
commit 93a73361b8
1 changed files with 51 additions and 0 deletions

View File

@ -12,6 +12,7 @@ A pure go implementation for gnu readline.
# Usage
* Simplest example
```go
import "github.com/chzyer/readline"
@ -30,6 +31,56 @@ for {
}
```
* Example with durable history
```go
rl, err := readline.NewEx(&readline.Config{
Prompt: "> ",
HistoryFile: "/tmp/readline.tmp",
})
if err != nil {
panic(err)
}
defer rl.Close()
for {
line, err := rl.Readline()
if err != nil { // io.EOF
break
}
println(line)
}
```
* Example with auto refresh
```go
import (
"log"
"github.com/chzyer/readline"
)
rl, err := readline.New("> ")
if err != nil {
panic(err)
}
defer rl.Close()
log.SetOutput(l.Stderr()) // let "log" write to l.Stderr instead of os.Stderr
go func() {
for _ = range time.Tick(time.Second) {
log.Println("hello")
}
}()
for {
line, err := rl.Readline()
if err != nil { // io.EOF
break
}
println(line)
}
```
# Shortcut
`Meta`+`B` means press `Esc` and `n` separately.