Categorygithub.com/rivalo/readline
repositorypackage
0.0.0-20160215020341-01cffb6cb2f1
Repository: https://github.com/rivalo/readline.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author

# README

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.

It can also provides shell-like interactives by using flagly (demo: flagly-shell)

Demo

demo

Also works fine in windows

demo windows

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 :)