mirror of https://github.com/chzyer/readline.git
readme: add example
This commit is contained in:
parent
97dbc9e329
commit
93a73361b8
51
README.md
51
README.md
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue