bubbles/textinput/textinput.go

460 lines
9.1 KiB
Go
Raw Normal View History

package textinput
import (
"strings"
"time"
"unicode"
tea "github.com/charmbracelet/bubbletea"
rw "github.com/mattn/go-runewidth"
2020-01-31 15:50:42 +03:00
"github.com/muesli/termenv"
)
const (
defaultBlinkSpeed = time.Millisecond * 600
)
var (
// color is a helper for returning colors
color func(s string) termenv.Color = termenv.ColorProfile().Color
)
// 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
// Model is the Tea model for this text input element.
type Model struct {
2020-04-22 21:28:22 +03:00
Err error
Prompt string
Cursor string
BlinkSpeed time.Duration
Placeholder string
2020-02-25 06:43:52 +03:00
TextColor string
BackgroundColor string
PlaceholderColor string
2020-02-02 06:47:38 +03:00
CursorColor string
// CharLimit is the maximum amount of characters this input element will
// accept. If 0 or less, there's no limit.
CharLimit int
// Width is the maximum number of characters that can be displayed at once.
// It essentially treats the text field like a horizontally scrolling
// viewport. If 0 or less this setting is ignored.
Width int
// Underlying text value
value []rune
// 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
// Cursor blink state
blink bool
// Cursor position
pos int
// Used to emulate a viewport when width is set and the content is
// overflowing
offset int
offsetRight int
}
// SetValue sets the value of the text input.
func (m *Model) SetValue(s string) {
runes := []rune(s)
if m.CharLimit > 0 && len(runes) > m.CharLimit {
m.value = runes[:m.CharLimit]
} else {
m.value = runes
}
if m.pos > len(m.value) {
m.pos = len(m.value)
}
m.handleOverflow()
}
// Value returns the value of the text input.
func (m Model) Value() string {
return string(m.value)
}
// Cursor start moves the cursor to the given position. If the position is out
// of bounds the cursor will be moved to the start or end accordingly.
func (m *Model) SetCursor(pos int) {
m.pos = clamp(pos, 0, len(m.value))
m.handleOverflow()
}
// CursorStart moves the cursor to the start of the field.
func (m *Model) CursorStart() {
m.pos = 0
m.handleOverflow()
}
// CursorEnd moves the cursor to the end of the field.
func (m *Model) CursorEnd() {
m.pos = len(m.value)
m.handleOverflow()
}
// Focused returns the focus state on the model.
2020-02-18 01:00:01 +03:00
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.
2020-02-18 01:00:01 +03:00
func (m *Model) Focus() {
m.focus = true
m.blink = false
}
// Blur removes the focus state on the model.
2020-02-18 01:00:01 +03:00
func (m *Model) Blur() {
m.focus = false
m.blink = true
}
// Reset sets the input to its default state with no input.
func (m *Model) Reset() {
m.value = nil
m.pos = 0
m.blink = false
}
// If a max width is defined, perform some logic to treat the visible area
// as a horizontally scrolling viewport.
func (m *Model) handleOverflow() {
if m.Width <= 0 || rw.StringWidth(string(m.value)) <= m.Width {
m.offset = 0
m.offsetRight = len(m.value)
return
}
if m.pos < m.offset {
m.offset = m.pos
w := 0
i := 0
runes := m.value[m.offset:]
for i < len(runes) && w <= m.Width {
w += rw.RuneWidth(runes[i])
if w <= m.Width+1 {
i++
}
}
m.offsetRight = m.offset + i
} else if m.pos >= m.offsetRight {
m.offsetRight = m.pos
w := 0
runes := m.value[:m.offsetRight]
i := len(runes) - 1
for i > 0 && w < m.Width {
w += rw.RuneWidth(runes[i])
if w <= m.Width {
i--
}
}
m.offset = m.offsetRight - (len(runes) - 1 - i)
}
}
2020-02-25 06:43:52 +03:00
// colorText colorizes a given string according to the TextColor value of the
// model.
2020-02-25 06:43:52 +03:00
func (m *Model) colorText(s string) string {
return termenv.
String(s).
Foreground(color(m.TextColor)).
Background(color(m.BackgroundColor)).
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)).
Background(color(m.BackgroundColor)).
2020-02-25 06:43:52 +03:00
String()
}
func (m *Model) wordLeft() {
if m.pos == 0 || len(m.value) == 0 {
return
}
i := m.pos - 1
for i >= 0 {
if unicode.IsSpace(m.value[i]) {
m.pos--
i--
} else {
break
}
}
for i >= 0 {
if !unicode.IsSpace(m.value[i]) {
m.pos--
i--
} else {
break
}
}
}
func (m *Model) wordRight() {
if m.pos >= len(m.value) || len(m.value) == 0 {
return
}
i := m.pos
for i < len(m.value) {
if unicode.IsSpace(rune(m.value[i])) {
m.pos++
i++
} else {
break
}
}
for i < len(m.value) {
if !unicode.IsSpace(rune(m.value[i])) {
m.pos++
i++
} else {
break
}
}
}
// BlinkMsg is sent when the cursor should alternate it's blinking state.
type BlinkMsg struct{}
// NewModel creates a new model with default settings.
func NewModel() Model {
return Model{
Prompt: "> ",
BlinkSpeed: defaultBlinkSpeed,
Placeholder: "",
2020-02-25 06:43:52 +03:00
TextColor: "",
PlaceholderColor: "240",
2020-02-02 06:47:38 +03:00
CursorColor: "",
CharLimit: 0,
value: nil,
focus: false,
blink: true,
pos: 0,
}
}
// Update is the Tea update loop.
func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
2020-02-18 01:00:01 +03:00
if !m.focus {
m.blink = true
return m, nil
}
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:
if len(m.value) > 0 {
m.value = append(m.value[:m.pos-1], m.value[m.pos:]...)
m.pos--
}
case tea.KeyLeft:
if msg.Alt { // alt+left arrow, back one word
m.wordLeft()
break
}
if m.pos > 0 {
m.pos--
}
case tea.KeyRight:
if msg.Alt { // alt+right arrow, forward one word
m.wordRight()
break
}
if m.pos < len(m.value) {
m.pos++
}
2020-02-20 17:44:58 +03:00
case tea.KeyCtrlF: // ^F, forward one character
fallthrough
case tea.KeyCtrlB: // ^B, back one charcter
fallthrough
case tea.KeyCtrlA: // ^A, go to beginning
m.CursorStart()
case tea.KeyCtrlD: // ^D, delete char under cursor
if len(m.value) > 0 && m.pos < len(m.value) {
m.value = append(m.value[:m.pos], m.value[m.pos+1:]...)
}
case tea.KeyCtrlE: // ^E, go to end
m.CursorEnd()
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)
2020-01-30 06:25:39 +03:00
case tea.KeyCtrlU: // ^U, kill text before cursor
m.value = m.value[m.pos:]
2020-01-30 06:25:39 +03:00
m.pos = 0
m.offset = 0
case tea.KeyRune: // input a regular character
if msg.Alt {
if msg.Rune == 'b' { // alt+b, back one word
m.wordLeft()
break
}
if msg.Rune == 'f' { // alt+f, forward one word
m.wordRight()
break
}
}
// Input a regular character
if m.CharLimit <= 0 || len(m.value) < m.CharLimit {
m.value = append(m.value[:m.pos], append([]rune{msg.Rune}, m.value[m.pos:]...)...)
m.pos++
}
}
2020-04-22 21:28:22 +03:00
case ErrMsg:
m.Err = msg
case BlinkMsg:
m.blink = !m.blink
return m, Blink(m)
}
m.handleOverflow()
return m, nil
}
// View renders the textinput in its current state.
func View(model tea.Model) string {
m, ok := model.(Model)
if !ok {
return "could not perform assertion on model"
}
// Placeholder text
if m.value == nil && m.Placeholder != "" {
2020-02-02 06:35:21 +03:00
return placeholderView(m)
}
value := m.value[m.offset:m.offsetRight]
pos := m.pos - m.offset
v := m.colorText(string(value[:pos]))
if pos < len(value) {
v += cursorView(string(value[pos]), m) // cursor and text under it
v += m.colorText(string(value[pos+1:])) // text after cursor
} else {
2020-02-02 06:47:38 +03:00
v += cursorView(" ", m)
}
// If a max width and background color were set fill the empty spaces with
// the background color.
valWidth := rw.StringWidth(string(value))
if m.Width > 0 && len(m.BackgroundColor) > 0 && valWidth <= m.Width {
padding := max(0, m.Width-valWidth)
if valWidth+padding <= m.Width && pos < len(value) {
padding++
}
v += strings.Repeat(
termenv.String(" ").Background(color(m.BackgroundColor)).String(),
padding,
)
}
return m.Prompt + v
}
// placeholderView
func placeholderView(m Model) string {
var (
v string
p = m.Placeholder
)
// Cursor
2020-02-18 01:00:01 +03:00
if m.blink && m.PlaceholderColor != "" {
2020-02-02 06:47:38 +03:00
v += cursorView(
m.colorPlaceholder(p[:1]),
2020-02-02 06:47:38 +03:00
m,
)
} else {
2020-02-02 06:47:38 +03:00
v += cursorView(p[:1], m)
}
2020-04-17 21:38:28 +03:00
// The rest of the placeholder text
v += m.colorPlaceholder(p[1:])
return m.Prompt + v
}
// cursorView styles 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 {
if m.TextColor != "" || m.BackgroundColor != "" {
return termenv.String(s).
Foreground(color(m.TextColor)).
Background(color(m.BackgroundColor)).
String()
}
return s
}
2020-04-17 21:38:11 +03:00
return termenv.String(s).
Foreground(color(m.CursorColor)).
Background(color(m.BackgroundColor)).
2020-04-17 21:38:11 +03:00
Reverse().
String()
}
// Blink is a command used to time the cursor blinking.
func Blink(model Model) tea.Cmd {
return func() tea.Msg {
time.Sleep(model.BlinkSpeed)
return BlinkMsg{}
}
}
func clamp(v, low, high int) int {
return min(high, max(low, v))
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}