Improve color options
This commit is contained in:
parent
2921adafb3
commit
85830110de
6
main.go
6
main.go
@ -50,8 +50,10 @@ func main() {
|
||||
completer,
|
||||
prompt.MaxCompletionsOption(8),
|
||||
prompt.PrefixOption(">>> "),
|
||||
prompt.PrefixColorOption("blue"),
|
||||
prompt.TitleOption("Hello! this is prompt toolkit"),
|
||||
prompt.PrefixColorOption(prompt.Blue),
|
||||
prompt.TitleOption("SQLITE CLI"),
|
||||
prompt.CompletionTextColor(prompt.Black),
|
||||
prompt.SelectedCompletionTextColor(prompt.White),
|
||||
)
|
||||
defer fmt.Println("\nGoodbye!")
|
||||
fmt.Print("Hello! This is a example appication using prompt-toolkit.\n")
|
||||
|
27
prompt/color.go
Normal file
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 */
|
||||
|
||||
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 {
|
||||
p.renderer.prefixColor = x
|
||||
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 {
|
||||
return func(p *Prompt) error {
|
||||
p.renderer.maxCompletions = x
|
||||
@ -51,8 +79,12 @@ func NewPrompt(executor Executor, completer Completer, opts ...option) *Prompt {
|
||||
in: &VT100Parser{fd: syscall.Stdin},
|
||||
renderer: &Render{
|
||||
prefix: "> ",
|
||||
prefixColor: "green",
|
||||
out: &VT100Writer{fd: syscall.Stdout},
|
||||
prefixColor: Green,
|
||||
completionTextColor: White,
|
||||
completionBGColor: Cyan,
|
||||
selectedCompletionTextColor: Black,
|
||||
selectedCompletionBGColor: Turquoise,
|
||||
},
|
||||
buf: NewBuffer(),
|
||||
executor: executor,
|
||||
|
@ -2,12 +2,18 @@ package prompt
|
||||
|
||||
type Render struct {
|
||||
prefix string
|
||||
prefixColor string
|
||||
title string
|
||||
out ConsoleWriter
|
||||
row uint16
|
||||
col uint16
|
||||
maxCompletions uint16
|
||||
// colors
|
||||
prefixColor Color
|
||||
textColor Color
|
||||
completionTextColor Color
|
||||
completionBGColor Color
|
||||
selectedCompletionTextColor Color
|
||||
selectedCompletionBGColor Color
|
||||
}
|
||||
|
||||
func (r *Render) Setup() {
|
||||
@ -19,9 +25,9 @@ func (r *Render) Setup() {
|
||||
}
|
||||
|
||||
func (r *Render) renderPrefix() {
|
||||
r.out.SetColor(r.prefixColor, "default")
|
||||
r.out.SetColor(r.prefixColor, DefaultColor)
|
||||
r.out.WriteStr(r.prefix)
|
||||
r.out.SetColor("default", "default")
|
||||
r.out.SetColor(DefaultColor, DefaultColor)
|
||||
}
|
||||
|
||||
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.SetColor("white", "teal")
|
||||
r.out.SetColor(White, Cyan)
|
||||
for i := 0; i < l; i++ {
|
||||
r.out.CursorDown(1)
|
||||
if i == chosen {
|
||||
r.out.SetColor("black", "turquoise")
|
||||
r.out.SetColor(r.selectedCompletionTextColor, r.selectedCompletionBGColor)
|
||||
} else {
|
||||
r.out.SetColor("white", "cyan")
|
||||
r.out.SetColor(r.completionTextColor, r.completionBGColor)
|
||||
}
|
||||
r.out.WriteStr(" " + formatted[i] + " ")
|
||||
r.out.SetColor("white", "darkGray")
|
||||
r.out.SetColor(White, DarkGray)
|
||||
r.out.Write([]byte(" "))
|
||||
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.SetColor("default", "default")
|
||||
r.out.SetColor(DefaultColor, DefaultColor)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -185,7 +185,7 @@ func (w *VT100Writer) ClearTitle() {
|
||||
|
||||
/* colors */
|
||||
|
||||
func (w *VT100Writer) SetColor(fg, bg string) (ok bool) {
|
||||
func (w *VT100Writer) SetColor(fg, bg Color) (ok bool) {
|
||||
f, ok := foregroundANSIColors[fg]
|
||||
if !ok {
|
||||
return
|
||||
@ -203,52 +203,52 @@ func (w *VT100Writer) SetColor(fg, bg string) (ok bool) {
|
||||
return
|
||||
}
|
||||
|
||||
var foregroundANSIColors = map[string][]byte{
|
||||
"default": {0x33, 0x39}, // 39
|
||||
var foregroundANSIColors = map[Color][]byte{
|
||||
DefaultColor: {0x33, 0x39}, // 39
|
||||
|
||||
// Low intensity.
|
||||
"black": {0x33, 0x30}, // 30
|
||||
"darkRed": {0x33, 0x31}, // 31
|
||||
"darkGreen": {0x33, 0x32}, // 32
|
||||
"brown": {0x33, 0x33}, // 33
|
||||
"darkBlue": {0x33, 0x34}, // 34
|
||||
"purple": {0x33, 0x35}, // 35
|
||||
"cyan": {0x33, 0x36}, //36
|
||||
"lightGray": {0x33, 0x37}, //37
|
||||
Black: {0x33, 0x30}, // 30
|
||||
DarkRed: {0x33, 0x31}, // 31
|
||||
DarkGreen: {0x33, 0x32}, // 32
|
||||
Brown: {0x33, 0x33}, // 33
|
||||
DarkBlue: {0x33, 0x34}, // 34
|
||||
Purple: {0x33, 0x35}, // 35
|
||||
Cyan: {0x33, 0x36}, //36
|
||||
LightGray: {0x33, 0x37}, //37
|
||||
|
||||
// High intensity.
|
||||
"darkGray": {0x39, 0x30}, // 90
|
||||
"red": {0x39, 0x31}, // 91
|
||||
"green": {0x39, 0x32}, // 92
|
||||
"yellow": {0x39, 0x33}, // 93
|
||||
"blue": {0x39, 0x34}, // 94
|
||||
"fuchsia": {0x39, 0x35}, // 95
|
||||
"turquoise": {0x39, 0x36}, // 96
|
||||
"white": {0x39, 0x37}, // 97
|
||||
DarkGray: {0x39, 0x30}, // 90
|
||||
Red: {0x39, 0x31}, // 91
|
||||
Green: {0x39, 0x32}, // 92
|
||||
Yellow: {0x39, 0x33}, // 93
|
||||
Blue: {0x39, 0x34}, // 94
|
||||
Fuchsia: {0x39, 0x35}, // 95
|
||||
Turquoise: {0x39, 0x36}, // 96
|
||||
White: {0x39, 0x37}, // 97
|
||||
}
|
||||
|
||||
var backgroundANSIColors = map[string][]byte{
|
||||
"default": {0x34, 0x39}, // 49
|
||||
var backgroundANSIColors = map[Color][]byte{
|
||||
DefaultColor: {0x34, 0x39}, // 49
|
||||
|
||||
// Low intensity.
|
||||
"black": {0x34, 0x30}, // 40
|
||||
"darkRed": {0x34, 0x31}, // 41
|
||||
"darkGreen": {0x34, 0x32}, // 42
|
||||
"brown": {0x34, 0x33}, // 43
|
||||
"darkBlue": {0x34, 0x34}, // 44
|
||||
"purple": {0x34, 0x35}, // 45
|
||||
"cyan": {0x34, 0x36}, // 46
|
||||
"lightGray": {0x34, 0x37}, // 47
|
||||
Black: {0x34, 0x30}, // 40
|
||||
DarkRed: {0x34, 0x31}, // 41
|
||||
DarkGreen: {0x34, 0x32}, // 42
|
||||
Brown: {0x34, 0x33}, // 43
|
||||
DarkBlue: {0x34, 0x34}, // 44
|
||||
Purple: {0x34, 0x35}, // 45
|
||||
Cyan: {0x34, 0x36}, // 46
|
||||
LightGray: {0x34, 0x37}, // 47
|
||||
|
||||
// High intensity
|
||||
"darkGray": {0x31, 0x30, 0x30}, // 100
|
||||
"red": {0x31, 0x30, 0x31}, // 101
|
||||
"green": {0x31, 0x30, 0x32}, // 102
|
||||
"yellow": {0x31, 0x30, 0x33}, // 103
|
||||
"blue": {0x31, 0x30, 0x34}, // 104
|
||||
"fuchsia": {0x31, 0x30, 0x35}, // 105
|
||||
"turquoise": {0x31, 0x30, 0x36}, // 106
|
||||
"white": {0x31, 0x30, 0x37}, // 107
|
||||
DarkGray: {0x31, 0x30, 0x30}, // 100
|
||||
Red: {0x31, 0x30, 0x31}, // 101
|
||||
Green: {0x31, 0x30, 0x32}, // 102
|
||||
Yellow: {0x31, 0x30, 0x33}, // 103
|
||||
Blue: {0x31, 0x30, 0x34}, // 104
|
||||
Fuchsia: {0x31, 0x30, 0x35}, // 105
|
||||
Turquoise: {0x31, 0x30, 0x36}, // 106
|
||||
White: {0x31, 0x30, 0x37}, // 107
|
||||
}
|
||||
|
||||
func writeFilter(buf byte) bool {
|
||||
|
Loading…
Reference in New Issue
Block a user