Readline is a pure go(golang) implementation for GNU-Readline kind library
Go to file
Cheney e2dc5c8314 fix backward in complete select mode 2015-09-26 10:34:29 +08:00
example update demo 2015-09-26 01:01:20 +08:00
.gitignore support history 2015-09-21 13:14:10 +08:00
.travis.yml add test to travis 2015-09-25 12:48:59 +08:00
LICENSE Initial commit 2015-09-20 23:11:30 +08:00
README.md update README 2015-09-26 01:06:47 +08:00
char.go add auto complete 2015-09-25 22:56:00 +08:00
complete.go fix backward in complete select mode 2015-09-26 10:34:29 +08:00
complete_helper.go add prefix completer 2015-09-26 00:31:09 +08:00
doc.go add doc 2015-09-23 13:55:53 +08:00
history.go fix fwd search crash 2015-09-23 14:22:22 +08:00
operation.go update demo 2015-09-26 01:01:20 +08:00
readline.go add prefix completer 2015-09-26 00:31:09 +08:00
runebuf.go add auto complete 2015-09-25 22:56:00 +08:00
search.go support double-width-char & colorful prompt 2015-09-25 12:45:39 +08:00
terminal.go fix Ctrl+C in search mode do not send EOF 2015-09-25 00:49:55 +08:00
utils.go add prefix completer 2015-09-26 00:31:09 +08:00
utils_test.go add auto complete 2015-09-25 22:56:00 +08:00

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
  • Transpose words
  • 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 Comment Support
Ctrl+A Beginning of line Yes
Ctrl+B / Backward one character Yes
Meta+B Backward one word Yes
Ctrl+C Send io.EOF Yes
Ctrl+D Delete one character Yes
Meta+D Delete one word Yes
Ctrl+E End of line Yes
Ctrl+F / Forward one character Yes
Meta+F Forward one word Yes
Ctrl+G Cancel Yes
Ctrl+H Delete previous character Yes
Ctrl+I / Tab Command line completion Yes
Ctrl+J Line feed Yes
Ctrl+K Cut text to the end of line Yes
Ctrl+L Clean screen NoYet
Ctrl+M Same as Enter key Yes
Ctrl+N / Next line (in history) Yes
Ctrl+P / Prev line (in history) Yes
Ctrl+R Search backwards in history Yes
Ctrl+S Search forwards in history Yes
Ctrl+T Transpose characters Yes
Meta+T Transpose words Not Yet
Ctrl+U Cut text to the beginning of line Not Yet
Ctrl+W Cut previous word Yes
Backspace Delete previous character Yes
Meta+Backspace Cut previous word Yes
Enter Line feed Yes

Feedback

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