Improve color options

This commit is contained in:
c-bata 2017-07-17 04:32:42 +09:00
parent 2921adafb3
commit 85830110de
6 changed files with 117 additions and 50 deletions

@ -50,8 +50,10 @@ func main() {
completer, completer,
prompt.MaxCompletionsOption(8), prompt.MaxCompletionsOption(8),
prompt.PrefixOption(">>> "), prompt.PrefixOption(">>> "),
prompt.PrefixColorOption("blue"), prompt.PrefixColorOption(prompt.Blue),
prompt.TitleOption("Hello! this is prompt toolkit"), prompt.TitleOption("SQLITE CLI"),
prompt.CompletionTextColor(prompt.Black),
prompt.SelectedCompletionTextColor(prompt.White),
) )
defer fmt.Println("\nGoodbye!") defer fmt.Println("\nGoodbye!")
fmt.Print("Hello! This is a example appication using prompt-toolkit.\n") fmt.Print("Hello! This is a example appication using prompt-toolkit.\n")

27
prompt/color.go Normal file

@ -0,0 +1,27 @@
package prompt
type Color int
const (
DefaultColor Color = iota
// Low intensity
Black
DarkRed
DarkGreen
Brown
DarkBlue
Purple
Cyan
LightGray
// High intensity
DarkGray
Red
Green
Yellow
Blue
Fuchsia
Turquoise
White
)

@ -58,5 +58,5 @@ type ConsoleWriter interface {
/* colors */ /* colors */
SetColor(fg, bg string) (ok bool) SetColor(fg, bg Color) (ok bool)
} }

