Cursor color option on input field

This commit is contained in:
Christian Rocha 2020-02-01 22:47:38 -05:00
parent d012797c69
commit 68774581b7
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018

View File

@ -15,6 +15,7 @@ type Model struct {
BlinkSpeed time.Duration BlinkSpeed time.Duration
Placeholder string Placeholder string
PlaceholderColor string PlaceholderColor string
CursorColor string
blink bool blink bool
pos int pos int
@ -30,6 +31,7 @@ func DefaultModel() Model {
BlinkSpeed: time.Millisecond * 600, BlinkSpeed: time.Millisecond * 600,
Placeholder: "", Placeholder: "",
PlaceholderColor: "240", PlaceholderColor: "240",
CursorColor: "",
blink: false, blink: false,
pos: 0, pos: 0,
@ -111,10 +113,10 @@ func View(model tea.Model) string {
v := m.Value[:m.pos] v := m.Value[:m.pos]
if m.pos < len(m.Value) { if m.pos < len(m.Value) {
v += cursor(string(m.Value[m.pos]), m.blink) v += cursorView(string(m.Value[m.pos]), m)
v += m.Value[m.pos+1:] v += m.Value[m.pos+1:]
} else { } else {
v += cursor(" ", m.blink) v += cursorView(" ", m)
} }
return m.Prompt + v return m.Prompt + v
} }
@ -129,14 +131,14 @@ func placeholderView(m Model) string {
// Cursor // Cursor
if m.blink && m.PlaceholderColor != "" { if m.blink && m.PlaceholderColor != "" {
v += cursor( v += cursorView(
termenv.String(p[:1]). termenv.String(p[:1]).
Foreground(color(c)). Foreground(color(c)).
String(), String(),
m.blink, m,
) )
} else { } else {
v += cursor(p[:1], m.blink) v += cursorView(p[:1], m)
} }
// The rest of the palceholder text // The rest of the palceholder text
@ -148,9 +150,14 @@ func placeholderView(m Model) string {
} }
// Style the cursor // Style the cursor
func cursor(s string, blink bool) string { func cursorView(s string, m Model) string {
if blink { if m.blink {
return s return s
} else if m.CursorColor != "" {
return termenv.String(s).
Foreground(m.colorProfile.Color(m.CursorColor)).
Reverse().
String()
} }
return termenv.String(s).Reverse().String() return termenv.String(s).Reverse().String()
} }