Just set the color profile on the package level

This commit is contained in:
Christian Rocha 2020-02-24 22:57:48 -05:00
parent f4a59f7bc3
commit 9225516bf4
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018

View File

@ -8,6 +8,11 @@ import (
"github.com/muesli/termenv"
)
var (
// Helper for returning colors
color func(s string) termenv.Color = termenv.ColorProfile().Color
)
type Model struct {
Prompt string
Value string
@ -24,7 +29,6 @@ type Model struct {
blink bool
pos int
colorProfile termenv.Profile
}
// Focused returns the focus state on the model
@ -52,7 +56,16 @@ func (m *Model) Blur() {
func (m *Model) colorText(s string) string {
return termenv.
String(s).
Foreground(m.colorProfile.Color(m.TextColor)).
Foreground(color(m.TextColor)).
String()
}
// colorPlaceholder colorizes a given string according to the TextColor value
// of the model
func (m *Model) colorPlaceholder(s string) string {
return termenv.
String(s).
Foreground(color(m.PlaceholderColor)).
String()
}
@ -71,7 +84,6 @@ func DefaultModel() Model {
focus: false,
blink: true,
pos: 0,
colorProfile: termenv.ColorProfile(),
}
}
@ -166,16 +178,12 @@ func placeholderView(m Model) string {
var (
v string
p = m.Placeholder
c = m.PlaceholderColor
color = m.colorProfile.Color
)
// Cursor
if m.blink && m.PlaceholderColor != "" {
v += cursorView(
termenv.String(p[:1]).
Foreground(color(c)).
String(),
m.colorPlaceholder(p[:1]),
m,
)
} else {
@ -183,9 +191,7 @@ func placeholderView(m Model) string {
}
// The rest of the palceholder text
v += termenv.String(p[1:]).
Foreground(color(c)).
String()
v += m.colorPlaceholder(p[1:])
return m.Prompt + v
}
@ -196,7 +202,7 @@ func cursorView(s string, m Model) string {
return s
} else {
return termenv.String(s).
Foreground(m.colorProfile.Color(m.CursorColor)).
Foreground(color(m.CursorColor)).
Reverse().
String()
}