@ -32,13 +32,41 @@ func PrefixOption(x string) option {
} }
} }
func PrefixColorOption(x string) option { func PrefixColorOption(x Color) option {
return func(p *Prompt) error { return func(p *Prompt) error {
p.renderer.prefixColor = x p.renderer.prefixColor = x
return nil return nil
} }
} }
func CompletionTextColor(x Color) option {
return func(p *Prompt) error {
p.renderer.completionTextColor = x
return nil
}
}
func CompletionBackgroundColor(x Color) option {
return func(p *Prompt) error {
p.renderer.completionBGColor = x
return nil
}
}
func SelectedCompletionTextColor(x Color) option {
return func(p *Prompt) error {
p.renderer.selectedCompletionTextColor = x
return nil
}
}
func SelectedCompletionBackgroundColor(x Color) option {
return func(p *Prompt) error {
p.renderer.selectedCompletionBGColor = x
return nil
}
}
func MaxCompletionsOption(x uint16) option { func MaxCompletionsOption(x uint16) option {
return func(p *Prompt) error { return func(p *Prompt) error {
p.renderer.maxCompletions = x p.renderer.maxCompletions = x
@ -51,8 +79,12 @@ func NewPrompt(executor Executor, completer Completer, opts ...option) *Prompt {
in: &VT100Parser{fd: syscall.Stdin}, in: &VT100Parser{fd: syscall.Stdin},
renderer: &Render{ renderer: &Render{
prefix: "> ", prefix: "> ",
prefixColor: "green",
out: &VT100Writer{fd: syscall.Stdout}, out: &VT100Writer{fd: syscall.Stdout},
prefixColor: Green,
completionTextColor: White,
completionBGColor: Cyan,
selectedCompletionTextColor: Black,
selectedCompletionBGColor: Turquoise,
}, },
buf: NewBuffer(), buf: NewBuffer(),
executor: executor, executor: executor,

@ -2,12 +2,18 @@ package prompt
type Render struct { type Render struct {
prefix string prefix string
prefixColor string
title string title string
out ConsoleWriter out ConsoleWriter
row uint16 row uint16
col uint16 col uint16
maxCompletions uint16 maxCompletions uint16
// colors
prefixColor Color
textColor Color
completionTextColor Color
completionBGColor Color
selectedCompletionTextColor Color
selectedCompletionBGColor Color
} }
func (r *Render) Setup() { func (r *Render) Setup() {
@ -19,9 +25,9 @@ func (r *Render) Setup() {
} }
func (r *Render) renderPrefix() { func (r *Render) renderPrefix() {
r.out.SetColor(r.prefixColor, "default") r.out.SetColor(r.prefixColor, DefaultColor)
r.out.WriteStr(r.prefix) r.out.WriteStr(r.prefix)
r.out.SetColor("default", "default") r.out.SetColor(DefaultColor, DefaultColor)
} }
func (r *Render) TearDown() { func (r *Render) TearDown() {
@ -66,16 +72,16 @@ func (r *Render) renderCompletion(buf *Buffer, words []string, chosen int) {
r.out.CursorBackward(d + width + 3 - int(r.col)) r.out.CursorBackward(d + width + 3 - int(r.col))
} }
r.out.SetColor("white", "teal") r.out.SetColor(White, Cyan)
for i := 0; i < l; i++ { for i := 0; i < l; i++ {
r.out.CursorDown(1) r.out.CursorDown(1)
if i == chosen { if i == chosen {
r.out.SetColor("black", "turquoise") r.out.SetColor(r.selectedCompletionTextColor, r.selectedCompletionBGColor)
} else { } else {
r.out.SetColor("white", "cyan") r.out.SetColor(r.completionTextColor, r.completionBGColor)
} }
r.out.WriteStr(" " + formatted[i] + " ") r.out.WriteStr(" " + formatted[i] + " ")
r.out.SetColor("white", "darkGray") r.out.SetColor(White, DarkGray)
r.out.Write([]byte(" ")) r.out.Write([]byte(" "))
r.out.CursorBackward(width + 3) r.out.CursorBackward(width + 3)
} }
@ -84,7 +90,7 @@ func (r *Render) renderCompletion(buf *Buffer, words []string, chosen int) {
} }
r.out.CursorUp(l) r.out.CursorUp(l)
r.out.SetColor("default", "default") r.out.SetColor(DefaultColor, DefaultColor)
return return
} }

@ -185,7 +185,7 @@ func (w *VT100Writer) ClearTitle() {
/* colors */ /* colors */
func (w *VT100Writer) SetColor(fg, bg string) (ok bool) { func (w *VT100Writer) SetColor(fg, bg Color) (ok bool) {
f, ok := foregroundANSIColors[fg] f, ok := foregroundANSIColors[fg]
if !ok { if !ok {
return return
@ -203,52 +203,52 @@ func (w *VT100Writer) SetColor(fg, bg string) (ok bool) {
return return
} }
var foregroundANSIColors = map[string][]byte{ var foregroundANSIColors = map[Color][]byte{
"default": {0x33, 0x39}, // 39 DefaultColor: {0x33, 0x39}, // 39
// Low intensity. // Low intensity.
"black": {0x33, 0x30}, // 30 Black: {0x33, 0x30}, // 30
"darkRed": {0x33, 0x31}, // 31 DarkRed: {0x33, 0x31}, // 31
"darkGreen": {0x33, 0x32}, // 32 DarkGreen: {0x33, 0x32}, // 32
"brown": {0x33, 0x33}, // 33 Brown: {0x33, 0x33}, // 33
"darkBlue": {0x33, 0x34}, // 34 DarkBlue: {0x33, 0x34}, // 34
"purple": {0x33, 0x35}, // 35 Purple: {0x33, 0x35}, // 35
"cyan": {0x33, 0x36}, //36 Cyan: {0x33, 0x36}, //36
"lightGray": {0x33, 0x37}, //37 LightGray: {0x33, 0x37}, //37
// High intensity. // High intensity.
"darkGray": {0x39, 0x30}, // 90 DarkGray: {0x39, 0x30}, // 90
"red": {0x39, 0x31}, // 91 Red: {0x39, 0x31}, // 91
"green": {0x39, 0x32}, // 92 Green: {0x39, 0x32}, // 92
"yellow": {0x39, 0x33}, // 93 Yellow: {0x39, 0x33}, // 93
"blue": {0x39, 0x34}, // 94 Blue: {0x39, 0x34}, // 94
"fuchsia": {0x39, 0x35}, // 95 Fuchsia: {0x39, 0x35}, // 95
"turquoise": {0x39, 0x36}, // 96 Turquoise: {0x39, 0x36}, // 96
"white": {0x39, 0x37}, // 97 White: {0x39, 0x37}, // 97
} }
var backgroundANSIColors = map[string][]byte{ var backgroundANSIColors = map[Color][]byte{
"default": {0x34, 0x39}, // 49 DefaultColor: {0x34, 0x39}, // 49
// Low intensity. // Low intensity.
"black": {0x34, 0x30}, // 40 Black: {0x34, 0x30}, // 40
"darkRed": {0x34, 0x31}, // 41 DarkRed: {0x34, 0x31}, // 41
"darkGreen": {0x34, 0x32}, // 42 DarkGreen: {0x34, 0x32}, // 42
"brown": {0x34, 0x33}, // 43 Brown: {0x34, 0x33}, // 43
"darkBlue": {0x34, 0x34}, // 44 DarkBlue: {0x34, 0x34}, // 44
"purple": {0x34, 0x35}, // 45 Purple: {0x34, 0x35}, // 45
"cyan": {0x34, 0x36}, // 46 Cyan: {0x34, 0x36}, // 46
"lightGray": {0x34, 0x37}, // 47 LightGray: {0x34, 0x37}, // 47
// High intensity // High intensity
"darkGray": {0x31, 0x30, 0x30}, // 100 DarkGray: {0x31, 0x30, 0x30}, // 100
"red": {0x31, 0x30, 0x31}, // 101 Red: {0x31, 0x30, 0x31}, // 101
"green": {0x31, 0x30, 0x32}, // 102 Green: {0x31, 0x30, 0x32}, // 102
"yellow": {0x31, 0x30, 0x33}, // 103 Yellow: {0x31, 0x30, 0x33}, // 103
"blue": {0x31, 0x30, 0x34}, // 104 Blue: {0x31, 0x30, 0x34}, // 104
"fuchsia": {0x31, 0x30, 0x35}, // 105 Fuchsia: {0x31, 0x30, 0x35}, // 105
"turquoise": {0x31, 0x30, 0x36}, // 106 Turquoise: {0x31, 0x30, 0x36}, // 106
"white": {0x31, 0x30, 0x37}, // 107 White: {0x31, 0x30, 0x37}, // 107
} }
func writeFilter(buf byte) bool { func writeFilter(buf byte) bool {