go-prompt/README.md

151 lines
4.0 KiB
Markdown
Raw Normal View History

2017-08-07 15:19:51 +00:00
# go-prompt
2017-07-03 14:36:56 +00:00
2017-07-17 13:32:13 +00:00
![demo](./_resources/demo.gif)
2017-08-07 15:19:51 +00:00
Library for building powerful interactive prompt in Go, inspired by python-prompt-toolkit.
2017-07-03 14:36:56 +00:00
2017-07-17 13:32:13 +00:00
#### Similar Projects
2017-08-07 15:19:51 +00:00
* [jonathanslenders/python-prompt-toolkit](https://github.com/jonathanslenders/python-prompt-toolkit): **go-prompt** is inspired by this library.
2017-07-17 13:32:13 +00:00
* [peterh/liner](https://github.com/peterh/liner): The most similar project in golang is **liner** that I've ever seen.
2017-08-07 15:19:51 +00:00
#### Projects using go-prompt
2017-07-17 13:32:13 +00:00
2017-08-07 15:19:51 +00:00
* [kube-prompt : An interactive kubernetes client featuring auto-complete written in Go.](https://github.com/c-bata/kube-prompt)
2017-07-16 18:41:00 +00:00
2017-07-17 13:32:13 +00:00
## Getting Started
2017-07-16 18:41:00 +00:00
```go
package main
import (
2017-08-07 15:19:51 +00:00
"fmt"
2017-07-23 15:35:13 +00:00
2017-08-07 15:19:51 +00:00
"github.com/c-bata/go-prompt"
2017-07-16 18:41:00 +00:00
)
2017-08-06 06:31:44 +00:00
// executor executes command and print the output.
2017-08-07 15:19:51 +00:00
func executor(in string) {
fmt.Println("Your input: " + in)
2017-07-16 18:41:00 +00:00
}
2017-07-17 05:49:41 +00:00
// completer returns the completion items from user input.
2017-08-07 09:48:33 +00:00
func completer(in string) []prompt.Suggest {
2017-08-07 15:19:51 +00:00
s := []prompt.Suggest{
{Text: "users", Description: "user table"},
{Text: "sites", Description: "sites table"},
{Text: "articles", Description: "articles table"},
{Text: "comments", Description: "comments table"},
}
return prompt.FilterHasPrefix(s, in, true)
2017-07-16 18:41:00 +00:00
}
func main() {
2017-08-07 15:19:51 +00:00
p := prompt.New(
executor,
completer,
prompt.OptionPrefix(">>> "),
prompt.OptionTitle("sql-prompt"),
)
p.Run()
2017-07-16 18:41:00 +00:00
}
```
2017-08-09 03:50:16 +00:00
## Color Options
2017-07-03 14:36:56 +00:00
2017-08-09 03:50:16 +00:00
go-prompt has many color options.
It is difficult to describe by text. So please see below:
2017-07-16 17:58:51 +00:00
![options](./_resources/prompt-options.png)
2017-08-09 03:50:16 +00:00
* **OptionPrefixTextColor(prompt.Color)** : default `prompt.Blue`
* **OptionPrefixBackgroundColor(prompt.Color)** : default `prompt.DefaultColor`
* **OptionInputTextColor(prompt.Color)** : default `prompt.DefaultColor`
* **OptionInputBGColor(prompt.Color)** : default `prompt.DefaultColor`
2017-08-09 06:35:11 +00:00
* **OptionPreviewSuggestionTextColor(prompt.Color)** : default `prompt.Green`
2017-08-09 03:50:16 +00:00
* **OptionPreviewSuggestionBGColor(prompt.Color)** : default `prompt.DefaultColor`
2017-08-09 06:35:11 +00:00
* **OptionSuggestionTextColor(prompt.Color)** : default `prompt.White`
* **OptionSuggestionBGColor(prompt.Color)** : default `prompt.Cyan`
* **OptionSelectedSuggestionTextColor(prompt.Color)** : `default prompt.Black`
2017-08-09 03:50:16 +00:00
* **OptionSelectedSuggestionBGColor(prompt.Color)** : `default prompt.DefaultColor`
2017-08-09 06:35:11 +00:00
* **OptionDescriptionTextColor(prompt.Color)** : default `prompt.Black`
* **OptionDescriptionBGColor(prompt.Color)** : default `prompt.Turquoise`
* **OptionSelectedDescriptionTextColor(prompt.Color)** : default `prompt.White`
* **OptionSelectedDescriptionBGColor(prompt.Color)** : default `prompt.Cyan`
2017-07-16 18:41:00 +00:00
2017-08-09 03:50:16 +00:00
## Other Options
2017-07-16 17:20:59 +00:00
2017-08-09 03:50:16 +00:00
#### `OptionTitle(string)` : default `""`
Option to set title displayed at the header bar of terminal.
#### `OptionHistory([]string)` : default `[]string{}`
Option to set history.
#### `OptionPrefix(string)` : default `"> "`
Option to set prefix string.
#### `OptionMaxSuggestions(x uint16)` : default `6`
The max number of displayed suggestions.
#### `OptionParser(prompt.ConsoleParser)` : default `VT100Parser`
To set a custom ConsoleParser object.
An argument should implement ConsoleParser interface.
#### `OptionWriter(prompt.ConsoleWriter)` : default `VT100Writer`
To set a custom ConsoleWriter object.
An argument should implement ConsoleWriter interace.
## `go-prompt` internals
This is a short description of go-prompt implementation.
go-prompt consists of three parts.
1. Input parser
2. Emulate user input with Buffer object.
3. Render buffer object.
### Input Parser
![input-parser animation](./_resources/input-parser.gif)
Input Parser only supports only vt100 compatible console now.
* Set raw mode.
* Read standard input.
* Parse byte array
### Emulate user input
go-prompt contains Buffer class.
It represents input state by handling user input key.
`Buffer` object has text and cursor position.
2017-07-03 14:36:56 +00:00
2017-08-09 03:50:16 +00:00
```go
package main
import "github.com/c-bata/go-prompt"
func main() {
b := prompt.NewBuffer()
... wip
}
2017-07-03 14:36:56 +00:00
```
2017-08-09 03:50:16 +00:00
### Render Buffer object.
`Renderer` object renders a buffer object.
```go
package main
2017-07-03 14:36:56 +00:00
```
2017-08-09 03:50:16 +00:00
the output is below:
**TODO this is screen shot**
## LICENSE
This software is licensed under the MIT License (See [LICENSE](./LICENSE) ).