2020-04-23 19:06:08 +03:00
|
|
|
package textinput
|
2020-01-18 18:57:38 +03:00
|
|
|
|
|
|
|
import (
|
2020-05-26 02:57:58 +03:00
|
|
|
"strings"
|
2020-01-18 18:57:38 +03:00
|
|
|
"time"
|
2020-05-26 02:57:58 +03:00
|
|
|
"unicode"
|
2020-01-18 18:57:38 +03:00
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2020-06-12 03:43:32 +03:00
|
|
|
rw "github.com/mattn/go-runewidth"
|
2020-01-31 15:50:42 +03:00
|
|
|
"github.com/muesli/termenv"
|
2020-01-18 18:57:38 +03:00
|
|
|
)
|
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
const (
|
|
|
|
defaultBlinkSpeed = time.Millisecond * 600
|
|
|
|
)
|
|
|
|
|
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-05-26 02:57:58 +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
|
|
|
|
Cursor string
|
|
|
|
BlinkSpeed time.Duration
|
|
|
|
Placeholder string
|
2020-02-25 06:43:52 +03:00
|
|
|
TextColor string
|
2020-05-26 02:57:58 +03:00
|
|
|
BackgroundColor 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-05-26 02:57:58 +03:00
|
|
|
// 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
|
2020-06-12 01:31:17 +03:00
|
|
|
value []rune
|
2020-05-26 02:57:58 +03:00
|
|
|
|
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-05-26 02:57:58 +03:00
|
|
|
// Cursor blink state
|
2020-02-25 06:57:48 +03:00
|
|
|
blink bool
|
2020-05-26 02:57:58 +03:00
|
|
|
|
|
|
|
// Cursor position
|
|
|
|
pos int
|
|
|
|
|
|
|
|
// Used to emulate a viewport when width is set and the content is
|
|
|
|
// overflowing
|
2020-06-12 03:43:32 +03:00
|
|
|
offset int
|
|
|
|
offsetRight int
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetValue sets the value of the text input.
|
|
|
|
func (m *Model) SetValue(s string) {
|
2020-06-12 01:31:17 +03:00
|
|
|
runes := []rune(s)
|
|
|
|
if m.CharLimit > 0 && len(runes) > m.CharLimit {
|
|
|
|
m.value = runes[:m.CharLimit]
|
2020-05-26 02:57:58 +03:00
|
|
|
} else {
|
2020-06-12 01:31:17 +03:00
|
|
|
m.value = runes
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
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 {
|
2020-06-12 01:31:17 +03:00
|
|
|
return string(m.value)
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2020-06-12 03:43:32 +03:00
|
|
|
m.pos = clamp(pos, 0, len(m.value))
|
2020-05-26 02:57:58 +03:00
|
|
|
m.handleOverflow()
|
2020-01-18 18:57:38 +03:00
|
|
|
}
|
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2020-05-26 02:57:58 +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
|
|
|
|
}
|
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
// Reset sets the input to its default state with no input.
|
|
|
|
func (m *Model) Reset() {
|
2020-06-12 01:31:17 +03:00
|
|
|
m.value = nil
|
2020-05-26 02:57:58 +03:00
|
|
|
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() {
|
2020-06-12 03:43:32 +03:00
|
|
|
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:]
|
2020-05-26 02:57:58 +03:00
|
|
|
|
2020-06-12 03:43:32 +03:00
|
|
|
for i < len(runes) && w <= m.Width {
|
|
|
|
w += rw.RuneWidth(runes[i])
|
|
|
|
if w <= m.Width+1 {
|
|
|
|
i++
|
|
|
|
}
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
2020-06-12 03:43:32 +03:00
|
|
|
|
|
|
|
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-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 06:43:52 +03:00
|
|
|
// colorText colorizes a given string according to the TextColor value of the
|
2020-05-26 02:57:58 +03:00
|
|
|
// model.
|
2020-02-25 06:43:52 +03:00
|
|
|
func (m *Model) colorText(s string) string {
|
|
|
|
return termenv.
|
|
|
|
String(s).
|
2020-02-25 06:57:48 +03:00
|
|
|
Foreground(color(m.TextColor)).
|
2020-05-26 02:57:58 +03:00
|
|
|
Background(color(m.BackgroundColor)).
|
2020-02-25 06:57:48 +03:00
|
|
|
String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// colorPlaceholder colorizes a given string according to the TextColor value
|
2020-05-26 02:57:58 +03:00
|
|
|
// of the model.
|
2020-02-25 06:57:48 +03:00
|
|
|
func (m *Model) colorPlaceholder(s string) string {
|
|
|
|
return termenv.
|
|
|
|
String(s).
|
|
|
|
Foreground(color(m.PlaceholderColor)).
|
2020-05-26 02:57:58 +03:00
|
|
|
Background(color(m.BackgroundColor)).
|
2020-02-25 06:43:52 +03:00
|
|
|
String()
|
|
|
|
}
|
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
func (m *Model) wordLeft() {
|
|
|
|
if m.pos == 0 || len(m.value) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
i := m.pos - 1
|
|
|
|
|
|
|
|
for i >= 0 {
|
2020-06-12 01:31:17 +03:00
|
|
|
if unicode.IsSpace(m.value[i]) {
|
2020-05-26 02:57:58 +03:00
|
|
|
m.pos--
|
|
|
|
i--
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i >= 0 {
|
2020-06-12 01:31:17 +03:00
|
|
|
if !unicode.IsSpace(m.value[i]) {
|
2020-05-26 02:57:58 +03:00
|
|
|
m.pos--
|
|
|
|
i--
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Model) wordRight() {
|
|
|
|
if m.pos >= len(m.value) || len(m.value) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-01-18 18:57:38 +03:00
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
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.
|
2020-04-23 19:06:08 +03:00
|
|
|
func NewModel() Model {
|
2020-01-18 18:57:38 +03:00
|
|
|
return Model{
|
2020-02-02 06:26:35 +03:00
|
|
|
Prompt: "> ",
|
2020-05-26 02:57:58 +03:00
|
|
|
BlinkSpeed: defaultBlinkSpeed,
|
2020-02-02 06:26:35 +03:00
|
|
|
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-06-12 01:31:17 +03:00
|
|
|
value: nil,
|
2020-02-25 06:57:48 +03:00
|
|
|
focus: false,
|
|
|
|
blink: true,
|
|
|
|
pos: 0,
|
2020-01-18 18:57:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 02:57:58 +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-05-26 02:57:58 +03:00
|
|
|
if len(m.value) > 0 {
|
2020-06-12 01:31:17 +03:00
|
|
|
m.value = append(m.value[:m.pos-1], m.value[m.pos:]...)
|
2020-01-18 18:57:38 +03:00
|
|
|
m.pos--
|
|
|
|
}
|
|
|
|
case tea.KeyLeft:
|
2020-05-26 02:57:58 +03:00
|
|
|
if msg.Alt { // alt+left arrow, back one word
|
|
|
|
m.wordLeft()
|
|
|
|
break
|
|
|
|
}
|
2020-01-18 18:57:38 +03:00
|
|
|
if m.pos > 0 {
|
|
|
|
m.pos--
|
|
|
|
}
|
|
|
|
case tea.KeyRight:
|
2020-05-26 02:57:58 +03:00
|
|
|
if msg.Alt { // alt+right arrow, forward one word
|
|
|
|
m.wordRight()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if m.pos < len(m.value) {
|
2020-01-18 18:57:38 +03:00
|
|
|
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
|
2020-04-23 19:06:08 +03:00
|
|
|
case tea.KeyCtrlA: // ^A, go to beginning
|
2020-05-26 02:57:58 +03:00
|
|
|
m.CursorStart()
|
2020-01-30 06:19:47 +03:00
|
|
|
case tea.KeyCtrlD: // ^D, delete char under cursor
|
2020-05-26 02:57:58 +03:00
|
|
|
if len(m.value) > 0 && m.pos < len(m.value) {
|
2020-06-12 01:31:17 +03:00
|
|
|
m.value = append(m.value[:m.pos], m.value[m.pos+1:]...)
|
2020-01-30 06:19:47 +03:00
|
|
|
}
|
2020-04-23 19:06:08 +03:00
|
|
|
case tea.KeyCtrlE: // ^E, go to end
|
2020-05-26 02:57:58 +03:00
|
|
|
m.CursorEnd()
|
2020-01-30 05:51:52 +03:00
|
|
|
case tea.KeyCtrlK: // ^K, kill text after cursor
|
2020-05-26 02:57:58 +03:00
|
|
|
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
|
2020-05-26 02:57:58 +03:00
|
|
|
m.value = m.value[m.pos:]
|
2020-01-30 06:25:39 +03:00
|
|
|
m.pos = 0
|
2020-05-26 02:57:58 +03:00
|
|
|
m.offset = 0
|
2020-04-02 03:51:09 +03:00
|
|
|
case tea.KeyRune: // input a regular character
|
2020-05-26 02:57:58 +03:00
|
|
|
|
|
|
|
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 {
|
2020-06-12 01:31:17 +03:00
|
|
|
m.value = append(m.value[:m.pos], append([]rune{msg.Rune}, m.value[m.pos:]...)...)
|
2020-04-02 03:51:09 +03:00
|
|
|
m.pos++
|
|
|
|
}
|
2020-01-18 18:57:38 +03:00
|
|
|
}
|
|
|
|
|
2020-04-22 21:28:22 +03:00
|
|
|
case ErrMsg:
|
|
|
|
m.Err = msg
|
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
case BlinkMsg:
|
2020-01-18 18:57:38 +03:00
|
|
|
m.blink = !m.blink
|
2020-05-26 02:57:58 +03:00
|
|
|
return m, Blink(m)
|
2020-01-18 18:57:38 +03:00
|
|
|
}
|
2020-05-26 02:57:58 +03:00
|
|
|
|
|
|
|
m.handleOverflow()
|
|
|
|
|
|
|
|
return m, nil
|
2020-01-18 18:57:38 +03:00
|
|
|
}
|
|
|
|
|
2020-05-26 02:57:58 +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
|
2020-06-12 01:31:17 +03:00
|
|
|
if m.value == nil && m.Placeholder != "" {
|
2020-02-02 06:35:21 +03:00
|
|
|
return placeholderView(m)
|
2020-02-02 06:26:35 +03:00
|
|
|
}
|
|
|
|
|
2020-06-12 03:43:32 +03:00
|
|
|
value := m.value[m.offset:m.offsetRight]
|
2020-05-26 02:57:58 +03:00
|
|
|
pos := m.pos - m.offset
|
|
|
|
|
2020-06-12 01:31:17 +03:00
|
|
|
v := m.colorText(string(value[:pos]))
|
2020-02-02 06:26:35 +03:00
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
if pos < len(value) {
|
2020-06-12 01:31:17 +03:00
|
|
|
v += cursorView(string(value[pos]), m) // cursor and text under it
|
|
|
|
v += m.colorText(string(value[pos+1:])) // text after cursor
|
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
|
|
|
}
|
2020-05-26 02:57:58 +03:00
|
|
|
|
|
|
|
// If a max width and background color were set fill the empty spaces with
|
|
|
|
// the background color.
|
2020-06-12 03:43:32 +03:00
|
|
|
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) {
|
2020-05-26 02:57:58 +03:00
|
|
|
padding++
|
|
|
|
}
|
|
|
|
v += strings.Repeat(
|
|
|
|
termenv.String(" ").Background(color(m.BackgroundColor)).String(),
|
|
|
|
padding,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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-05-26 02:57:58 +03:00
|
|
|
// 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 {
|
2020-05-26 02:57:58 +03:00
|
|
|
if m.TextColor != "" || m.BackgroundColor != "" {
|
|
|
|
return termenv.String(s).
|
|
|
|
Foreground(color(m.TextColor)).
|
|
|
|
Background(color(m.BackgroundColor)).
|
|
|
|
String()
|
|
|
|
}
|
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)).
|
2020-05-26 02:57:58 +03:00
|
|
|
Background(color(m.BackgroundColor)).
|
2020-04-17 21:38:11 +03:00
|
|
|
Reverse().
|
|
|
|
String()
|
2020-01-18 18:57:38 +03:00
|
|
|
}
|
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
// Blink is a command used to time the cursor blinking.
|
|
|
|
func Blink(model Model) tea.Cmd {
|
2020-05-05 01:54:26 +03:00
|
|
|
return func() tea.Msg {
|
2020-05-26 02:57:58 +03:00
|
|
|
time.Sleep(model.BlinkSpeed)
|
|
|
|
return BlinkMsg{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 03:43:32 +03:00
|
|
|
func clamp(v, low, high int) int {
|
|
|
|
return min(high, max(low, v))
|
|
|
|
}
|
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
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
|
2020-01-18 18:57:38 +03:00
|
|
|
}
|