Remove output options

This commit is contained in:
c-bata 2017-08-06 15:31:44 +09:00
parent 44eda9ed4e
commit ad17465489
6 changed files with 23 additions and 45 deletions

View File

@ -21,21 +21,29 @@ package main
import (
"context"
"fmt"
"time"
"github.com/c-bata/go-prompt-toolkit/prompt"
)
// executor executes command and return the output string.
// executor executes command and print the output.
// 1. Execute sql
// 2. Get response and return output
func executor(ctx context.Context, sql string) string {
// 2. Get response and print it
func executor(ctx context.Context, sql string) {
res := "something response from db."
return res // this is printed in console.
fmt.Println(res)
return
}
// completer returns the completion items from user input.
func completer(sql string) []string {
return []string{"users", "articles", "comments", "groups", "tags"}
func completer(sql string) []prompt.Suggest {
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"},
}
}
func main() {
@ -66,8 +74,6 @@ func main() {
#### `OptionInputBGColor(x Color)`
#### `OptionPreviewSuggestionTextColor(x Color)`
#### `OptionPreviewSuggestionBGColor(x Color)`
#### `OptionOutputTextColor(x Color)`
#### `OptionOutputBGColor(x Color)`
#### `OptionSuggestionTextColor(x Color)`
#### `OptionSuggestionBGColor(x Color)`
#### `OptionSelectedSuggestionTextColor(x Color)`

View File

@ -7,8 +7,8 @@ import (
"github.com/c-bata/go-prompt-toolkit"
)
func executor(ctx context.Context, t string) string {
return fmt.Sprintln("Your input: " + t)
func executor(ctx context.Context, t string) {
fmt.Println("Your input: " + t)
}
func completer(t string) []prompt.Suggest {
@ -26,7 +26,6 @@ func main() {
completer,
prompt.OptionPrefix(">>> "),
prompt.OptionTitle("sqlite3-cli"),
prompt.OptionOutputTextColor(prompt.DarkGray),
)
defer fmt.Println("\nGoodbye!")
pt.Run()

View File

@ -9,7 +9,7 @@ import (
"github.com/c-bata/go-prompt-toolkit"
)
func executor(ctx context.Context, t string) string {
func executor(ctx context.Context, t string) {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
@ -22,7 +22,7 @@ func executor(ctx context.Context, t string) string {
cmd.Run()
fmt.Println("Foo")
}
return ""
return
}
func completer(t string) []prompt.Suggest {
@ -36,7 +36,6 @@ func main() {
pt := prompt.NewPrompt(
executor,
completer,
prompt.OptionOutputTextColor(prompt.DarkGray),
)
defer fmt.Println("\nGoodbye!")
pt.Run()

View File

@ -74,20 +74,6 @@ func OptionPreviewSuggestionBGColor(x Color) option {
}
}
func OptionOutputTextColor(x Color) option {
return func(p *Prompt) error {
p.renderer.outputTextColor = x
return nil
}
}
func OptionOutputBGColor(x Color) option {
return func(p *Prompt) error {
p.renderer.outputBGColor = x
return nil
}
}
func OptionSuggestionTextColor(x Color) option {
return func(p *Prompt) error {
p.renderer.suggestionTextColor = x
@ -161,8 +147,6 @@ func NewPrompt(executor Executor, completer Completer, opts ...option) *Prompt {
prefixBGColor: DefaultColor,
inputTextColor: DefaultColor,
inputBGColor: DefaultColor,
outputTextColor: DefaultColor,
outputBGColor: DefaultColor,
previewSuggestionTextColor: Green,
previewSuggestionBGColor: DefaultColor,
suggestionTextColor: White,

View File

@ -15,7 +15,7 @@ const (
envEnableLog = "GO_PROMPT_ENABLE_LOG"
)
type Executor func(context.Context, string) string
type Executor func(context.Context, string)
type Completer func(string) []Suggest
type Prompt struct {
@ -93,16 +93,16 @@ func (p *Prompt) Run() {
}
func (p *Prompt) runExecutor(exec *Exec, bufCh chan []byte) {
resCh := make(chan string, 1)
resCh := make(chan struct{}, 1)
ctx, cancel := context.WithCancel(exec.Context())
go func() {
resCh <- p.executor(ctx, exec.input)
p.executor(ctx, exec.input)
resCh <- struct{}{}
}()
for {
select {
case r := <-resCh:
p.renderer.RenderResult(r)
case <-resCh:
return
case b := <-bufCh:
if p.in.GetKey(b) == ControlC {

View File

@ -23,8 +23,6 @@ type Render struct {
prefixBGColor Color
inputTextColor Color
inputBGColor Color
outputTextColor Color
outputBGColor Color
previewSuggestionTextColor Color
previewSuggestionBGColor Color
suggestionTextColor Color
@ -179,14 +177,6 @@ func (r *Render) BreakLine(buffer *Buffer) {
r.out.Flush()
}
func (r *Render) RenderResult(result string) {
if result != "" {
r.out.SetColor(r.outputTextColor, r.outputBGColor, false)
r.out.WriteRawStr(result)
}
r.out.SetColor(DefaultColor, DefaultColor, false)
}
func formatCompletions(completions []Suggest, max int) (new []Suggest, width int) {
num := len(completions)
new = make([]Suggest, num)