2020-04-23 19:06:08 +03:00
|
|
|
package textinput
|
2020-01-18 18:57:38 +03:00
|
|
|
|
|
|
|
import (
|
2020-10-27 02:24:42 +03:00
|
|
|
"context"
|
2020-05-26 02:57:58 +03:00
|
|
|
"strings"
|
2021-06-02 04:24:57 +03:00
|
|
|
"sync"
|
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-07-21 23:49:27 +03:00
|
|
|
"github.com/atotto/clipboard"
|
2020-05-26 02:57:58 +03:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2021-04-13 01:16:28 +03:00
|
|
|
"github.com/charmbracelet/lipgloss"
|
2020-06-12 03:43:32 +03:00
|
|
|
rw "github.com/mattn/go-runewidth"
|
2020-01-18 18:57:38 +03:00
|
|
|
)
|
|
|
|
|
2020-10-24 06:44:51 +03:00
|
|
|
const defaultBlinkSpeed = time.Millisecond * 530
|
2020-10-22 01:17:41 +03:00
|
|
|
|
2021-06-02 04:24:57 +03:00
|
|
|
// Internal ID management for text inputs. Necessary for blink integrity when
|
|
|
|
// multiple text inputs are involved.
|
|
|
|
var (
|
|
|
|
lastID int
|
|
|
|
idMtx sync.Mutex
|
|
|
|
)
|
|
|
|
|
|
|
|
// Return the next ID we should use on the Model.
|
|
|
|
func nextID() int {
|
|
|
|
idMtx.Lock()
|
|
|
|
defer idMtx.Unlock()
|
|
|
|
lastID++
|
|
|
|
return lastID
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialBlinkMsg initializes cursor blinking.
|
|
|
|
type initialBlinkMsg struct{}
|
|
|
|
|
|
|
|
// blinkMsg signals that the cursor should blink. It contains metadata that
|
|
|
|
// allows us to tell if the blink message is the one we're expecting.
|
|
|
|
type blinkMsg struct {
|
|
|
|
id int
|
|
|
|
tag int
|
|
|
|
}
|
|
|
|
|
|
|
|
// blinkCanceled is sent when a blink operation is canceled.
|
2020-10-27 02:24:42 +03:00
|
|
|
type blinkCanceled struct{}
|
|
|
|
|
2021-06-02 04:24:57 +03:00
|
|
|
// Internal messages for clipboard operations.
|
2020-10-27 02:24:42 +03:00
|
|
|
type pasteMsg string
|
|
|
|
type pasteErrMsg struct{ error }
|
|
|
|
|
2020-10-25 00:18:43 +03:00
|
|
|
// EchoMode sets the input behavior of the text input field.
|
|
|
|
type EchoMode int
|
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
const (
|
2020-10-22 01:17:41 +03:00
|
|
|
// EchoNormal displays text as is. This is the default behavior.
|
|
|
|
EchoNormal EchoMode = iota
|
|
|
|
|
|
|
|
// EchoPassword displays the EchoCharacter mask instead of actual
|
|
|
|
// characters. This is commonly used for password fields.
|
|
|
|
EchoPassword
|
|
|
|
|
|
|
|
// EchoNone displays nothing as characters are entered. This is commonly
|
|
|
|
// seen for password fields on the command line.
|
|
|
|
EchoNone
|
|
|
|
|
2022-01-19 01:15:38 +03:00
|
|
|
// EchoOnEdit.
|
2020-05-26 02:57:58 +03:00
|
|
|
)
|
|
|
|
|
2020-10-27 05:56:53 +03:00
|
|
|
// blinkCtx manages cursor blinking.
|
2020-10-27 02:24:42 +03:00
|
|
|
type blinkCtx struct {
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
}
|
2020-10-22 01:17:41 +03:00
|
|
|
|
2021-06-02 00:02:04 +03:00
|
|
|
// CursorMode describes the behavior of the cursor.
|
|
|
|
type CursorMode int
|
2020-02-25 06:57:48 +03:00
|
|
|
|
2021-06-02 00:02:04 +03:00
|
|
|
// Available cursor modes.
|
2020-10-27 02:24:42 +03:00
|
|
|
const (
|
2021-06-02 00:02:04 +03:00
|
|
|
CursorBlink CursorMode = iota
|
|
|
|
CursorStatic
|
|
|
|
CursorHide
|
2020-10-27 02:24:42 +03:00
|
|
|
)
|
2020-10-25 03:20:45 +03:00
|
|
|
|
2021-06-02 00:02:04 +03:00
|
|
|
// String returns a the cursor mode in a human-readable format. This method is
|
|
|
|
// provisional and for informational purposes only.
|
|
|
|
func (c CursorMode) String() string {
|
|
|
|
return [...]string{
|
|
|
|
"blink",
|
|
|
|
"static",
|
|
|
|
"hidden",
|
|
|
|
}[c]
|
|
|
|
}
|
|
|
|
|
2022-02-09 01:11:45 +03:00
|
|
|
// ValidateFunc is a function that returns an error if the input is invalid.
|
|
|
|
type ValidateFunc func(string) error
|
|
|
|
|
2020-10-22 01:17:41 +03:00
|
|
|
// Model is the Bubble Tea model for this text input element.
|
2020-01-18 18:57:38 +03:00
|
|
|
type Model struct {
|
2020-10-22 01:17:41 +03:00
|
|
|
Err error
|
|
|
|
|
2021-04-13 01:16:28 +03:00
|
|
|
// General settings.
|
|
|
|
Prompt string
|
|
|
|
Placeholder string
|
|
|
|
BlinkSpeed time.Duration
|
|
|
|
EchoMode EchoMode
|
|
|
|
EchoCharacter rune
|
|
|
|
|
2021-04-13 02:33:16 +03:00
|
|
|
// Styles. These will be applied as inline styles.
|
|
|
|
//
|
|
|
|
// For an introduction to styling with Lip Gloss see:
|
|
|
|
// https://github.com/charmbracelet/lipgloss
|
2021-04-13 01:16:28 +03:00
|
|
|
PromptStyle lipgloss.Style
|
|
|
|
TextStyle lipgloss.Style
|
|
|
|
BackgroundStyle lipgloss.Style
|
|
|
|
PlaceholderStyle lipgloss.Style
|
|
|
|
CursorStyle lipgloss.Style
|
2020-10-22 01:17:41 +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
|
|
|
|
|
2021-06-02 04:24:57 +03:00
|
|
|
// The ID of this Model as it relates to other textinput Models.
|
|
|
|
id int
|
|
|
|
|
|
|
|
// The ID of the blink message we're expecting to receive.
|
|
|
|
blinkTag int
|
|
|
|
|
2020-10-22 01:17:41 +03:00
|
|
|
// Underlying text value.
|
2020-06-12 01:31:17 +03:00
|
|
|
value []rune
|
2020-05-26 02:57:58 +03:00
|
|
|
|
2021-06-02 00:02:04 +03:00
|
|
|
// focus indicates whether user input focus should be on this input
|
|
|
|
// component. When false, ignore keyboard input and hide the cursor.
|
2020-02-18 01:00:01 +03:00
|
|
|
focus bool
|
2020-02-18 00:00:53 +03:00
|
|
|
|
2020-10-22 01:17:41 +03:00
|
|
|
// Cursor blink state.
|
2020-02-25 06:57:48 +03:00
|
|
|
blink bool
|
2020-05-26 02:57:58 +03:00
|
|
|
|
2020-10-22 01:17:41 +03:00
|
|
|
// Cursor position.
|
2020-05-26 02:57:58 +03:00
|
|
|
pos int
|
|
|
|
|
|
|
|
// Used to emulate a viewport when width is set and the content is
|
2020-10-22 01:17:41 +03:00
|
|
|
// overflowing.
|
2020-06-12 03:43:32 +03:00
|
|
|
offset int
|
|
|
|
offsetRight int
|
2020-10-25 00:18:43 +03:00
|
|
|
|
|
|
|
// Used to manage cursor blink
|
2020-10-27 02:24:42 +03:00
|
|
|
blinkCtx *blinkCtx
|
|
|
|
|
|
|
|
// cursorMode determines the behavior of the cursor
|
2021-06-02 00:02:04 +03:00
|
|
|
cursorMode CursorMode
|
2022-02-09 01:11:45 +03:00
|
|
|
|
|
|
|
// Validate is a function that checks whether or not the text within the
|
|
|
|
// input is valid. If it is not valid, the `Err` field will be set to the
|
|
|
|
// error returned by the function. If the function is not defined, all
|
|
|
|
// input is considered valid.
|
|
|
|
Validate ValidateFunc
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
2022-02-09 01:11:45 +03:00
|
|
|
// New creates a new model with default settings.
|
2022-01-11 02:27:33 +03:00
|
|
|
func New() Model {
|
2020-10-25 22:50:52 +03:00
|
|
|
return Model{
|
|
|
|
Prompt: "> ",
|
|
|
|
BlinkSpeed: defaultBlinkSpeed,
|
|
|
|
EchoCharacter: '*',
|
|
|
|
CharLimit: 0,
|
2021-04-13 01:16:28 +03:00
|
|
|
PlaceholderStyle: lipgloss.NewStyle().Foreground(lipgloss.Color("240")),
|
2020-10-25 22:50:52 +03:00
|
|
|
|
2021-06-02 04:24:57 +03:00
|
|
|
id: nextID(),
|
2020-10-27 02:24:42 +03:00
|
|
|
value: nil,
|
|
|
|
focus: false,
|
|
|
|
blink: true,
|
|
|
|
pos: 0,
|
2021-06-02 00:02:04 +03:00
|
|
|
cursorMode: CursorBlink,
|
2020-10-27 02:24:42 +03:00
|
|
|
|
|
|
|
blinkCtx: &blinkCtx{
|
|
|
|
ctx: context.Background(),
|
|
|
|
},
|
2020-10-25 22:50:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-11 02:27:33 +03:00
|
|
|
// NewModel creates a new model with default settings.
|
|
|
|
//
|
|
|
|
// Deprecated. Use New instead.
|
|
|
|
var NewModel = New
|
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
// SetValue sets the value of the text input.
|
|
|
|
func (m *Model) SetValue(s string) {
|
2022-02-09 01:11:45 +03:00
|
|
|
if m.Validate != nil {
|
|
|
|
if err := m.Validate(s); err != nil {
|
|
|
|
m.Err = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.Err = nil
|
|
|
|
|
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
|
|
|
}
|
2022-07-05 15:15:32 +03:00
|
|
|
if m.pos == 0 || m.pos > len(m.value) {
|
2021-04-26 17:51:59 +03:00
|
|
|
m.setCursor(len(m.value))
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-04-26 18:02:25 +03:00
|
|
|
// Cursor returns the cursor position.
|
|
|
|
func (m Model) Cursor() int {
|
|
|
|
return m.pos
|
|
|
|
}
|
|
|
|
|
2022-03-01 03:58:14 +03:00
|
|
|
// Blink returns whether or not to draw the cursor.
|
|
|
|
func (m Model) Blink() bool {
|
|
|
|
return m.blink
|
|
|
|
}
|
|
|
|
|
2021-04-26 17:51:59 +03:00
|
|
|
// SetCursor moves the cursor to the given position. If the position is
|
2020-10-22 01:17:41 +03:00
|
|
|
// out of bounds the cursor will be moved to the start or end accordingly.
|
2021-04-26 17:51:59 +03:00
|
|
|
func (m *Model) SetCursor(pos int) {
|
|
|
|
m.setCursor(pos)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setCursor moves the cursor to the given position and returns whether or not
|
|
|
|
// the cursor blink should be reset. If the position is out of bounds the
|
|
|
|
// cursor will be moved to the start or end accordingly.
|
|
|
|
func (m *Model) setCursor(pos int) bool {
|
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-10-27 04:14:48 +03:00
|
|
|
|
|
|
|
// Show the cursor unless it's been explicitly hidden
|
2021-06-02 00:02:04 +03:00
|
|
|
m.blink = m.cursorMode == CursorHide
|
2020-10-25 00:18:43 +03:00
|
|
|
|
2020-10-27 05:56:53 +03:00
|
|
|
// Reset cursor blink if necessary
|
2021-06-02 00:02:04 +03:00
|
|
|
return m.cursorMode == CursorBlink
|
2020-01-18 18:57:38 +03:00
|
|
|
}
|
|
|
|
|
2021-04-26 17:51:59 +03:00
|
|
|
// CursorStart moves the cursor to the start of the input field.
|
|
|
|
func (m *Model) CursorStart() {
|
|
|
|
m.cursorStart()
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
2021-04-26 17:51:59 +03:00
|
|
|
// cursorStart moves the cursor to the start of the input field and returns
|
|
|
|
// whether or not the curosr blink should be reset.
|
|
|
|
func (m *Model) cursorStart() bool {
|
|
|
|
return m.setCursor(0)
|
|
|
|
}
|
|
|
|
|
2022-01-19 01:15:38 +03:00
|
|
|
// CursorEnd moves the cursor to the end of the input field.
|
2021-04-26 17:51:59 +03:00
|
|
|
func (m *Model) CursorEnd() {
|
|
|
|
m.cursorEnd()
|
|
|
|
}
|
|
|
|
|
2021-06-02 00:02:04 +03:00
|
|
|
// CursorMode returns the model's cursor mode. For available cursor modes, see
|
|
|
|
// type CursorMode.
|
|
|
|
func (m Model) CursorMode() CursorMode {
|
|
|
|
return m.cursorMode
|
|
|
|
}
|
|
|
|
|
2022-02-09 01:11:45 +03:00
|
|
|
// SetCursorMode sets the model's cursor mode. This method returns a command.
|
2021-06-02 00:02:04 +03:00
|
|
|
//
|
|
|
|
// For available cursor modes, see type CursorMode.
|
|
|
|
func (m *Model) SetCursorMode(mode CursorMode) tea.Cmd {
|
|
|
|
m.cursorMode = mode
|
|
|
|
m.blink = m.cursorMode == CursorHide || !m.focus
|
|
|
|
if mode == CursorBlink {
|
|
|
|
return Blink
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-26 17:51:59 +03:00
|
|
|
// cursorEnd moves the cursor to the end of the input field and returns whether
|
2021-06-02 04:24:57 +03:00
|
|
|
// the cursor should blink should reset.
|
2021-04-26 17:51:59 +03:00
|
|
|
func (m *Model) cursorEnd() bool {
|
|
|
|
return m.setCursor(len(m.value))
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2021-06-02 00:02:04 +03:00
|
|
|
// Focus sets the focus state on the model. When the model is in focus it can
|
|
|
|
// receive keyboard input and the cursor will be hidden.
|
2021-06-02 01:37:11 +03:00
|
|
|
func (m *Model) Focus() tea.Cmd {
|
2020-02-18 01:00:01 +03:00
|
|
|
m.focus = true
|
2021-06-02 00:02:04 +03:00
|
|
|
m.blink = m.cursorMode == CursorHide // show the cursor unless we've explicitly hidden it
|
2021-06-02 01:37:11 +03:00
|
|
|
|
|
|
|
if m.cursorMode == CursorBlink && m.focus {
|
|
|
|
return m.blinkCmd()
|
|
|
|
}
|
|
|
|
return nil
|
2020-02-18 01:00:01 +03:00
|
|
|
}
|
|
|
|
|
2021-06-02 00:02:04 +03:00
|
|
|
// Blur removes the focus state on the model. When the model is blurred it can
|
|
|
|
// not receive keyboard input and the cursor will be hidden.
|
2020-02-18 01:00:01 +03:00
|
|
|
func (m *Model) Blur() {
|
|
|
|
m.focus = false
|
|
|
|
m.blink = true
|
|
|
|
}
|
|
|
|
|
2020-10-27 02:24:42 +03:00
|
|
|
// Reset sets the input to its default state with no input. Returns whether
|
|
|
|
// or not the cursor blink should reset.
|
|
|
|
func (m *Model) Reset() bool {
|
2020-06-12 01:31:17 +03:00
|
|
|
m.value = nil
|
2021-04-26 17:51:59 +03:00
|
|
|
return m.setCursor(0)
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
2020-10-27 02:24:42 +03:00
|
|
|
// handle a clipboard paste event, if supported. Returns whether or not the
|
2021-06-02 04:24:57 +03:00
|
|
|
// cursor blink should reset.
|
2021-03-12 03:37:19 +03:00
|
|
|
func (m *Model) handlePaste(v string) bool {
|
2020-10-25 03:20:45 +03:00
|
|
|
paste := []rune(v)
|
2020-07-21 23:49:27 +03:00
|
|
|
|
2020-10-25 01:15:44 +03:00
|
|
|
var availSpace int
|
|
|
|
if m.CharLimit > 0 {
|
|
|
|
availSpace = m.CharLimit - len(m.value)
|
|
|
|
}
|
2020-07-21 23:49:27 +03:00
|
|
|
|
|
|
|
// If the char limit's been reached cancel
|
|
|
|
if m.CharLimit > 0 && availSpace <= 0 {
|
2021-03-12 03:37:19 +03:00
|
|
|
return false
|
2020-07-21 23:49:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there's not enough space to paste the whole thing cut the pasted
|
|
|
|
// runes down so they'll fit
|
2020-10-25 01:15:44 +03:00
|
|
|
if m.CharLimit > 0 && availSpace < len(paste) {
|
2020-07-21 23:49:27 +03:00
|
|
|
paste = paste[:len(paste)-availSpace]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stuff before and after the cursor
|
|
|
|
head := m.value[:m.pos]
|
|
|
|
tailSrc := m.value[m.pos:]
|
|
|
|
tail := make([]rune, len(tailSrc))
|
|
|
|
copy(tail, tailSrc)
|
|
|
|
|
2022-02-09 01:11:45 +03:00
|
|
|
oldPos := m.pos
|
|
|
|
|
2020-07-21 23:49:27 +03:00
|
|
|
// Insert pasted runes
|
|
|
|
for _, r := range paste {
|
|
|
|
head = append(head, r)
|
2020-10-25 01:17:59 +03:00
|
|
|
m.pos++
|
2020-10-25 01:15:44 +03:00
|
|
|
if m.CharLimit > 0 {
|
|
|
|
availSpace--
|
|
|
|
if availSpace <= 0 {
|
|
|
|
break
|
|
|
|
}
|
2020-07-21 23:49:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put it all back together
|
2022-02-09 01:11:45 +03:00
|
|
|
value := append(head, tail...)
|
|
|
|
m.SetValue(string(value))
|
|
|
|
|
|
|
|
if m.Err != nil {
|
|
|
|
m.pos = oldPos
|
|
|
|
}
|
2020-10-25 01:17:59 +03:00
|
|
|
|
2020-10-27 02:24:42 +03:00
|
|
|
// Reset blink state if necessary and run overflow checks
|
2021-04-26 17:51:59 +03:00
|
|
|
return m.setCursor(m.pos)
|
2020-07-21 23:49:27 +03:00
|
|
|
}
|
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-06-21 06:30:19 +03:00
|
|
|
// Correct right offset if we've deleted characters
|
|
|
|
m.offsetRight = min(m.offsetRight, len(m.value))
|
|
|
|
|
2020-06-12 03:43:32 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-26 18:28:37 +03:00
|
|
|
// deleteBeforeCursor deletes all text before the cursor. Returns whether or
|
|
|
|
// not the cursor blink should be reset.
|
|
|
|
func (m *Model) deleteBeforeCursor() bool {
|
|
|
|
m.value = m.value[m.pos:]
|
|
|
|
m.offset = 0
|
|
|
|
return m.setCursor(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// deleteAfterCursor deletes all text after the cursor. Returns whether or not
|
|
|
|
// the cursor blink should be reset. If input is masked delete everything after
|
|
|
|
// the cursor so as not to reveal word breaks in the masked input.
|
|
|
|
func (m *Model) deleteAfterCursor() bool {
|
|
|
|
m.value = m.value[:m.pos]
|
|
|
|
return m.setCursor(len(m.value))
|
|
|
|
}
|
|
|
|
|
2020-10-27 02:24:42 +03:00
|
|
|
// deleteWordLeft deletes the word left to the cursor. Returns whether or not
|
|
|
|
// the cursor blink should be reset.
|
2021-03-12 03:37:19 +03:00
|
|
|
func (m *Model) deleteWordLeft() bool {
|
2020-10-25 07:35:20 +03:00
|
|
|
if m.pos == 0 || len(m.value) == 0 {
|
2021-03-12 03:37:19 +03:00
|
|
|
return false
|
2020-10-25 07:35:20 +03:00
|
|
|
}
|
|
|
|
|
2021-04-26 18:28:37 +03:00
|
|
|
if m.EchoMode != EchoNormal {
|
|
|
|
return m.deleteBeforeCursor()
|
|
|
|
}
|
|
|
|
|
2022-01-20 04:53:10 +03:00
|
|
|
// Linter note: it's critical that we acquire the initial cursor position
|
|
|
|
// here prior to altering it via SetCursor() below. As such, moving this
|
|
|
|
// call into the corresponding if clause does not apply here.
|
|
|
|
oldPos := m.pos //nolint:ifshort
|
|
|
|
|
2021-04-26 17:51:59 +03:00
|
|
|
blink := m.setCursor(m.pos - 1)
|
2020-10-25 07:35:20 +03:00
|
|
|
for unicode.IsSpace(m.value[m.pos]) {
|
2021-12-31 18:33:55 +03:00
|
|
|
if m.pos <= 0 {
|
|
|
|
break
|
|
|
|
}
|
2020-10-25 07:35:20 +03:00
|
|
|
// ignore series of whitespace before cursor
|
2021-04-26 17:51:59 +03:00
|
|
|
blink = m.setCursor(m.pos - 1)
|
2020-10-25 07:35:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for m.pos > 0 {
|
|
|
|
if !unicode.IsSpace(m.value[m.pos]) {
|
2021-04-26 17:51:59 +03:00
|
|
|
blink = m.setCursor(m.pos - 1)
|
2020-10-25 07:35:20 +03:00
|
|
|
} else {
|
|
|
|
if m.pos > 0 {
|
|
|
|
// keep the previous space
|
2021-04-26 17:51:59 +03:00
|
|
|
blink = m.setCursor(m.pos + 1)
|
2020-10-25 07:35:20 +03:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 04:53:10 +03:00
|
|
|
if oldPos > len(m.value) {
|
|
|
|
m.value = m.value[:m.pos]
|
|
|
|
} else {
|
|
|
|
m.value = append(m.value[:m.pos], m.value[oldPos:]...)
|
|
|
|
}
|
2020-10-27 02:24:42 +03:00
|
|
|
|
2021-03-12 03:37:19 +03:00
|
|
|
return blink
|
2020-10-25 07:35:20 +03:00
|
|
|
}
|
|
|
|
|
2020-10-27 02:24:42 +03:00
|
|
|
// deleteWordRight deletes the word right to the cursor. Returns whether or not
|
2021-04-26 18:28:37 +03:00
|
|
|
// the cursor blink should be reset. If input is masked delete everything after
|
|
|
|
// the cursor so as not to reveal word breaks in the masked input.
|
2021-03-12 03:37:19 +03:00
|
|
|
func (m *Model) deleteWordRight() bool {
|
2020-10-25 07:35:20 +03:00
|
|
|
if m.pos >= len(m.value) || len(m.value) == 0 {
|
2021-03-12 03:37:19 +03:00
|
|
|
return false
|
2020-10-25 07:35:20 +03:00
|
|
|
}
|
|
|
|
|
2021-04-26 18:28:37 +03:00
|
|
|
if m.EchoMode != EchoNormal {
|
|
|
|
return m.deleteAfterCursor()
|
|
|
|
}
|
|
|
|
|
2022-01-20 04:53:54 +03:00
|
|
|
oldPos := m.pos
|
2021-04-26 17:51:59 +03:00
|
|
|
m.setCursor(m.pos + 1)
|
2020-10-25 07:35:20 +03:00
|
|
|
for unicode.IsSpace(m.value[m.pos]) {
|
|
|
|
// ignore series of whitespace after cursor
|
2021-04-26 17:51:59 +03:00
|
|
|
m.setCursor(m.pos + 1)
|
2021-12-31 18:33:55 +03:00
|
|
|
|
|
|
|
if m.pos >= len(m.value) {
|
|
|
|
break
|
|
|
|
}
|
2020-10-25 07:35:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for m.pos < len(m.value) {
|
|
|
|
if !unicode.IsSpace(m.value[m.pos]) {
|
2021-04-26 17:51:59 +03:00
|
|
|
m.setCursor(m.pos + 1)
|
2020-10-25 07:35:20 +03:00
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.pos > len(m.value) {
|
2022-01-20 04:53:54 +03:00
|
|
|
m.value = m.value[:oldPos]
|
2020-10-25 07:35:20 +03:00
|
|
|
} else {
|
2022-01-20 04:53:54 +03:00
|
|
|
m.value = append(m.value[:oldPos], m.value[m.pos:]...)
|
2020-10-25 07:35:20 +03:00
|
|
|
}
|
2020-10-27 02:24:42 +03:00
|
|
|
|
2022-01-20 04:53:54 +03:00
|
|
|
return m.setCursor(oldPos)
|
2020-10-25 07:35:20 +03:00
|
|
|
}
|
|
|
|
|
2020-10-27 02:24:42 +03:00
|
|
|
// wordLeft moves the cursor one word to the left. Returns whether or not the
|
2021-04-26 18:14:25 +03:00
|
|
|
// cursor blink should be reset. If input is masked, move input to the start
|
|
|
|
// so as not to reveal word breaks in the masked input.
|
2021-03-12 03:37:19 +03:00
|
|
|
func (m *Model) wordLeft() bool {
|
2020-05-26 02:57:58 +03:00
|
|
|
if m.pos == 0 || len(m.value) == 0 {
|
2021-03-12 03:37:19 +03:00
|
|
|
return false
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
2021-04-26 18:14:25 +03:00
|
|
|
if m.EchoMode != EchoNormal {
|
|
|
|
return m.cursorStart()
|
|
|
|
}
|
|
|
|
|
2021-03-12 03:37:19 +03:00
|
|
|
blink := false
|
2020-05-26 02:57:58 +03:00
|
|
|
i := m.pos - 1
|
|
|
|
for i >= 0 {
|
2020-06-12 01:31:17 +03:00
|
|
|
if unicode.IsSpace(m.value[i]) {
|
2021-04-26 17:51:59 +03:00
|
|
|
blink = m.setCursor(m.pos - 1)
|
2020-05-26 02:57:58 +03:00
|
|
|
i--
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i >= 0 {
|
2020-06-12 01:31:17 +03:00
|
|
|
if !unicode.IsSpace(m.value[i]) {
|
2021-04-26 17:51:59 +03:00
|
|
|
blink = m.setCursor(m.pos - 1)
|
2020-05-26 02:57:58 +03:00
|
|
|
i--
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-10-27 02:24:42 +03:00
|
|
|
|
2021-03-12 03:37:19 +03:00
|
|
|
return blink
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
2020-10-27 02:24:42 +03:00
|
|
|
// wordRight moves the cursor one word to the right. Returns whether or not the
|
2021-04-26 18:14:25 +03:00
|
|
|
// cursor blink should be reset. If the input is masked, move input to the end
|
|
|
|
// so as not to reveal word breaks in the masked input.
|
2021-03-12 03:37:19 +03:00
|
|
|
func (m *Model) wordRight() bool {
|
2020-05-26 02:57:58 +03:00
|
|
|
if m.pos >= len(m.value) || len(m.value) == 0 {
|
2021-03-12 03:37:19 +03:00
|
|
|
return false
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
2020-01-18 18:57:38 +03:00
|
|
|
|
2021-04-26 18:14:25 +03:00
|
|
|
if m.EchoMode != EchoNormal {
|
|
|
|
return m.cursorEnd()
|
|
|
|
}
|
|
|
|
|
2021-03-12 03:37:19 +03:00
|
|
|
blink := false
|
2020-05-26 02:57:58 +03:00
|
|
|
i := m.pos
|
|
|
|
for i < len(m.value) {
|
2020-08-22 13:41:37 +03:00
|
|
|
if unicode.IsSpace(m.value[i]) {
|
2021-04-26 17:51:59 +03:00
|
|
|
blink = m.setCursor(m.pos + 1)
|
2020-05-26 02:57:58 +03:00
|
|
|
i++
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i < len(m.value) {
|
2020-08-22 13:41:37 +03:00
|
|
|
if !unicode.IsSpace(m.value[i]) {
|
2021-04-26 17:51:59 +03:00
|
|
|
blink = m.setCursor(m.pos + 1)
|
2020-05-26 02:57:58 +03:00
|
|
|
i++
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-10-27 02:24:42 +03:00
|
|
|
|
2021-03-12 03:37:19 +03:00
|
|
|
return blink
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
2020-10-22 01:17:41 +03:00
|
|
|
func (m Model) echoTransform(v string) string {
|
|
|
|
switch m.EchoMode {
|
|
|
|
case EchoPassword:
|
|
|
|
return strings.Repeat(string(m.EchoCharacter), rw.StringWidth(v))
|
|
|
|
case EchoNone:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
default:
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 00:18:43 +03:00
|
|
|
// Update is the Bubble Tea update loop.
|
2020-10-25 22:50:52 +03:00
|
|
|
func (m Model) Update(msg tea.Msg) (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-10-27 02:24:42 +03:00
|
|
|
var resetBlink bool
|
|
|
|
|
2020-01-18 18:57:38 +03:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.KeyMsg:
|
|
|
|
switch msg.Type {
|
2022-07-31 20:15:31 +03:00
|
|
|
case tea.KeyBackspace, tea.KeyCtrlH: // delete character before cursor
|
2022-02-09 01:11:45 +03:00
|
|
|
m.Err = nil
|
|
|
|
|
2020-10-27 10:50:22 +03:00
|
|
|
if msg.Alt {
|
2020-10-27 02:24:42 +03:00
|
|
|
resetBlink = m.deleteWordLeft()
|
2020-10-27 10:50:22 +03:00
|
|
|
} else {
|
|
|
|
if len(m.value) > 0 {
|
|
|
|
m.value = append(m.value[:max(0, m.pos-1)], m.value[m.pos:]...)
|
|
|
|
if m.pos > 0 {
|
2021-04-26 17:51:59 +03:00
|
|
|
resetBlink = m.setCursor(m.pos - 1)
|
2020-10-27 10:50:22 +03:00
|
|
|
}
|
2020-07-22 01:22:18 +03:00
|
|
|
}
|
2020-01-18 18:57:38 +03:00
|
|
|
}
|
2020-10-27 03:59:09 +03:00
|
|
|
case tea.KeyLeft, tea.KeyCtrlB:
|
2020-05-26 02:57:58 +03:00
|
|
|
if msg.Alt { // alt+left arrow, back one word
|
2020-10-27 02:24:42 +03:00
|
|
|
resetBlink = m.wordLeft()
|
2020-05-26 02:57:58 +03:00
|
|
|
break
|
|
|
|
}
|
2020-10-27 03:59:09 +03:00
|
|
|
if m.pos > 0 { // left arrow, ^F, back one character
|
2021-04-26 17:51:59 +03:00
|
|
|
resetBlink = m.setCursor(m.pos - 1)
|
2020-01-18 18:57:38 +03:00
|
|
|
}
|
2020-10-27 03:59:09 +03:00
|
|
|
case tea.KeyRight, tea.KeyCtrlF:
|
2020-05-26 02:57:58 +03:00
|
|
|
if msg.Alt { // alt+right arrow, forward one word
|
2020-10-27 02:24:42 +03:00
|
|
|
resetBlink = m.wordRight()
|
2020-05-26 02:57:58 +03:00
|
|
|
break
|
|
|
|
}
|
2021-04-26 18:14:25 +03:00
|
|
|
if m.pos < len(m.value) { // right arrow, ^F, forward one character
|
2021-04-26 17:51:59 +03:00
|
|
|
resetBlink = m.setCursor(m.pos + 1)
|
2020-01-18 18:57:38 +03:00
|
|
|
}
|
2020-10-25 07:35:20 +03:00
|
|
|
case tea.KeyCtrlW: // ^W, delete word left of cursor
|
2020-10-27 02:24:42 +03:00
|
|
|
resetBlink = m.deleteWordLeft()
|
2020-10-22 22:32:16 +03:00
|
|
|
case tea.KeyHome, tea.KeyCtrlA: // ^A, go to beginning
|
2021-04-26 17:51:59 +03:00
|
|
|
resetBlink = m.cursorStart()
|
2020-10-22 22:32:16 +03:00
|
|
|
case tea.KeyDelete, 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-10-22 22:32:16 +03:00
|
|
|
case tea.KeyCtrlE, tea.KeyEnd: // ^E, go to end
|
2021-04-26 17:51:59 +03:00
|
|
|
resetBlink = m.cursorEnd()
|
2020-01-30 05:51:52 +03:00
|
|
|
case tea.KeyCtrlK: // ^K, kill text after cursor
|
2021-04-26 18:28:37 +03:00
|
|
|
resetBlink = m.deleteAfterCursor()
|
2020-01-30 06:25:39 +03:00
|
|
|
case tea.KeyCtrlU: // ^U, kill text before cursor
|
2021-04-26 18:28:37 +03:00
|
|
|
resetBlink = m.deleteBeforeCursor()
|
2020-07-21 23:49:27 +03:00
|
|
|
case tea.KeyCtrlV: // ^V paste
|
2020-10-25 03:20:45 +03:00
|
|
|
return m, Paste
|
2022-05-29 07:33:14 +03:00
|
|
|
case tea.KeyRunes, tea.KeySpace: // input regular characters
|
2020-10-31 06:38:36 +03:00
|
|
|
if msg.Alt && len(msg.Runes) == 1 {
|
|
|
|
if msg.Runes[0] == 'd' { // alt+d, delete word right of cursor
|
2020-10-27 02:24:42 +03:00
|
|
|
resetBlink = m.deleteWordRight()
|
2020-10-25 07:35:20 +03:00
|
|
|
break
|
|
|
|
}
|
2020-10-31 06:38:36 +03:00
|
|
|
if msg.Runes[0] == 'b' { // alt+b, back one word
|
2020-10-27 02:24:42 +03:00
|
|
|
resetBlink = m.wordLeft()
|
2020-05-26 02:57:58 +03:00
|
|
|
break
|
|
|
|
}
|
2020-10-31 06:38:36 +03:00
|
|
|
if msg.Runes[0] == 'f' { // alt+f, forward one word
|
2020-10-27 02:24:42 +03:00
|
|
|
resetBlink = m.wordRight()
|
2020-05-26 02:57:58 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Input a regular character
|
|
|
|
if m.CharLimit <= 0 || len(m.value) < m.CharLimit {
|
2022-02-09 01:11:45 +03:00
|
|
|
runes := msg.Runes
|
|
|
|
|
|
|
|
value := make([]rune, len(m.value))
|
|
|
|
copy(value, m.value)
|
|
|
|
value = append(value[:m.pos], append(runes, value[m.pos:]...)...)
|
|
|
|
m.SetValue(string(value))
|
|
|
|
if m.Err == nil {
|
|
|
|
resetBlink = m.setCursor(m.pos + len(runes))
|
|
|
|
}
|
2020-04-02 03:51:09 +03:00
|
|
|
}
|
2020-01-18 18:57:38 +03:00
|
|
|
}
|
|
|
|
|
2021-06-02 04:24:57 +03:00
|
|
|
case initialBlinkMsg:
|
|
|
|
// We accept all initialBlinkMsgs genrated by the Blink command.
|
|
|
|
|
|
|
|
if m.cursorMode != CursorBlink || !m.focus {
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := m.blinkCmd()
|
|
|
|
return m, cmd
|
|
|
|
|
2020-10-25 00:18:43 +03:00
|
|
|
case blinkMsg:
|
2021-06-02 04:24:57 +03:00
|
|
|
// We're choosy about whether to accept blinkMsgs so that our cursor
|
|
|
|
// only exactly when it should.
|
|
|
|
|
|
|
|
// Is this model blinkable?
|
|
|
|
if m.cursorMode != CursorBlink || !m.focus {
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Were we expecting this blink message?
|
|
|
|
if msg.id != m.id || msg.tag != m.blinkTag {
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2020-10-27 04:14:48 +03:00
|
|
|
var cmd tea.Cmd
|
2021-06-02 00:02:04 +03:00
|
|
|
if m.cursorMode == CursorBlink {
|
2020-10-27 04:14:48 +03:00
|
|
|
m.blink = !m.blink
|
|
|
|
cmd = m.blinkCmd()
|
|
|
|
}
|
2020-10-25 00:18:43 +03:00
|
|
|
return m, cmd
|
2020-10-25 03:20:45 +03:00
|
|
|
|
2020-10-27 02:24:42 +03:00
|
|
|
case blinkCanceled: // no-op
|
|
|
|
return m, nil
|
|
|
|
|
2020-10-25 03:20:45 +03:00
|
|
|
case pasteMsg:
|
2020-10-27 02:24:42 +03:00
|
|
|
resetBlink = m.handlePaste(string(msg))
|
2020-10-25 03:20:45 +03:00
|
|
|
|
|
|
|
case pasteErrMsg:
|
|
|
|
m.Err = msg
|
2020-01-18 18:57:38 +03:00
|
|
|
}
|
2020-05-26 02:57:58 +03:00
|
|
|
|
2020-10-27 02:24:42 +03:00
|
|
|
var cmd tea.Cmd
|
|
|
|
if resetBlink {
|
|
|
|
cmd = m.blinkCmd()
|
|
|
|
}
|
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
m.handleOverflow()
|
2020-10-27 02:24:42 +03:00
|
|
|
return m, cmd
|
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-10-25 22:50:52 +03:00
|
|
|
func (m Model) View() string {
|
2020-02-02 06:26:35 +03:00
|
|
|
// Placeholder text
|
2020-06-22 22:28:38 +03:00
|
|
|
if len(m.value) == 0 && m.Placeholder != "" {
|
2020-10-25 22:50:52 +03:00
|
|
|
return m.placeholderView()
|
2020-02-02 06:26:35 +03:00
|
|
|
}
|
|
|
|
|
2021-04-13 01:16:28 +03:00
|
|
|
styleText := m.TextStyle.Inline(true).Render
|
|
|
|
|
2020-06-12 03:43:32 +03:00
|
|
|
value := m.value[m.offset:m.offsetRight]
|
2020-07-22 01:22:18 +03:00
|
|
|
pos := max(0, m.pos-m.offset)
|
2021-04-13 01:16:28 +03:00
|
|
|
v := styleText(m.echoTransform(string(value[:pos])))
|
2020-02-02 06:26:35 +03:00
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
if pos < len(value) {
|
2021-04-13 01:16:28 +03:00
|
|
|
v += m.cursorView(m.echoTransform(string(value[pos]))) // cursor and text under it
|
|
|
|
v += styleText(m.echoTransform(string(value[pos+1:]))) // text after cursor
|
2020-01-18 18:57:38 +03:00
|
|
|
} else {
|
2020-10-25 22:50:52 +03:00
|
|
|
v += m.cursorView(" ")
|
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))
|
2021-04-13 01:16:28 +03:00
|
|
|
if m.Width > 0 && valWidth <= m.Width {
|
2020-06-12 03:43:32 +03:00
|
|
|
padding := max(0, m.Width-valWidth)
|
|
|
|
if valWidth+padding <= m.Width && pos < len(value) {
|
2020-05-26 02:57:58 +03:00
|
|
|
padding++
|
|
|
|
}
|
2021-04-13 01:16:28 +03:00
|
|
|
v += styleText(strings.Repeat(" ", padding))
|
2020-05-26 02:57:58 +03:00
|
|
|
}
|
|
|
|
|
2021-04-13 01:16:28 +03:00
|
|
|
return m.PromptStyle.Render(m.Prompt) + v
|
2020-01-18 18:57:38 +03:00
|
|
|
}
|
|
|
|
|
2020-10-25 22:50:52 +03:00
|
|
|
// placeholderView returns the prompt and placeholder view, if any.
|
|
|
|
func (m Model) placeholderView() string {
|
2020-02-02 06:26:35 +03:00
|
|
|
var (
|
2021-04-13 01:16:28 +03:00
|
|
|
v string
|
|
|
|
p = m.Placeholder
|
|
|
|
style = m.PlaceholderStyle.Inline(true).Render
|
2020-02-02 06:26:35 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Cursor
|
2021-04-13 01:16:28 +03:00
|
|
|
if m.blink {
|
|
|
|
v += m.cursorView(style(p[:1]))
|
2020-02-02 06:26:35 +03:00
|
|
|
} else {
|
2020-10-25 22:50:52 +03:00
|
|
|
v += m.cursorView(p[:1])
|
2020-02-02 06:26:35 +03:00
|
|
|
}
|
|
|
|
|
2020-04-17 21:38:28 +03:00
|
|
|
// The rest of the placeholder text
|
2021-04-13 01:16:28 +03:00
|
|
|
v += style(p[1:])
|
2020-02-02 06:26:35 +03:00
|
|
|
|
2021-04-13 01:16:28 +03:00
|
|
|
return m.PromptStyle.Render(m.Prompt) + v
|
2020-02-02 06:26:35 +03:00
|
|
|
}
|
|
|
|
|
2020-05-26 02:57:58 +03:00
|
|
|
// cursorView styles the cursor.
|
2020-10-25 22:50:52 +03:00
|
|
|
func (m Model) cursorView(v string) string {
|
2020-02-18 01:00:01 +03:00
|
|
|
if m.blink {
|
2021-04-13 01:16:28 +03:00
|
|
|
return m.TextStyle.Render(v)
|
2020-01-18 18:57:38 +03:00
|
|
|
}
|
2021-04-13 01:16:28 +03:00
|
|
|
return m.CursorStyle.Inline(true).Reverse(true).Render(v)
|
2020-01-18 18:57:38 +03:00
|
|
|
}
|
|
|
|
|
2020-10-27 05:56:53 +03:00
|
|
|
// blinkCmd is an internal command used to manage cursor blinking.
|
2021-06-02 04:24:57 +03:00
|
|
|
func (m *Model) blinkCmd() tea.Cmd {
|
|
|
|
if m.cursorMode != CursorBlink {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-27 02:24:42 +03:00
|
|
|
if m.blinkCtx != nil && m.blinkCtx.cancel != nil {
|
|
|
|
m.blinkCtx.cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(m.blinkCtx.ctx, m.BlinkSpeed)
|
|
|
|
m.blinkCtx.cancel = cancel
|
|
|
|
|
2021-06-02 04:24:57 +03:00
|
|
|
m.blinkTag++
|
|
|
|
|
2020-10-27 02:24:42 +03:00
|
|
|
return func() tea.Msg {
|
|
|
|
defer cancel()
|
|
|
|
<-ctx.Done()
|
|
|
|
if ctx.Err() == context.DeadlineExceeded {
|
2021-06-02 04:24:57 +03:00
|
|
|
return blinkMsg{id: m.id, tag: m.blinkTag}
|
2020-10-27 02:24:42 +03:00
|
|
|
}
|
|
|
|
return blinkCanceled{}
|
2020-10-25 22:50:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blink is a command used to initialize cursor blinking.
|
2020-10-25 00:18:43 +03:00
|
|
|
func Blink() tea.Msg {
|
2021-06-02 04:24:57 +03:00
|
|
|
return initialBlinkMsg{}
|
2020-10-25 00:18:43 +03:00
|
|
|
}
|
|
|
|
|
2020-10-25 22:50:52 +03:00
|
|
|
// Paste is a command for pasting from the clipboard into the text input.
|
2020-10-25 03:20:45 +03:00
|
|
|
func Paste() tea.Msg {
|
|
|
|
str, err := clipboard.ReadAll()
|
|
|
|
if err != nil {
|
|
|
|
return pasteErrMsg{err}
|
|
|
|
}
|
|
|
|
return pasteMsg(str)
|
|
|
|
}
|
|
|
|
|
2020-06-12 03:43:32 +03:00
|
|
|
func clamp(v, low, high int) int {
|
2022-01-20 04:54:44 +03:00
|
|
|
if high < low {
|
|
|
|
low, high = high, low
|
|
|
|
}
|
2020-06-12 03:43:32 +03:00
|
|
|
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
|
|
|
}
|