update README: Tested with
1 file changed
tree: 10c8d3b1f1ba3d482b74a905e9fae42f1ef0b888
  1. example/
  2. .gitignore
  3. .travis.yml
  4. char.go
  5. complete.go
  6. complete_helper.go
  7. doc.go
  8. history.go
  9. LICENSE
  10. operation.go
  11. readline.go
  12. README.md
  13. runebuf.go
  14. search.go
  15. terminal.go
  16. utils.go
  17. utils_test.go
README.md

readline

Software License Build Status GoDoc

Readline is A Pure Go Implementation of a libreadline-style Library.
The goal is to be a powerful alternater for GNU-Readline.

WHY: Readline will support most of features which GNU Readline is supported, and provide a pure go environment and a MIT license.

Demo

demo

You can read the source code in example/main.go.

Todo

  • Vim mode
  • More funny examples

Usage

  • Simplest example
import "github.com/chzyer/readline"

rl, err := readline.New("> ")
if err != nil {
	panic(err)
}
defer rl.Close()

for {
	line, err := rl.Readline()
	if err != nil { // io.EOF
		break
	}
	println(line)
}
  • Example with durable history
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
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)
}
  • Example with auto completion
import (
	"log"
	"github.com/chzyer/readline"
)

var completer = readline.NewPrefixCompleter(
	readline.PcItem("say",
		readline.PcItem("hello"),
		readline.PcItem("bye"),
	),
	readline.PcItem("help"),
)

rl, err := readline.New(&readline.Config{
	Prompt:       "> ",
	AutoComplete: completer,
})
if err != nil {
	panic(err)
}
defer rl.Close()

for {
	line, err := rl.Readline()
	if err != nil { // io.EOF
		break
	}
	println(line)
}

Shortcut

Meta+B means press Esc and n separately.
Users can change that in terminal simulator(i.e. iTerm2) to Alt+B

  • Shortcut in normal mode
ShortcutComment
Ctrl+ABeginning of line
Ctrl+B / Backward one character
Meta+BBackward one word
Ctrl+CSend io.EOF
Ctrl+DDelete one character
Meta+DDelete one word
Ctrl+EEnd of line
Ctrl+F / Forward one character
Meta+FForward one word
Ctrl+GCancel
Ctrl+HDelete previous character
Ctrl+I / TabCommand line completion
Ctrl+JLine feed
Ctrl+KCut text to the end of line
Ctrl+LClean screen (TODO)
Ctrl+MSame as Enter key
Ctrl+N / Next line (in history)
Ctrl+P / Prev line (in history)
Ctrl+RSearch backwards in history
Ctrl+SSearch forwards in history
Ctrl+TTranspose characters
Meta+TTranspose words (TODO)
Ctrl+UCut text to the beginning of line (TODO)
Ctrl+WCut previous word
BackspaceDelete previous character
Meta+BackspaceCut previous word
EnterLine feed
  • Shortcut in Search Mode (Ctrl+S or Ctrl+r to enter this mode)
ShortcutComment
Ctrl+SSearch forwards in history
Ctrl+RSearch backwards in history
Ctrl+C / Ctrl+GExit Search Mode and revert the history
BackspaceDelete previous character
OtherExit Search Mode
  • Shortcut in Complete Select Mode (double Tab to enter this mode)
ShortcutComment
Ctrl+FMove Forward
Ctrl+BMove Backward
Ctrl+NMove to next line
Ctrl+PMove to previous line
Ctrl+AMove to the first candicate in current line
Ctrl+EMove to the last candicate in current line
Tab / EnterUse the word on cursor to complete
Ctrl+C / Ctrl+GExit Complete Select Mode
OtherExit Complete Select Mode

Tested with

Environment$TERM
Mac OS X iTerm2xterm
Mac OS X default Terminal.appxterm
Ubuntu Server 14.04 LTSlinux
Centos 7linux

If you test it otherwhere, whether it works fine or not, please let me know!

Feedback

If you have any question, please submit an GitHub Issues and any pull request is welcomed :)