Text color setting on input element

This commit is contained in:
Christian Rocha 2020-02-24 22:43:52 -05:00
parent aecd5ac8e2
commit f4a59f7bc3
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
2 changed files with 19 additions and 2 deletions

View File

@ -11,6 +11,7 @@ import (
var ( var (
color = te.ColorProfile().Color color = te.ColorProfile().Color
focusedText = "205"
focusedPrompt = te.String("> ").Foreground(color("205")).String() focusedPrompt = te.String("> ").Foreground(color("205")).String()
blurredPrompt = "> " blurredPrompt = "> "
focusedSubmitButton = "[ " + te.String("Submit").Foreground(color("205")).String() + " ]" focusedSubmitButton = "[ " + te.String("Submit").Foreground(color("205")).String() + " ]"
@ -42,6 +43,7 @@ func initialize() (tea.Model, tea.Cmd) {
name.Placeholder = "Name" name.Placeholder = "Name"
name.Focus() name.Focus()
name.Prompt = focusedPrompt name.Prompt = focusedPrompt
name.TextColor = focusedText
nickName := input.DefaultModel() nickName := input.DefaultModel()
nickName.Placeholder = "Nickname" nickName.Placeholder = "Nickname"
@ -107,12 +109,16 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
for i := 0; i <= len(inputs)-1; i++ { for i := 0; i <= len(inputs)-1; i++ {
if i == m.index { if i == m.index {
// Focused input
inputs[i].Focus() inputs[i].Focus()
inputs[i].Prompt = focusedPrompt inputs[i].Prompt = focusedPrompt
inputs[i].TextColor = focusedText
continue continue
} }
// Blurred input
inputs[i].Blur() inputs[i].Blur()
inputs[i].Prompt = blurredPrompt inputs[i].Prompt = blurredPrompt
inputs[i].TextColor = ""
} }
m.nameInput = inputs[0] m.nameInput = inputs[0]

View File

@ -14,6 +14,7 @@ type Model struct {
Cursor string Cursor string
BlinkSpeed time.Duration BlinkSpeed time.Duration
Placeholder string Placeholder string
TextColor string
PlaceholderColor string PlaceholderColor string
CursorColor string CursorColor string
@ -46,6 +47,15 @@ func (m *Model) Blur() {
m.blink = true m.blink = true
} }
// colorText colorizes a given string according to the TextColor value of the
// model
func (m *Model) colorText(s string) string {
return termenv.
String(s).
Foreground(m.colorProfile.Color(m.TextColor)).
String()
}
type CursorBlinkMsg struct{} type CursorBlinkMsg struct{}
func DefaultModel() Model { func DefaultModel() Model {
@ -54,6 +64,7 @@ func DefaultModel() Model {
Value: "", Value: "",
BlinkSpeed: time.Millisecond * 600, BlinkSpeed: time.Millisecond * 600,
Placeholder: "", Placeholder: "",
TextColor: "",
PlaceholderColor: "240", PlaceholderColor: "240",
CursorColor: "", CursorColor: "",
@ -140,11 +151,11 @@ func View(model tea.Model) string {
return placeholderView(m) return placeholderView(m)
} }
v := m.Value[:m.pos] v := m.colorText(m.Value[:m.pos])
if m.pos < len(m.Value) { if m.pos < len(m.Value) {
v += cursorView(string(m.Value[m.pos]), m) v += cursorView(string(m.Value[m.pos]), m)
v += m.Value[m.pos+1:] v += m.colorText(m.Value[m.pos+1:])
} else { } else {
v += cursorView(" ", m) v += cursorView(" ", m)
} }