go-prompt/README.md

112 lines
3.6 KiB
Markdown
Raw Normal View History

2017-07-03 14:36:56 +00:00
# go-prompt-toolkit
2017-07-17 13:32:13 +00:00
![demo](./_resources/demo.gif)
2017-07-03 14:36:56 +00:00
Library for building powerful interactive command lines in Golang.
2017-07-17 13:32:13 +00:00
#### Similar Projects
* [jonathanslenders/python-prompt-toolkit](https://github.com/jonathanslenders/python-prompt-toolkit): **go-prompt-toolkit** is inspired by this library.
* [peterh/liner](https://github.com/peterh/liner): The most similar project in golang is **liner** that I've ever seen.
#### Projects using go-prompt-toolkit
2017-07-30 07:47:49 +00:00
* [kube-prompt : An interactive kubernetes client featuring autocomplete using prompt-toolkit.](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 (
"fmt"
2017-08-06 06:31:44 +00:00
"time"
2017-07-23 15:35:13 +00:00
2017-07-16 18:41:00 +00:00
"github.com/c-bata/go-prompt-toolkit/prompt"
)
2017-08-06 06:31:44 +00:00
// executor executes command and print the output.
2017-07-17 05:49:41 +00:00
// 1. Execute sql
2017-08-06 06:31:44 +00:00
// 2. Get response and print it
2017-08-07 09:48:33 +00:00
func executor(in string) {
out := "something response from db."
fmt.Println(out)
2017-08-06 06:31:44 +00:00
return
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-06 06:31:44 +00:00
return []primpt.Suggest{
{Text: "users", Description: "user collections."},
{Text: "articles", Description: "article is posted by users."},
{Text: "comments", Description: "comment is inserted with each articles."},
{Text: "groups", Description: "group is the collection of users."},
{Text: "tags", Description: "tag contains hash tag like #prompt"},
}
2017-07-16 18:41:00 +00:00
}
func main() {
pt := prompt.NewPrompt(
executor,
completer,
2017-07-17 05:49:41 +00:00
prompt.OptionTitle("sqlite3-prompt"),
prompt.OptionPrefix(">>> "),
2017-07-23 15:35:13 +00:00
prompt.OptionPrefixColor(prompt.Blue),
2017-07-16 18:41:00 +00:00
)
defer fmt.Println("\nGoodbye!")
pt.Run()
}
```
2017-07-03 14:36:56 +00:00
2017-07-16 17:58:51 +00:00
## Options
![options](./_resources/prompt-options.png)
2017-07-17 13:32:13 +00:00
#### `OptionParser(x ConsoleParser)`
#### `OptionWriter(x ConsoleWriter)`
#### `OptionTitle(x string)`
#### `OptionPrefix(x string)`
#### `OptionPrefixTextColor(x Color)`
#### `OptionPrefixBackgroundColor(x Color)`
#### `OptionInputTextColor(x Color)`
#### `OptionInputBGColor(x Color)`
#### `OptionPreviewSuggestionTextColor(x Color)`
#### `OptionPreviewSuggestionBGColor(x Color)`
#### `OptionSuggestionTextColor(x Color)`
#### `OptionSuggestionBGColor(x Color)`
#### `OptionSelectedSuggestionTextColor(x Color)`
#### `OptionSelectedSuggestionBGColor(x Color)`
#### `OptionMaxCompletions(x uint16)`
2017-07-18 15:36:16 +00:00
#### `OptionDescriptionTextColor(x Color)`
#### `OptionDescriptionBGColor(x Color)`
#### `OptionSelectedDescriptionTextColor(x Color)`
#### `OptionSelectedDescriptionBGColor(x Color)`
2017-07-16 18:41:00 +00:00
2017-07-16 17:20:59 +00:00
2017-07-03 14:36:56 +00:00
## LICENSE
```
MIT License
2017-07-16 04:51:31 +00:00
Copyright (c) 2017 Masashi SHIBATA
2017-07-03 14:36:56 +00:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```