* #12 Add support for key `<Delete>`/`<Home>`/`<End>`
* Only enter raw mode as needed (calling `Readline()`), program will receive signal(e.g. Ctrl+C) if not interact with `readline`.
* Bugs fixed for `PrefixCompleter`
* Press `Ctrl+D` in empty line will cause `io.EOF` in error, Press `Ctrl+C` in anytime will cause `ErrInterrupt` instead of `io.EOF`, this will privodes a shell-like user experience.
* Customable Interrupt/EOF prompt in `Config`
* #17 Change atomic package to use 32bit function to let it runnable on arm 32bit devices
* Provides a new password user experience(`readline.ReadPasswordEx()`).
changelog: fix syntax
1 file changed
tree: aae132bcd433842cc6619f763902a7d96074dcf9
  1. example/
  2. runes/
  3. .gitignore
  4. .travis.yml
  5. ansi_windows.go
  6. CHANGELOG.md
  7. char.go
  8. complete.go
  9. complete_helper.go
  10. debug.go
  11. doc.go
  12. history.go
  13. LICENSE
  14. operation.go
  15. password.go
  16. rawreader_windows.go
  17. readline.go
  18. README.md
  19. runebuf.go
  20. search.go
  21. std.go
  22. std_windows.go
  23. terminal.go
  24. utils.go
  25. utils_test.go
  26. utils_unix.go
  27. utils_windows.go
  28. vim.go
  29. windows_api.go
README.md

readline

Software License Build Status GoDoc
Gitter

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

Also works fine in windows

demo windows

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

Todo

  • Vi Mode is not completely finish
  • More funny examples
  • Support dumb/eterm-color terminal in emacs

Features

  • Support emacs/vi mode, almost all basic features that GNU-Readline is supported
  • zsh-style backward/forward history search
  • zsh-style completion
  • Readline auto refresh when others write to Stdout while editing (it needs specify the Stdout/Stderr provided by *readline.Instance to others).
  • Support colourful prompt in all platforms.

Usage

  • Import package
go get gopkg.in/readline.v1

or

go get github.com/chzyer/readline
  • Simplest example
import "gopkg.in/readline.v1"

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"
	"gopkg.in/readline.v1"
)

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 (
	"gopkg.in/readline.v1"
)

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

rl, err := readline.NewEx(&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
Notice: Meta+B is equals with Alt+B in windows.

  • 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
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
Mac OS X iTerm2 Screenscreen
Mac OS X iTerm2 Tmuxscreen
Ubuntu Server 14.04 LTSlinux
Centos 7linux
Windows 10-

Notice:

  • Ctrl+A is not working in screen because it used as a control command by default

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

Feedback

If you have any questions, please submit a github issue and any pull requests is welcomed :)