Add PrefixColorOption
This commit is contained in:
parent
580cdb454e
commit
afa85b3f5c
1
main.go
1
main.go
|
@ -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!")
|
||||
|
|
|
@ -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(),
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue