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 (
color = te.ColorProfile().Color
focusedText = "205"
focusedPrompt = te.String("> ").Foreground(color("205")).String()
blurredPrompt = "> "
focusedSubmitButton = "[ " + te.String("Submit").Foreground(color("205")).String() + " ]"
@ -42,6 +43,7 @@ func initialize() (tea.Model, tea.Cmd) {
name.Placeholder = "Name"
name.Focus()
name.Prompt = focusedPrompt
name.TextColor = focusedText
nickName := input.DefaultModel()
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++ {
if i == m.index {
// Focused input
inputs[i].Focus()
inputs[i].Prompt = focusedPrompt
inputs[i].TextColor = focusedText
continue
}
// Blurred input
inputs[i].Blur()
inputs[i].Prompt = blurredPrompt
inputs[i].TextColor = ""
}
m.nameInput = inputs[0]

View File

@ -14,6 +14,7 @@ type Model struct {
Cursor string
BlinkSpeed time.Duration
Placeholder string
TextColor string
PlaceholderColor string
CursorColor string
@ -46,6 +47,15 @@ func (m *Model) Blur() {
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{}
func DefaultModel() Model {
@ -54,6 +64,7 @@ func DefaultModel() Model {
Value: "",
BlinkSpeed: time.Millisecond * 600,
Placeholder: "",
TextColor: "",
PlaceholderColor: "240",
CursorColor: "",
@ -140,11 +151,11 @@ func View(model tea.Model) string {
return placeholderView(m)
}
v := m.Value[:m.pos]
v := m.colorText(m.Value[:m.pos])
if m.pos < len(m.Value) {
v += cursorView(string(m.Value[m.pos]), m)
v += m.Value[m.pos+1:]
v += m.colorText(m.Value[m.pos+1:])
} else {
v += cursorView(" ", m)
}