2020-04-23 19:06:08 +03:00
|
|
|
package textinput
|
2020-01-18 18:57:38 +03:00
|
|
|
|
|
|
|
import (
|
2020-04-22 21:28:22 +03:00
|
|
|
"errors"
|
2020-01-18 18:57:38 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/charmbracelet/tea"
|
2020-01-31 15:50:42 +03:00
|
|
|
"github.com/muesli/termenv"
|
2020-01-18 18:57:38 +03:00
|
|
|
)
|
|
|
|
|
2020-02-25 06:57:48 +03:00
|
|
|
var (
|
2020-04-23 19:06:08 +03:00
|
|
|
// color is a helper for returning colors
|
2020-02-25 06:57:48 +03:00
|
|
|
color func(s string) termenv.Color = termenv.ColorProfile().Color
|
|
|
|
)
|
|
|
|
|
2020-04-23 19:06:08 +03:00
|
|
|
// ErrMsg indicates there's been an error. We don't handle errors in the this
|
|
|
|
// package; we're expecting errors to be handle in the program that implements
|
|
|
|
// this text input.
|
2020-04-22 21:28:22 +03:00
|
|
|
type ErrMsg error
|
|
|
|
|
2020-04-23 19:06:08 +03:00
|
|
|
// Model is the Tea model for this text input element
|
2020-01-18 18:57:38 +03:00
|
|
|
type Model struct {
|
2020-04-22 21:28:22 +03:00
|
|
|
Err error
|
2020-02-02 06:26:35 +03:00
|
|
|
Prompt string
|
|
|
|
Value string
|
|
|
|
Cursor string
|
|
|
|
BlinkSpeed time.Duration
|
|
|
|
Placeholder string
|
2020-02-25 06:43:52 +03:00
|
|
|
TextColor string
|
2020-02-02 06:26:35 +03:00
|
|
|
PlaceholderColor string
|
2020-02-02 06:47:38 +03:00
|
|
|
CursorColor string
|
2020-02-02 06:26:35 +03:00
|
|
|
|
2020-04-02 03:51:09 +03:00
|
|
|
// CharLimit is the maximum amount of characters this input element will
|
|
|
|
// accept. If 0 or less, there's no limit.
|
|
|
|
CharLimit int
|
|
|
|
|
2020-02-18 00:00:53 +03:00
|
|
|
// Focus indicates whether user input focus should be on this input
|
|
|
|
// component. When false, don't blink and ignore keyboard input.
|
2020-02-18 01:00:01 +03:00
|
|
|
focus bool
|
2020-02-18 00:00:53 +03:00
|
|
|
|
2020-02-25 06:57:48 +03:00
|
|
|
blink bool
|
|
|
|
pos int
|
2020-01-18 18:57:38 +03:00
|
|
|
}
|
|
|
|
|
2020-02-18 01:00:01 +03:00
|
|
|
// Focused returns the focus state on the model
|
|
|
|
func (m Model) Focused() bool {
|
2020-04-17 21:38:11 +03:00
|
|
|
return m.focus
|
2020-02-18 01:00:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Focus sets the focus state on the model
|
|
|
|
func (m *Model) Focus() {
|
|
|
|
m.focus = true
|
|
|
|
m.blink = false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blur removes the focus state on the model
|
|
|
|
func (m *Model) Blur() {
|
|
|
|
m.focus = false
|
|
|
|
m.blink = true
|
|
|
|
}
|
|
|
|
|
2020-02-25 06:43:52 +03:00
|
|
|
// colorText colorizes a given string according to the TextColor value of the
|
|
|
|
// model
|
|
|
|
func (m *Model) colorText(s string) string {
|
|
|
|
return termenv.
|
|
|
|
String(s).
|
2020-02-25 06:57:48 +03:00
|
|
|
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)).
|
2020-02-25 06:43:52 +03:00
|
|
|
String()
|
|
|
|
}
|
|
|
|
|
2020-04-23 19:06:08 +03:00
|
|
|
// CursorBlinkMsg is sent when the cursor should alternate it's blinking state
|
2020-01-18 18:57:38 +03:00
|
|
|
type CursorBlinkMsg struct{}
|
|
|
|
|
2020-04-23 19:06:08 +03:00
|
|
|
// NewModel creates a new model with default settings
|
|
|
|
func NewModel() Model {
|
2020-01-18 18:57:38 +03:00
|
|
|
return Model{
|
2020-02-02 06:26:35 +03:00
|
|
|
Prompt: "> ",
|
|
|
|
Value: "",
|
|
|
|
BlinkSpeed: time.Millisecond * 600,
|
|
|
|
Placeholder: "",
|
2020-02-25 06:43:52 +03:00
|
|
|
TextColor: "",
|
2020-02-02 06:26:35 +03:00
|
|
|
PlaceholderColor: "240",
|
2020-02-02 06:47:38 +03:00
|
|
|
CursorColor: "",
|
2020-04-23 19:06:08 +03:00
|
|
|
CharLimit: 0,
|
2020-01-18 18:57:38 +03:00
|
|
|
|
2020-02-25 06:57:48 +03:00
|
|
|
focus: false,
|
|
|
|
blink: true,
|
|
|
|
pos: 0,
|
2020-01-18 18:57:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 19:06:08 +03:00
|
|
|
// Update is the Tea update loop
|
2020-01-18 18:57:38 +03:00
|
|
|
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
|
2020-02-18 01:00:01 +03:00
|
|
|
if !m.focus {
|
2020-02-18 00:00:53 +03:00
|
|
|
m.blink = true
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2020-01-18 18:57:38 +03:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
|
|
|
|
case tea.KeyMsg:
|
|
|
|
switch msg.Type {
|
|
|
|
case tea.KeyBackspace:
|
2020-01-30 05:52:03 +03:00
|
|
|
fallthrough
|
|
|
|
case tea.KeyDelete:
|
2020-01-18 18:57:38 +03:00
|
|
|
if len(m.Value) > 0 {
|
|
|
|
m.Value = m.Value[:m.pos-1] + m.Value[m.pos:]
|
|
|
|
m.pos--
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
case tea.KeyLeft:
|
|
|
|
if m.pos > 0 {
|
|
|
|
m.pos--
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
case tea.KeyRight:
|
|
|
|
if m.pos < len(m.Value) {
|
|
|
|
m.pos++
|
|
|
|
}
|
|
|
|
return m, nil
|
2020-02-20 17:44:58 +03:00
|
|
|
case tea.KeyCtrlF: // ^F, forward one character
|
|
|
|
fallthrough
|
|
|
|
case tea.KeyCtrlB: // ^B, back one charcter
|
|
|
|
fallthrough
|
2020-04-23 19:06:08 +03:00
|
|
|
case tea.KeyCtrlA: // ^A, go to beginning
|
2020-01-30 05:46:03 +03:00
|
|
|
m.pos = 0
|
|
|
|
return m, nil
|
2020-01-30 06:19:47 +03:00
|
|
|
case tea.KeyCtrlD: // ^D, delete char under cursor
|
|
|
|
if len(m.Value) > 0 && m.pos < len(m.Value) {
|
|
|
|
m.Value = m.Value[:m.pos] + m.Value[m.pos+1:]
|
|
|
|
}
|
|
|
|
return m, nil
|
2020-04-23 19:06:08 +03:00
|
|
|
case tea.KeyCtrlE: // ^E, go to end
|
2020-02-02 06:30:08 +03:00
|
|
|
m.pos = len(m.Value)
|
2020-01-30 05:46:03 +03:00
|
|
|
return m, nil
|
2020-01-30 05:51:52 +03:00
|
|
|
case tea.KeyCtrlK: // ^K, kill text after cursor
|
|
|
|
m.Value = m.Value[:m.pos]
|
|
|
|
m.pos = len(m.Value)
|
|
|
|
return m, nil
|
2020-01-30 06:25:39 +03:00
|
|
|
case tea.KeyCtrlU: // ^U, kill text before cursor
|
|
|
|
m.Value = m.Value[m.pos:]
|
|
|
|
m.pos = 0
|
|
|
|
return m, nil
|
2020-04-02 03:51:09 +03:00
|
|
|
case tea.KeyRune: // input a regular character
|
|
|
|
if m.CharLimit > 0 && len(m.Value) < m.CharLimit {
|
|
|
|
m.Value = m.Value[:m.pos] + string(msg.Rune) + m.Value[m.pos:]
|
|
|
|
m.pos++
|
|
|
|
}
|
2020-01-18 18:57:38 +03:00
|
|
|
return m, nil
|
|
|
|
default:
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2020-04-22 21:28:22 +03:00
|
|
|
case ErrMsg:
|
|
|
|
m.Err = msg
|
|
|
|
return m, nil
|
|
|
|
|
2020-01-18 18:57:38 +03:00
|
|
|
case CursorBlinkMsg:
|
|
|
|
m.blink = !m.blink
|
|
|
|
return m, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 19:06:08 +03:00
|
|
|
// View renders the textinput in its current state
|
2020-01-18 18:57:38 +03:00
|
|
|
func View(model tea.Model) string {
|
2020-04-23 19:06:08 +03:00
|
|
|
m, ok := model.(Model)
|
|
|
|
if !ok {
|
|
|
|
return "could not perform assertion on model"
|
|
|
|
}
|
2020-02-02 06:26:35 +03:00
|
|
|
|
|
|
|
// Placeholder text
|
|
|
|
if m.Value == "" && m.Placeholder != "" {
|
2020-02-02 06:35:21 +03:00
|
|
|
return placeholderView(m)
|
2020-02-02 06:26:35 +03:00
|
|
|
}
|
|
|
|
|
2020-02-25 06:43:52 +03:00
|
|
|
v := m.colorText(m.Value[:m.pos])
|
2020-02-02 06:26:35 +03:00
|
|
|
|
2020-01-18 18:57:38 +03:00
|
|
|
if m.pos < len(m.Value) {
|
2020-02-02 06:47:38 +03:00
|
|
|
v += cursorView(string(m.Value[m.pos]), m)
|
2020-02-25 06:43:52 +03:00
|
|
|
v += m.colorText(m.Value[m.pos+1:])
|
2020-01-18 18:57:38 +03:00
|
|
|
} else {
|
2020-02-02 06:47:38 +03:00
|
|
|
v += cursorView(" ", m)
|
2020-01-18 18:57:38 +03:00
|
|
|
}
|
|
|
|
return m.Prompt + v
|
|
|
|
}
|
|
|
|
|
2020-04-23 19:06:08 +03:00
|
|
|
// placeholderView
|
2020-02-02 06:26:35 +03:00
|
|
|
func placeholderView(m Model) string {
|
|
|
|
var (
|
2020-02-25 06:57:48 +03:00
|
|
|
v string
|
|
|
|
p = m.Placeholder
|
2020-02-02 06:26:35 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Cursor
|
2020-02-18 01:00:01 +03:00
|
|
|
if m.blink && m.PlaceholderColor != "" {
|
2020-02-02 06:47:38 +03:00
|
|
|
v += cursorView(
|
2020-02-25 06:57:48 +03:00
|
|
|
m.colorPlaceholder(p[:1]),
|
2020-02-02 06:47:38 +03:00
|
|
|
m,
|
2020-02-02 06:26:35 +03:00
|
|
|
)
|
|
|
|
} else {
|
2020-02-02 06:47:38 +03:00
|
|
|
v += cursorView(p[:1], m)
|
2020-02-02 06:26:35 +03:00
|
|
|
}
|
|
|
|
|
2020-04-17 21:38:28 +03:00
|
|
|
// The rest of the placeholder text
|
2020-02-25 06:57:48 +03:00
|
|
|
v += m.colorPlaceholder(p[1:])
|
2020-02-02 06:26:35 +03:00
|
|
|
|
|
|
|
return m.Prompt + v
|
|
|
|
}
|
|
|
|
|
2020-04-23 19:06:08 +03:00
|
|
|
// cursorView style the cursor
|
2020-02-02 06:47:38 +03:00
|
|
|
func cursorView(s string, m Model) string {
|
2020-02-18 01:00:01 +03:00
|
|
|
if m.blink {
|
2020-01-18 18:57:38 +03:00
|
|
|
return s
|
|
|
|
}
|
2020-04-17 21:38:11 +03:00
|
|
|
return termenv.String(s).
|
|
|
|
Foreground(color(m.CursorColor)).
|
|
|
|
Reverse().
|
|
|
|
String()
|
2020-01-18 18:57:38 +03:00
|
|
|
}
|
|
|
|
|
2020-04-23 19:06:08 +03:00
|
|
|
// Blink is the subscription that lets us know when to alternate the blinking
|
|
|
|
// of the cursor.
|
2020-01-18 18:57:38 +03:00
|
|
|
func Blink(model tea.Model) tea.Msg {
|
2020-01-26 06:22:57 +03:00
|
|
|
m, ok := model.(Model)
|
|
|
|
if !ok {
|
2020-04-22 21:28:22 +03:00
|
|
|
return ErrMsg(errors.New("could not assert given model to the model we expected; make sure you're passing as input model"))
|
2020-01-26 06:22:57 +03:00
|
|
|
}
|
2020-01-18 18:57:38 +03:00
|
|
|
time.Sleep(m.BlinkSpeed)
|
|
|
|
return CursorBlinkMsg{}
|
|
|
|
}
|