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