Add PrefixColorOption

This commit is contained in:
c-bata 2017-07-17 02:11:52 +09:00
parent 580cdb454e
commit afa85b3f5c
3 changed files with 18 additions and 8 deletions

View File

@ -50,6 +50,7 @@ func main() {
completer,
prompt.MaxCompletionsOption(8),
prompt.PrefixOption(">>> "),
prompt.PrefixColorOption("blue"),
prompt.TitleOption("Hello! this is prompt toolkit"),
)
defer fmt.Println("\nGoodbye!")

View File

@ -27,7 +27,14 @@ func TitleOption(x string) option {
func PrefixOption(x string) option {
return func(p *Prompt) error {
p.renderer.Prefix = x
p.renderer.prefix = x
return nil
}
}
func PrefixColorOption(x string) option {
return func(p *Prompt) error {
p.renderer.prefixColor = x
return nil
}
}
@ -43,8 +50,9 @@ func NewPrompt(executor Executor, completer Completer, opts ...option) *Prompt {
pt := &Prompt{
in: &VT100Parser{fd: syscall.Stdin},
renderer: &Render{
Prefix: "> ",
out: &VT100Writer{fd: syscall.Stdout},
prefix: "> ",
prefixColor: "green",
out: &VT100Writer{fd: syscall.Stdout},
},
title: "",
buf: NewBuffer(),

View File

@ -1,7 +1,8 @@
package prompt
type Render struct {
Prefix string
prefix string
prefixColor string
Title string
out ConsoleWriter
row uint16
@ -18,8 +19,8 @@ func (r *Render) Setup() {
}
func (r *Render) renderPrefix() {
r.out.SetColor("green", "default")
r.out.WriteStr(r.Prefix)
r.out.SetColor(r.prefixColor, "default")
r.out.WriteStr(r.prefix)
r.out.SetColor("default", "default")
}
@ -56,11 +57,11 @@ func (r *Render) renderCompletion(buf *Buffer, words []string, chosen int) {
}
}
formatted, width := formatCompletions(words, int(r.col) - len(r.Prefix) - 3)
formatted, width := formatCompletions(words, int(r.col) - len(r.prefix) - 3)
l := len(formatted)
r.prepareArea(l)
d := (len(r.Prefix) + len(buf.Document().TextBeforeCursor())) % int(r.col)
d := (len(r.prefix) + len(buf.Document().TextBeforeCursor())) % int(r.col)
if d + width + 3 > int(r.col) {
r.out.CursorBackward(d + width + 3 - int(r.col))
}