readline/README.md

325 lines
13 KiB
Markdown
Raw Permalink Normal View History

2015-09-20 18:11:30 +03:00
# readline
2015-09-20 18:16:46 +03:00
2015-09-23 08:09:26 +03:00
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE.md)
2015-09-23 08:12:20 +03:00
[![Build Status](https://travis-ci.org/chzyer/readline.svg?branch=master)](https://travis-ci.org/chzyer/readline)
2016-02-11 07:21:32 +03:00
[![GoDoc](https://godoc.org/github.com/chzyer/readline?status.svg)](https://godoc.org/github.com/chzyer/readline)
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/chzyer/readline?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
[![OpenCollective](https://opencollective.com/readline/badge/backers.svg)](https://opencollective.com/readline#support)
2015-09-23 08:09:26 +03:00
2015-09-25 08:06:36 +03:00
Readline is A Pure Go Implementation of a libreadline-style Library.
The goal is to be a powerful alternater for GNU-Readline.
2015-09-23 08:43:48 +03:00
**WHY:**
Readline will support most of features which GNU Readline is supported, and provide a pure go environment and a MIT license.
2015-09-23 08:03:18 +03:00
2016-02-11 07:21:32 +03:00
It can also provides shell-like interactives by using [flagly](https://github.com/chzyer/flagly) (demo: [flagly-shell](https://github.com/chzyer/flagly/blob/master/demo/flagly-shell/flagly-shell.go))
2015-09-23 08:09:26 +03:00
# Demo
2015-10-09 06:26:59 +03:00
![demo](https://raw.githubusercontent.com/chzyer/readline/assets/demo.gif)
2015-09-23 08:09:26 +03:00
2015-09-29 18:57:14 +03:00
Also works fine in windows
2015-10-09 06:26:59 +03:00
![demo windows](https://raw.githubusercontent.com/chzyer/readline/assets/windows.gif)
2015-09-29 18:57:14 +03:00
2015-11-21 06:36:20 +03:00
* [example/readline-demo](https://github.com/chzyer/readline/blob/master/example/readline-demo/readline-demo.go) The source code about the demo above
* [example/readline-pass-strength](https://github.com/chzyer/readline/blob/master/example/readline-pass-strength/readline-pass-strength.go) A example about checking password strength, written by [@sahib](https://github.com/sahib)
2015-09-23 08:26:42 +03:00
2015-09-25 07:59:36 +03:00
# Todo
2015-10-02 07:46:27 +03:00
* Vi Mode is not completely finish
2015-09-25 07:59:36 +03:00
* More funny examples
2015-09-30 11:13:36 +03:00
* Support dumb/eterm-color terminal in emacs
2015-09-25 07:59:36 +03:00
2015-10-02 05:37:21 +03:00
# Features
2015-10-02 05:58:43 +03:00
* Support emacs/vi mode, almost all basic features that GNU-Readline is supported
2015-10-02 05:37:21 +03:00
* zsh-style backward/forward history search
* zsh-style completion
2015-10-02 06:01:54 +03:00
* 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.
2015-10-02 05:37:21 +03:00
2015-09-23 08:43:48 +03:00
# Usage
2015-09-23 08:35:48 +03:00
2015-10-16 04:01:31 +03:00
* Import package
```
go get gopkg.in/readline.v1
```
or
```
2015-10-16 04:02:55 +03:00
go get github.com/chzyer/readline
2015-10-16 04:01:31 +03:00
```
2015-09-23 08:32:43 +03:00
* Simplest example
2015-09-23 08:35:48 +03:00
2015-09-23 08:26:42 +03:00
```go
2015-10-16 04:01:31 +03:00
import "gopkg.in/readline.v1"
2015-09-23 08:26:42 +03:00
rl, err := readline.New("> ")
if err != nil {
panic(err)
}
defer rl.Close()
for {
line, err := rl.Readline()
2016-02-24 17:52:02 +03:00
if err != nil { // io.EOF, readline.ErrInterrupt
2015-09-23 08:26:42 +03:00
break
}
println(line)
}
```
2015-09-23 08:32:43 +03:00
* Example with durable history
2015-09-23 08:35:48 +03:00
2015-09-23 08:32:43 +03:00
```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()
2016-02-24 17:52:02 +03:00
if err != nil { // io.EOF, readline.ErrInterrupt
2015-09-23 08:32:43 +03:00
break
}
println(line)
}
```
2015-09-25 20:06:47 +03:00
* Example with auto completion
```go
import (
2015-10-16 04:01:31 +03:00
"gopkg.in/readline.v1"
2015-09-25 20:06:47 +03:00
)
var completer = readline.NewPrefixCompleter(
readline.PcItem("say",
readline.PcItem("hello"),
readline.PcItem("bye"),
),
readline.PcItem("help"),
)
rl, err := readline.NewEx(&readline.Config{
2015-09-25 20:06:47 +03:00
Prompt: "> ",
AutoComplete: completer,
})
if err != nil {
panic(err)
}
defer rl.Close()
for {
line, err := rl.Readline()
2016-02-24 17:52:02 +03:00
if err != nil { // io.EOF, readline.ErrInterrupt
2015-09-25 20:06:47 +03:00
break
}
println(line)
}
```
2015-09-23 08:32:43 +03:00
2015-09-23 08:09:26 +03:00
# Shortcut
2015-09-23 08:03:18 +03:00
2015-09-23 08:19:53 +03:00
`Meta`+`B` means press `Esc` and `n` separately.
2015-10-01 17:44:43 +03:00
Users can change that in terminal simulator(i.e. iTerm2) to `Alt`+`B`
2015-09-30 11:13:36 +03:00
Notice: `Meta`+`B` is equals with `Alt`+`B` in windows.
2015-09-23 08:03:18 +03:00
2015-09-26 19:32:04 +03:00
* Shortcut in normal mode
| Shortcut | Comment |
2015-09-26 19:33:32 +03:00
|--------------------|------------------------------------------|
2015-09-26 19:32:04 +03:00
| `Ctrl`+`A` | Beginning of line |
| `Ctrl`+`B` / `←` | Backward one character |
| `Meta`+`B` | Backward one word |
| `Ctrl`+`C` | Send io.EOF |
| `Ctrl`+`D` | Delete one character |
| `Meta`+`D` | Delete one word |
| `Ctrl`+`E` | End of line |
| `Ctrl`+`F` / `→` | Forward one character |
| `Meta`+`F` | Forward one word |
| `Ctrl`+`G` | Cancel |
| `Ctrl`+`H` | Delete previous character |
| `Ctrl`+`I` / `Tab` | Command line completion |
| `Ctrl`+`J` | Line feed |
| `Ctrl`+`K` | Cut text to the end of line |
| `Ctrl`+`L` | Clean screen (TODO) |
| `Ctrl`+`M` | Same as Enter key |
| `Ctrl`+`N` / `↓` | Next line (in history) |
| `Ctrl`+`P` / `↑` | Prev line (in history) |
| `Ctrl`+`R` | Search backwards in history |
| `Ctrl`+`S` | Search forwards in history |
| `Ctrl`+`T` | Transpose characters |
| `Meta`+`T` | Transpose words (TODO) |
2015-09-28 06:13:39 +03:00
| `Ctrl`+`U` | Cut text to the beginning of line |
2015-09-26 19:32:04 +03:00
| `Ctrl`+`W` | Cut previous word |
| `Backspace` | Delete previous character |
| `Meta`+`Backspace` | Cut previous word |
| `Enter` | Line feed |
* Shortcut in Search Mode (`Ctrl`+`S` or `Ctrl`+`r` to enter this mode)
| Shortcut | Comment |
2015-09-26 19:33:32 +03:00
|-------------------------|---------------------------------------------|
2015-09-26 19:32:04 +03:00
| `Ctrl`+`S` | Search forwards in history |
| `Ctrl`+`R` | Search backwards in history |
| `Ctrl`+`C` / `Ctrl`+`G` | Exit Search Mode and revert the history |
2015-09-26 19:36:35 +03:00
| `Backspace` | Delete previous character |
2015-09-26 19:32:04 +03:00
| Other | Exit Search Mode |
* Shortcut in Complete Select Mode (double `Tab` to enter this mode)
| Shortcut | Comment |
2015-09-26 19:33:32 +03:00
|-------------------------|---------------------------------------------|
2015-09-26 19:32:04 +03:00
| `Ctrl`+`F` | Move Forward |
| `Ctrl`+`B` | Move Backward |
| `Ctrl`+`N` | Move to next line |
| `Ctrl`+`P` | Move to previous line |
| `Ctrl`+`A` | Move to the first candicate in current line |
| `Ctrl`+`E` | Move to the last candicate in current line |
| `Tab` / `Enter` | Use the word on cursor to complete |
| `Ctrl`+`C` / `Ctrl`+`G` | Exit Complete Select Mode |
| Other | Exit Complete Select Mode |
2015-09-23 08:43:48 +03:00
2015-09-27 04:05:04 +03:00
# Tested with
2015-09-27 05:12:15 +03:00
| Environment | $TERM |
|-------------------------------|--------|
| Mac OS X iTerm2 | xterm |
| Mac OS X default Terminal.app | xterm |
| Mac OS X iTerm2 Screen | screen |
| Mac OS X iTerm2 Tmux | screen |
| Ubuntu Server 14.04 LTS | linux |
| Centos 7 | linux |
2015-09-29 18:35:11 +03:00
| Windows 10 | - |
2015-09-27 05:12:15 +03:00
### Notice:
2015-09-30 11:13:36 +03:00
* `Ctrl`+`A` is not working in `screen` because it used as a control command by default
2015-09-27 04:05:04 +03:00
If you test it otherwhere, whether it works fine or not, please let me know!
2016-02-24 17:52:02 +03:00
## Who is using Readline
* [cockroachdb/cockroach](https://github.com/cockroachdb/cockroach)
* [youtube/doorman](https://github.com/youtube/doorman)
* [bom-d-van/harp](https://github.com/bom-d-van/harp)
* [abiosoft/ishell](https://github.com/abiosoft/ishell)
* [robertkrimen/otto](https://github.com/robertkrimen/otto)
2015-09-23 08:43:48 +03:00
# Feedback
2015-09-30 11:13:36 +03:00
If you have any questions, please submit a github issue and any pull requests is welcomed :)
2015-11-08 09:23:29 +03:00
* [https://twitter.com/chzyer](https://twitter.com/chzyer)
* [http://weibo.com/2145262190](http://weibo.com/2145262190)
# Backers
Love Readline? Help me keep it alive by donating funds to cover project expenses!<br />
[[Become a backer](https://opencollective.com/readline)]
<a href="https://opencollective.com/readline/backers/0/website" target="_blank">
<img src="https://opencollective.com/readline/backers/0/avatar">
</a>
<a href="https://opencollective.com/readline/backers/1/website" target="_blank">
<img src="https://opencollective.com/readline/backers/1/avatar">
</a>
<a href="https://opencollective.com/readline/backers/2/website" target="_blank">
<img src="https://opencollective.com/readline/backers/2/avatar">
</a>
<a href="https://opencollective.com/readline/backers/3/website" target="_blank">
<img src="https://opencollective.com/readline/backers/3/avatar">
</a>
<a href="https://opencollective.com/readline/backers/4/website" target="_blank">
<img src="https://opencollective.com/readline/backers/4/avatar">
</a>
<a href="https://opencollective.com/readline/backers/5/website" target="_blank">
<img src="https://opencollective.com/readline/backers/5/avatar">
</a>
<a href="https://opencollective.com/readline/backers/6/website" target="_blank">
<img src="https://opencollective.com/readline/backers/6/avatar">
</a>
<a href="https://opencollective.com/readline/backers/7/website" target="_blank">
<img src="https://opencollective.com/readline/backers/7/avatar">
</a>
<a href="https://opencollective.com/readline/backers/8/website" target="_blank">
<img src="https://opencollective.com/readline/backers/8/avatar">
</a>
<a href="https://opencollective.com/readline/backers/9/website" target="_blank">
<img src="https://opencollective.com/readline/backers/9/avatar">
</a>
<a href="https://opencollective.com/readline/backers/10/website" target="_blank">
<img src="https://opencollective.com/readline/backers/10/avatar">
</a>
<a href="https://opencollective.com/readline/backers/11/website" target="_blank">
<img src="https://opencollective.com/readline/backers/11/avatar">
</a>
<a href="https://opencollective.com/readline/backers/12/website" target="_blank">
<img src="https://opencollective.com/readline/backers/12/avatar">
</a>
<a href="https://opencollective.com/readline/backers/13/website" target="_blank">
<img src="https://opencollective.com/readline/backers/13/avatar">
</a>
<a href="https://opencollective.com/readline/backers/14/website" target="_blank">
<img src="https://opencollective.com/readline/backers/14/avatar">
</a>
<a href="https://opencollective.com/readline/backers/15/website" target="_blank">
<img src="https://opencollective.com/readline/backers/15/avatar">
</a>
<a href="https://opencollective.com/readline/backers/16/website" target="_blank">
<img src="https://opencollective.com/readline/backers/16/avatar">
</a>
<a href="https://opencollective.com/readline/backers/17/website" target="_blank">
<img src="https://opencollective.com/readline/backers/17/avatar">
</a>
<a href="https://opencollective.com/readline/backers/18/website" target="_blank">
<img src="https://opencollective.com/readline/backers/18/avatar">
</a>
<a href="https://opencollective.com/readline/backers/19/website" target="_blank">
<img src="https://opencollective.com/readline/backers/19/avatar">
</a>
<a href="https://opencollective.com/readline/backers/20/website" target="_blank">
<img src="https://opencollective.com/readline/backers/20/avatar">
</a>
<a href="https://opencollective.com/readline/backers/21/website" target="_blank">
<img src="https://opencollective.com/readline/backers/21/avatar">
</a>
<a href="https://opencollective.com/readline/backers/22/website" target="_blank">
<img src="https://opencollective.com/readline/backers/22/avatar">
</a>
<a href="https://opencollective.com/readline/backers/23/website" target="_blank">
<img src="https://opencollective.com/readline/backers/23/avatar">
</a>
<a href="https://opencollective.com/readline/backers/24/website" target="_blank">
<img src="https://opencollective.com/readline/backers/24/avatar">
</a>
<a href="https://opencollective.com/readline/backers/25/website" target="_blank">
<img src="https://opencollective.com/readline/backers/25/avatar">
</a>
<a href="https://opencollective.com/readline/backers/26/website" target="_blank">
<img src="https://opencollective.com/readline/backers/26/avatar">
</a>
<a href="https://opencollective.com/readline/backers/27/website" target="_blank">
<img src="https://opencollective.com/readline/backers/27/avatar">
</a>
<a href="https://opencollective.com/readline/backers/28/website" target="_blank">
<img src="https://opencollective.com/readline/backers/28/avatar">
</a>
<a href="https://opencollective.com/readline/backers/29/website" target="_blank">
<img src="https://opencollective.com/readline/backers/29/avatar">
</a>