diff --git a/DEVELOPER_GUIDE.md b/DEVELOPER_GUIDE.md deleted file mode 100644 index 44f2c55..0000000 --- a/DEVELOPER_GUIDE.md +++ /dev/null @@ -1,155 +0,0 @@ -# Developer Guide - -## Getting Started - -The most simple example is below. - -```go -package main - -import ( - "fmt" - - "github.com/c-bata/go-prompt" -) - -// executor executes command and print the output. -func executor(in string) { - fmt.Println("Your input: " + in) -} - -// completer returns the completion items from user input. -func completer(d prompt.Document) []prompt.Suggest { - 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, d.GetWordBeforeCursor(), true) -} - -func main() { - p := prompt.New( - executor, - completer, - prompt.OptionPrefix(">>> "), - prompt.OptionTitle("sql-prompt"), - ) - p.Run() -} -``` - -If you want to create CLI using go-prompt, I recommend you to look at the [source code of kube-prompt](https://github.com/c-bata/kube-prompt). -It is the most practical example. - - -## Options - -go-prompt has many color options. -It is difficult to describe by text. So please see below figure: - -![options](https://github.com/c-bata/assets/raw/master/go-prompt/prompt-options.png) - -* **OptionPrefixTextColor(prompt.Color)** : default `prompt.Blue` -* **OptionPrefixBackgroundColor(prompt.Color)** : default `prompt.DefaultColor` -* **OptionInputTextColor(prompt.Color)** : default `prompt.DefaultColor` -* **OptionInputBGColor(prompt.Color)** : default `prompt.DefaultColor` -* **OptionPreviewSuggestionTextColor(prompt.Color)** : default `prompt.Green` -* **OptionPreviewSuggestionBGColor(prompt.Color)** : default `prompt.DefaultColor` -* **OptionSuggestionTextColor(prompt.Color)** : default `prompt.White` -* **OptionSuggestionBGColor(prompt.Color)** : default `prompt.Cyan` -* **OptionSelectedSuggestionTextColor(prompt.Color)** : `default prompt.Black` -* **OptionSelectedSuggestionBGColor(prompt.Color)** : `default prompt.DefaultColor` -* **OptionDescriptionTextColor(prompt.Color)** : default `prompt.Black` -* **OptionDescriptionBGColor(prompt.Color)** : default `prompt.Turquoise` -* **OptionSelectedDescriptionTextColor(prompt.Color)** : default `prompt.White` -* **OptionSelectedDescriptionBGColor(prompt.Color)** : default `prompt.Cyan` -* **OptionScrollbarThumbColor** : `prompt.DarkGray` -* **OptionScrollbarBGColor** : `prompt.Cyan` - -**Other Options** - -#### `OptionTitle(string)` : default `""` -Option to set a title that wll be displayed on the header bar of terminal. - -#### `OptionHistory([]string)` : default `[]string{}` -Option to set history. - -#### `OptionPrefix(string)` : default `"> "` -Option to set prefix string. - -#### `OptionLivePrefix(func() (prefix string, useLivePrefix bool))` : default `nil` -Option to set a callback function for updating prefix string dynamically. - -#### `OptionMaxSuggestions(x uint16)` : default `6` -The max number of displayed suggestions. - -#### `OptionParser(prompt.ConsoleParser)` : default `VT100Parser` -To set a custom ConsoleParser object. -The argument should implement ConsoleParser interface. - -#### `OptionWriter(prompt.ConsoleWriter)` : default `VT100Writer` -To set a custom ConsoleWriter object. -The argument should implement ConsoleWriter interface. - -#### `SwitchKeyBindMode(prompt.KeyBindMode)` : default `prompt.EmacsKeyBindMode` -To set a key bind mode. - -#### `OptionAddKeyBind(...KeyBind)` : default `[]KeyBind{}` -To set a custom key bind. - -## Architecture of go-prompt - -*Caution: This section is WIP.* - -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](https://github.com/c-bata/assets/raw/master/go-prompt/input-parser.gif) - -Input Parser only supports vt100-compatible console now. - -* Set as the raw mode. -* Read a standard input. -* Parse to byte array - -### Emulate user input - -go-prompt contains Buffer class. -It represents input state by handling a key input by user. - -`Buffer` object has text and cursor position. - -**TODO prepare the sample of buffer** - -```go -package main - -import "github.com/c-bata/go-prompt" - -func main() { - b := prompt.NewBuffer() - ... wip -} -``` - -### Renderer - -`Renderer` object renders a buffer object. - -**TODO prepare the sample of brender** - -```go -package main -``` - -the output is below: - -**TODO prepare a screen shot** diff --git a/README.md b/README.md index 820fb60..1d7a9ac 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ func main() { ### Flexible options -go-prompt provides many options. All options are listed in [Developer Guide](./DEVELOPER_GUIDE.md). +go-prompt provides many options. Please check [option section of GoDoc](https://godoc.org/github.com/c-bata/go-prompt#Option) for more details. [![options](https://github.com/c-bata/assets/raw/master/go-prompt/prompt-options.png)](#flexible-options) @@ -92,7 +92,6 @@ We confirmed go-prompt works fine on following terminals: ## Links -* [Developer Guide](./DEVELOPER_GUIDE.md). * [Change Log](./CHANGELOG.md) * [GoDoc](http://godoc.org/github.com/c-bata/go-prompt). diff --git a/_example/README.md b/_example/README.md new file mode 100644 index 0000000..7fcc607 --- /dev/null +++ b/_example/README.md @@ -0,0 +1,9 @@ +# Examples of go-prompt + +This directory includes some examples using go-prompt. + +* `echo` : just return your input +* `exec-command` : Run another CLI tool via `os/exec` package. + * More practical example is [source code of kube-prompt](https://github.com/c-bata/kube-prompt). I recommend you to look this if you want to create tools like kube-prompt. +* `input` : example of `prompt.Input` +* `live-prefix` : Change your prefix string dynamically. diff --git a/_example/sleep/main.go b/_example/sleep/main.go deleted file mode 100644 index b83a467..0000000 --- a/_example/sleep/main.go +++ /dev/null @@ -1,40 +0,0 @@ -package main - -import ( - "context" - "fmt" - "os/exec" - "time" - - "github.com/c-bata/go-prompt" -) - -func executor(t string) { - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() - - if t == "sleep 5s" { - cmd := exec.CommandContext(ctx, "sleep", "5") - cmd.Run() - } else if t == "sleep 20s" { - cmd := exec.CommandContext(ctx, "sleep", "20") - cmd.Run() - } - return -} - -func completer(t prompt.Document) []prompt.Suggest { - return []prompt.Suggest{ - {Text: "sleep 5s"}, - {Text: "sleep 20s"}, - } -} - -func main() { - p := prompt.New( - executor, - completer, - ) - defer fmt.Println("Goodbye!") - p.Run() -}