From 93a73361b8c26f93044a6b2ca90cc88f720f8a9a Mon Sep 17 00:00:00 2001 From: Cheney Date: Wed, 23 Sep 2015 13:32:43 +0800 Subject: [PATCH] readme: add example --- README.md | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/README.md b/README.md index 3ec62ea..71564db 100644 --- a/README.md +++ b/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.