diff --git a/examples/inputs/main.go b/examples/inputs/main.go index cfb3d89..9010ab0 100644 --- a/examples/inputs/main.go +++ b/examples/inputs/main.go @@ -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] diff --git a/input/input.go b/input/input.go index 49c69ac..cd62e4c 100644 --- a/input/input.go +++ b/input/input.go @@ -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) }