Update options
This commit is contained in:
parent
6121408e08
commit
16ecc66e9b
54
README.md
54
README.md
|
@ -1,10 +1,19 @@
|
|||
# go-prompt-toolkit
|
||||
|
||||
Library for building powerful interactive command lines in Golang.
|
||||
|
||||
![demo](./_resources/demo.gif)
|
||||
|
||||
## Usage
|
||||
Library for building powerful interactive command lines in Golang.
|
||||
|
||||
#### 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
|
||||
|
||||
* kube-prompt : This is available soon...
|
||||
|
||||
## Getting Started
|
||||
|
||||
```go
|
||||
package main
|
||||
|
@ -45,28 +54,23 @@ func main() {
|
|||
|
||||
![options](./_resources/prompt-options.png)
|
||||
|
||||
#### `ParserOption(x ConsoleParser)`
|
||||
#### `WriterOption(x ConsoleWriter)`
|
||||
#### `TitleOption(x string)`
|
||||
#### `PrefixOption(x string)`
|
||||
#### `PrefixColorOption(x string)`
|
||||
#### `CompletionTextColor(x string)`
|
||||
#### `CompletionBackgroundColor(x string)`
|
||||
#### `SelectedCompletionTextColor(x string)`
|
||||
#### `SelectedCompletionBackgroundColor(x string)`
|
||||
#### `MaxCompletionsOption(x uint16)`
|
||||
|
||||
|
||||
## Related projects.
|
||||
|
||||
#### 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
|
||||
|
||||
* kube-prompt : This is available soon...
|
||||
#### `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)`
|
||||
#### `OptionOutputTextColor(x Color)`
|
||||
#### `OptionOutputBGColor(x Color)`
|
||||
#### `OptionSuggestionTextColor(x Color)`
|
||||
#### `OptionSuggestionBGColor(x Color)`
|
||||
#### `OptionSelectedSuggestionTextColor(x Color)`
|
||||
#### `OptionSelectedSuggestionBGColor(x Color)`
|
||||
#### `OptionMaxCompletions(x uint16)`
|
||||
|
||||
|
||||
## LICENSE
|
||||
|
|
|
@ -4,91 +4,119 @@ import "syscall"
|
|||
|
||||
type option func(prompt *Prompt) error
|
||||
|
||||
func ParserOption(x ConsoleParser) option {
|
||||
func OptionParser(x ConsoleParser) option {
|
||||
return func(p *Prompt) error {
|
||||
p.in = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func WriterOption(x ConsoleWriter) option {
|
||||
func OptionWriter(x ConsoleWriter) option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.out = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func TitleOption(x string) option {
|
||||
func OptionTitle(x string) option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.title = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func PrefixOption(x string) option {
|
||||
func OptionPrefix(x string) option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.prefix = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func PrefixColorOption(x Color) option {
|
||||
func OptionPrefixTextColor(x Color) option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.prefixColor = x
|
||||
p.renderer.prefixTextColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func TextColorOption(x Color) option {
|
||||
func OptionPrefixBackgroundColor(x Color) option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.textColor = x
|
||||
p.renderer.prefixBGColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func CompletedTextColorOption(x Color) option {
|
||||
func OptionInputTextColor(x Color) option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.completedTextColor = x
|
||||
p.renderer.inputTextColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func ResultTextColorOption(x Color) option {
|
||||
func OptionInputBGColor(x Color) option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.inputBGColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionPreviewSuggestionTextColor(x Color) option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.previewSuggestionTextColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionPreviewSuggestionBGColor(x Color) option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.previewSuggestionBGColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionOutputTextColor(x Color) option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.outputTextColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func CompletionTextColor(x Color) option {
|
||||
func OptionOutputBGColor(x Color) option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.completionTextColor = x
|
||||
p.renderer.outputBGColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func CompletionBackgroundColor(x Color) option {
|
||||
func OptionSuggestionTextColor(x Color) option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.completionBGColor = x
|
||||
p.renderer.suggestionTextColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func SelectedCompletionTextColor(x Color) option {
|
||||
func OptionSuggestionBGColor(x Color) option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.selectedCompletionTextColor = x
|
||||
p.renderer.suggestionBGColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func SelectedCompletionBackgroundColor(x Color) option {
|
||||
func OptionSelectedSuggestionTextColor(x Color) option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.selectedCompletionBGColor = x
|
||||
p.renderer.selectedSuggestionTextColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func MaxCompletionsOption(x uint16) option {
|
||||
func OptionSelectedSuggestionBGColor(x Color) option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.selectedSuggestionBGColor = x
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func OptionMaxCompletions(x uint16) option {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.maxCompletions = x
|
||||
return nil
|
||||
|
@ -99,16 +127,21 @@ func NewPrompt(executor Executor, completer Completer, opts ...option) *Prompt {
|
|||
pt := &Prompt{
|
||||
in: &VT100Parser{fd: syscall.Stdin},
|
||||
renderer: &Render{
|
||||
prefix: "> ",
|
||||
out: &VT100Writer{fd: syscall.Stdout},
|
||||
prefixColor: Blue,
|
||||
textColor: DefaultColor,
|
||||
outputTextColor: DefaultColor,
|
||||
completedTextColor: Green,
|
||||
completionTextColor: White,
|
||||
completionBGColor: Cyan,
|
||||
selectedCompletionTextColor: Black,
|
||||
selectedCompletionBGColor: Turquoise,
|
||||
prefix: "> ",
|
||||
out: &VT100Writer{fd: syscall.Stdout},
|
||||
prefixTextColor: Blue,
|
||||
prefixBGColor: DefaultColor,
|
||||
inputTextColor: DefaultColor,
|
||||
inputBGColor: DefaultColor,
|
||||
outputTextColor: DefaultColor,
|
||||
outputBGColor: DefaultColor,
|
||||
previewSuggestionTextColor: Green,
|
||||
previewSuggestionBGColor: DefaultColor,
|
||||
suggestionTextColor: White,
|
||||
suggestionBGColor: Cyan,
|
||||
selectedSuggestionTextColor: Black,
|
||||
selectedSuggestionBGColor: Turquoise,
|
||||
maxCompletions: 10,
|
||||
},
|
||||
buf: NewBuffer(),
|
||||
executor: executor,
|
||||
|
|
|
@ -8,14 +8,18 @@ type Render struct {
|
|||
col uint16
|
||||
maxCompletions uint16
|
||||
// colors
|
||||
prefixColor Color
|
||||
textColor Color
|
||||
prefixTextColor Color
|
||||
prefixBGColor Color
|
||||
inputTextColor Color
|
||||
inputBGColor Color
|
||||
outputTextColor Color
|
||||
completedTextColor Color
|
||||
completionTextColor Color
|
||||
completionBGColor Color
|
||||
selectedCompletionTextColor Color
|
||||
selectedCompletionBGColor Color
|
||||
outputBGColor Color
|
||||
previewSuggestionTextColor Color
|
||||
previewSuggestionBGColor Color
|
||||
suggestionTextColor Color
|
||||
suggestionBGColor Color
|
||||
selectedSuggestionTextColor Color
|
||||
selectedSuggestionBGColor Color
|
||||
}
|
||||
|
||||
func (r *Render) Setup() {
|
||||
|
@ -27,7 +31,7 @@ func (r *Render) Setup() {
|
|||
}
|
||||
|
||||
func (r *Render) renderPrefix() {
|
||||
r.out.SetColor(r.prefixColor, DefaultColor)
|
||||
r.out.SetColor(r.prefixTextColor, r.prefixBGColor)
|
||||
r.out.WriteStr(r.prefix)
|
||||
r.out.SetColor(DefaultColor, DefaultColor)
|
||||
}
|
||||
|
@ -79,9 +83,9 @@ func (r *Render) renderCompletion(buf *Buffer, words []string, chosen int) {
|
|||
for i := 0; i < l; i++ {
|
||||
r.out.CursorDown(1)
|
||||
if i == chosen {
|
||||
r.out.SetColor(r.selectedCompletionTextColor, r.selectedCompletionBGColor)
|
||||
r.out.SetColor(r.selectedSuggestionTextColor, r.selectedSuggestionBGColor)
|
||||
} else {
|
||||
r.out.SetColor(r.completionTextColor, r.completionBGColor)
|
||||
r.out.SetColor(r.suggestionTextColor, r.suggestionBGColor)
|
||||
}
|
||||
r.out.WriteStr(" " + formatted[i] + " ")
|
||||
r.out.SetColor(White, DarkGray)
|
||||
|
@ -107,7 +111,7 @@ func (r *Render) Erase(buffer *Buffer) {
|
|||
|
||||
func (r *Render) Render(buffer *Buffer, completions []string, chosen int) {
|
||||
line := buffer.Document().CurrentLine()
|
||||
r.out.SetColor(r.textColor, DefaultColor)
|
||||
r.out.SetColor(r.inputTextColor, r.inputBGColor)
|
||||
r.out.WriteStr(line)
|
||||
r.out.SetColor(DefaultColor, DefaultColor)
|
||||
r.out.CursorBackward(len(line) - buffer.CursorPosition)
|
||||
|
@ -115,7 +119,7 @@ func (r *Render) Render(buffer *Buffer, completions []string, chosen int) {
|
|||
if chosen != -1 {
|
||||
c := completions[chosen]
|
||||
r.out.CursorBackward(len([]rune(buffer.Document().GetWordBeforeCursor())))
|
||||
r.out.SetColor(r.completedTextColor, DefaultColor)
|
||||
r.out.SetColor(r.previewSuggestionTextColor, r.previewSuggestionBGColor)
|
||||
r.out.WriteStr(c)
|
||||
r.out.SetColor(DefaultColor, DefaultColor)
|
||||
}
|
||||
|
@ -123,9 +127,9 @@ func (r *Render) Render(buffer *Buffer, completions []string, chosen int) {
|
|||
}
|
||||
|
||||
func (r *Render) BreakLine(buffer *Buffer, result string) {
|
||||
r.out.SetColor(r.textColor, DefaultColor)
|
||||
r.out.SetColor(r.inputTextColor, r.inputBGColor)
|
||||
r.out.WriteStr(buffer.Document().Text + "\n")
|
||||
r.out.SetColor(r.outputTextColor, DefaultColor)
|
||||
r.out.SetColor(r.outputTextColor, r.outputBGColor)
|
||||
r.out.WriteStr(result + "\n")
|
||||
r.out.SetColor(DefaultColor, DefaultColor)
|
||||
r.renderPrefix()
|
||||
|
|
Loading…
Reference in New Issue