|
|
|
@@ -46,6 +46,14 @@ type KeyMap struct {
|
|
|
|
|
Paste key.Binding
|
|
|
|
|
WordBackward key.Binding
|
|
|
|
|
WordForward key.Binding
|
|
|
|
|
InputBegin key.Binding
|
|
|
|
|
InputEnd key.Binding
|
|
|
|
|
|
|
|
|
|
UppercaseWordForward key.Binding
|
|
|
|
|
LowercaseWordForward key.Binding
|
|
|
|
|
CapitalizeWordForward key.Binding
|
|
|
|
|
|
|
|
|
|
TransposeCharacterBackward key.Binding
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DefaultKeyMap is the default set of key bindings for navigating and acting
|
|
|
|
@@ -67,6 +75,14 @@ var DefaultKeyMap = KeyMap{
|
|
|
|
|
LineStart: key.NewBinding(key.WithKeys("home", "ctrl+a")),
|
|
|
|
|
LineEnd: key.NewBinding(key.WithKeys("end", "ctrl+e")),
|
|
|
|
|
Paste: key.NewBinding(key.WithKeys("ctrl+v")),
|
|
|
|
|
InputBegin: key.NewBinding(key.WithKeys("alt+<", "ctrl+home")),
|
|
|
|
|
InputEnd: key.NewBinding(key.WithKeys("alt+>", "ctrl+end")),
|
|
|
|
|
|
|
|
|
|
CapitalizeWordForward: key.NewBinding(key.WithKeys("alt+c")),
|
|
|
|
|
LowercaseWordForward: key.NewBinding(key.WithKeys("alt+l")),
|
|
|
|
|
UppercaseWordForward: key.NewBinding(key.WithKeys("alt+u")),
|
|
|
|
|
|
|
|
|
|
TransposeCharacterBackward: key.NewBinding(key.WithKeys("ctrl+t")),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LineInfo is a helper for keeping track of line information regarding
|
|
|
|
@@ -117,10 +133,27 @@ type Model struct {
|
|
|
|
|
Err error
|
|
|
|
|
|
|
|
|
|
// General settings.
|
|
|
|
|
|
|
|
|
|
// Prompt is printed at the beginning of each line.
|
|
|
|
|
//
|
|
|
|
|
// When changing the value of Prompt after the model has been
|
|
|
|
|
// initialized, ensure that SetWidth() gets called afterwards.
|
|
|
|
|
//
|
|
|
|
|
// See also SetPromptFunc().
|
|
|
|
|
Prompt string
|
|
|
|
|
|
|
|
|
|
// Placeholder is the text displayed when the user
|
|
|
|
|
// hasn't entered anything yet.
|
|
|
|
|
Placeholder string
|
|
|
|
|
|
|
|
|
|
// ShowLineNumbers, if enabled, causes line numbers to be printed
|
|
|
|
|
// after the prompt.
|
|
|
|
|
ShowLineNumbers bool
|
|
|
|
|
|
|
|
|
|
// EndOfBufferCharacter is displayed at the end of the input.
|
|
|
|
|
EndOfBufferCharacter rune
|
|
|
|
|
|
|
|
|
|
// KeyMap encodes the keybindings recognized by the widget.
|
|
|
|
|
KeyMap KeyMap
|
|
|
|
|
|
|
|
|
|
// Styling. FocusedStyle and BlurredStyle are used to style the textarea in
|
|
|
|
@@ -140,6 +173,13 @@ type Model struct {
|
|
|
|
|
// accept. If 0 or less, there's no limit.
|
|
|
|
|
CharLimit int
|
|
|
|
|
|
|
|
|
|
// If promptFunc is set, it replaces Prompt as a generator for
|
|
|
|
|
// prompt strings at the beginning of each line.
|
|
|
|
|
promptFunc func(line int) string
|
|
|
|
|
|
|
|
|
|
// promptWidth is the width of the prompt.
|
|
|
|
|
promptWidth int
|
|
|
|
|
|
|
|
|
|
// width is the maximum number of characters that can be displayed at once.
|
|
|
|
|
// If 0 or less this setting is ignored.
|
|
|
|
|
width int
|
|
|
|
@@ -473,6 +513,24 @@ func (m *Model) deleteAfterCursor() {
|
|
|
|
|
m.SetCursor(len(m.value[m.row]))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// transposeLeft exchanges the runes at the cursor and immediately
|
|
|
|
|
// before. No-op if the cursor is at the beginning of the line. If
|
|
|
|
|
// the cursor is not at the end of the line yet, moves the cursor to
|
|
|
|
|
// the right.
|
|
|
|
|
func (m *Model) transposeLeft() {
|
|
|
|
|
if m.col == 0 || len(m.value[m.row]) < 2 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if m.col >= len(m.value[m.row]) {
|
|
|
|
|
m.SetCursor(m.col - 1)
|
|
|
|
|
}
|
|
|
|
|
m.value[m.row][m.col-1], m.value[m.row][m.col] =
|
|
|
|
|
m.value[m.row][m.col], m.value[m.row][m.col-1]
|
|
|
|
|
if m.col < len(m.value[m.row]) {
|
|
|
|
|
m.SetCursor(m.col + 1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// deleteWordLeft deletes the word left to the cursor. Returns whether or not
|
|
|
|
|
// the cursor blink should be reset.
|
|
|
|
|
func (m *Model) deleteWordLeft() {
|
|
|
|
@@ -547,31 +605,50 @@ func (m *Model) deleteWordRight() {
|
|
|
|
|
m.SetCursor(oldCol)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// characterRight moves the cursor one character to the right.
|
|
|
|
|
func (m *Model) characterRight() {
|
|
|
|
|
if m.col < len(m.value[m.row]) {
|
|
|
|
|
m.SetCursor(m.col + 1)
|
|
|
|
|
} else {
|
|
|
|
|
if m.row < len(m.value)-1 {
|
|
|
|
|
m.row++
|
|
|
|
|
m.CursorStart()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// characterLeft moves the cursor one character to the left.
|
|
|
|
|
// If insideLine is set, the cursor is moved to the last
|
|
|
|
|
// character in the previous line, instead of one past that.
|
|
|
|
|
func (m *Model) characterLeft(insideLine bool) {
|
|
|
|
|
if m.col == 0 && m.row != 0 {
|
|
|
|
|
m.row--
|
|
|
|
|
m.CursorEnd()
|
|
|
|
|
if !insideLine {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if m.col > 0 {
|
|
|
|
|
m.SetCursor(m.col - 1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// wordLeft moves the cursor one word to the left. Returns whether or not the
|
|
|
|
|
// 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.
|
|
|
|
|
func (m *Model) wordLeft() {
|
|
|
|
|
if m.col == 0 || len(m.value[m.row]) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i := m.col - 1
|
|
|
|
|
for i >= 0 {
|
|
|
|
|
if unicode.IsSpace(m.value[m.row][min(i, len(m.value[m.row])-1)]) {
|
|
|
|
|
m.SetCursor(m.col - 1)
|
|
|
|
|
i--
|
|
|
|
|
} else {
|
|
|
|
|
for {
|
|
|
|
|
m.characterLeft(true /* insideLine */)
|
|
|
|
|
if m.col < len(m.value[m.row]) && !unicode.IsSpace(m.value[m.row][m.col]) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i >= 0 {
|
|
|
|
|
if !unicode.IsSpace(m.value[m.row][min(i, len(m.value[m.row])-1)]) {
|
|
|
|
|
m.SetCursor(m.col - 1)
|
|
|
|
|
i--
|
|
|
|
|
} else {
|
|
|
|
|
for m.col > 0 {
|
|
|
|
|
if unicode.IsSpace(m.value[m.row][m.col-1]) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
m.SetCursor(m.col - 1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -579,28 +656,54 @@ func (m *Model) wordLeft() {
|
|
|
|
|
// 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.
|
|
|
|
|
func (m *Model) wordRight() {
|
|
|
|
|
if m.col >= len(m.value[m.row]) || len(m.value[m.row]) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
m.doWordRight(func(int, int) { /* nothing */ })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
i := m.col
|
|
|
|
|
for i < len(m.value[m.row]) {
|
|
|
|
|
if unicode.IsSpace(m.value[m.row][i]) {
|
|
|
|
|
m.SetCursor(m.col + 1)
|
|
|
|
|
i++
|
|
|
|
|
} else {
|
|
|
|
|
func (m *Model) doWordRight(fn func(charIdx int, pos int)) {
|
|
|
|
|
// Skip spaces forward.
|
|
|
|
|
for {
|
|
|
|
|
if m.col < len(m.value[m.row]) && !unicode.IsSpace(m.value[m.row][m.col]) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i < len(m.value[m.row]) {
|
|
|
|
|
if !unicode.IsSpace(m.value[m.row][i]) {
|
|
|
|
|
m.SetCursor(m.col + 1)
|
|
|
|
|
i++
|
|
|
|
|
} else {
|
|
|
|
|
if m.row == len(m.value)-1 && m.col == len(m.value[m.row]) {
|
|
|
|
|
// End of text.
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
m.characterRight()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
charIdx := 0
|
|
|
|
|
for m.col < len(m.value[m.row]) {
|
|
|
|
|
if unicode.IsSpace(m.value[m.row][m.col]) {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
fn(charIdx, m.col)
|
|
|
|
|
m.SetCursor(m.col + 1)
|
|
|
|
|
charIdx++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// uppercaseRight changes the word to the right to uppercase.
|
|
|
|
|
func (m *Model) uppercaseRight() {
|
|
|
|
|
m.doWordRight(func(_ int, i int) {
|
|
|
|
|
m.value[m.row][i] = unicode.ToUpper(m.value[m.row][i])
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// lowercaseRight changes the word to the right to lowercase.
|
|
|
|
|
func (m *Model) lowercaseRight() {
|
|
|
|
|
m.doWordRight(func(_ int, i int) {
|
|
|
|
|
m.value[m.row][i] = unicode.ToLower(m.value[m.row][i])
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// capitalizeRight changes the word to the right to title case.
|
|
|
|
|
func (m *Model) capitalizeRight() {
|
|
|
|
|
m.doWordRight(func(charIdx int, i int) {
|
|
|
|
|
if charIdx == 0 {
|
|
|
|
|
m.value[m.row][i] = unicode.ToTitle(m.value[m.row][i])
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// LineInfo returns the number of characters from the start of the
|
|
|
|
@@ -662,6 +765,18 @@ func (m Model) Width() int {
|
|
|
|
|
return m.width
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// moveToBegin moves the cursor to the beginning of the input.
|
|
|
|
|
func (m *Model) moveToBegin() {
|
|
|
|
|
m.row = 0
|
|
|
|
|
m.SetCursor(0)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// moveToEnd moves the cursor to the end of the input.
|
|
|
|
|
func (m *Model) moveToEnd() {
|
|
|
|
|
m.row = len(m.value) - 1
|
|
|
|
|
m.SetCursor(len(m.value[m.row]))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetWidth sets the width of the textarea to fit exactly within the given width.
|
|
|
|
|
// This means that the textarea will account for the width of the prompt and
|
|
|
|
|
// whether or not line numbers are being shown.
|
|
|
|
@@ -682,10 +797,26 @@ func (m *Model) SetWidth(w int) {
|
|
|
|
|
// Account for base style borders and padding.
|
|
|
|
|
inputWidth -= m.style.Base.GetHorizontalFrameSize()
|
|
|
|
|
|
|
|
|
|
inputWidth -= rw.StringWidth(m.Prompt)
|
|
|
|
|
if m.promptFunc == nil {
|
|
|
|
|
m.promptWidth = rw.StringWidth(m.Prompt)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inputWidth -= m.promptWidth
|
|
|
|
|
m.width = clamp(inputWidth, minWidth, maxWidth)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetPromptFunc supersedes the Prompt field and sets a dynamic prompt
|
|
|
|
|
// instead.
|
|
|
|
|
// If the function returns a prompt that is shorter than the
|
|
|
|
|
// specified promptWidth, it will be padded to the left.
|
|
|
|
|
// If it returns a prompt that is longer, display artifacts
|
|
|
|
|
// may occur; the caller is responsible for computing an adequate
|
|
|
|
|
// promptWidth.
|
|
|
|
|
func (m *Model) SetPromptFunc(promptWidth int, fn func(lineIdx int) string) {
|
|
|
|
|
m.promptFunc = fn
|
|
|
|
|
m.promptWidth = promptWidth
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Height returns the current height of the textarea.
|
|
|
|
|
func (m Model) Height() int {
|
|
|
|
|
return m.height
|
|
|
|
@@ -774,14 +905,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
|
|
|
|
case key.Matches(msg, m.KeyMap.LineStart):
|
|
|
|
|
m.CursorStart()
|
|
|
|
|
case key.Matches(msg, m.KeyMap.CharacterForward):
|
|
|
|
|
if m.col < len(m.value[m.row]) {
|
|
|
|
|
m.SetCursor(m.col + 1)
|
|
|
|
|
} else {
|
|
|
|
|
if m.row < len(m.value)-1 {
|
|
|
|
|
m.row++
|
|
|
|
|
m.CursorStart()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m.characterRight()
|
|
|
|
|
case key.Matches(msg, m.KeyMap.LineNext):
|
|
|
|
|
m.CursorDown()
|
|
|
|
|
case key.Matches(msg, m.KeyMap.WordForward):
|
|
|
|
@@ -789,18 +913,24 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
|
|
|
|
|
case key.Matches(msg, m.KeyMap.Paste):
|
|
|
|
|
return m, Paste
|
|
|
|
|
case key.Matches(msg, m.KeyMap.CharacterBackward):
|
|
|
|
|
if m.col == 0 && m.row != 0 {
|
|
|
|
|
m.row--
|
|
|
|
|
m.CursorEnd()
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if m.col > 0 {
|
|
|
|
|
m.SetCursor(m.col - 1)
|
|
|
|
|
}
|
|
|
|
|
m.characterLeft(false /* insideLine */)
|
|
|
|
|
case key.Matches(msg, m.KeyMap.LinePrevious):
|
|
|
|
|
m.CursorUp()
|
|
|
|
|
case key.Matches(msg, m.KeyMap.WordBackward):
|
|
|
|
|
m.wordLeft()
|
|
|
|
|
case key.Matches(msg, m.KeyMap.InputBegin):
|
|
|
|
|
m.moveToBegin()
|
|
|
|
|
case key.Matches(msg, m.KeyMap.InputEnd):
|
|
|
|
|
m.moveToEnd()
|
|
|
|
|
case key.Matches(msg, m.KeyMap.LowercaseWordForward):
|
|
|
|
|
m.lowercaseRight()
|
|
|
|
|
case key.Matches(msg, m.KeyMap.UppercaseWordForward):
|
|
|
|
|
m.uppercaseRight()
|
|
|
|
|
case key.Matches(msg, m.KeyMap.CapitalizeWordForward):
|
|
|
|
|
m.capitalizeRight()
|
|
|
|
|
case key.Matches(msg, m.KeyMap.TransposeCharacterBackward):
|
|
|
|
|
m.transposeLeft()
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
if m.CharLimit > 0 && rw.StringWidth(m.Value()) >= m.CharLimit {
|
|
|
|
|
break
|
|
|
|
@@ -848,6 +978,7 @@ func (m Model) View() string {
|
|
|
|
|
|
|
|
|
|
var newLines int
|
|
|
|
|
|
|
|
|
|
displayLine := 0
|
|
|
|
|
for l, line := range m.value {
|
|
|
|
|
wrappedLines := wrap(line, m.width)
|
|
|
|
|
|
|
|
|
@@ -858,7 +989,10 @@ func (m Model) View() string {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for wl, wrappedLine := range wrappedLines {
|
|
|
|
|
s.WriteString(style.Render(m.style.Prompt.Render(m.Prompt)))
|
|
|
|
|
prompt := m.getPromptString(displayLine)
|
|
|
|
|
prompt = m.style.Prompt.Render(prompt)
|
|
|
|
|
s.WriteString(style.Render(prompt))
|
|
|
|
|
displayLine++
|
|
|
|
|
|
|
|
|
|
if m.ShowLineNumbers {
|
|
|
|
|
if wl == 0 {
|
|
|
|
@@ -907,7 +1041,10 @@ func (m Model) View() string {
|
|
|
|
|
// Always show at least `m.Height` lines at all times.
|
|
|
|
|
// To do this we can simply pad out a few extra new lines in the view.
|
|
|
|
|
for i := 0; i < m.height; i++ {
|
|
|
|
|
s.WriteString(m.style.Prompt.Render(m.Prompt))
|
|
|
|
|
prompt := m.getPromptString(displayLine)
|
|
|
|
|
prompt = m.style.Prompt.Render(prompt)
|
|
|
|
|
s.WriteString(prompt)
|
|
|
|
|
displayLine++
|
|
|
|
|
|
|
|
|
|
if m.ShowLineNumbers {
|
|
|
|
|
lineNumber := m.style.EndOfBuffer.Render((fmt.Sprintf(m.lineNumberFormat, string(m.EndOfBufferCharacter))))
|
|
|
|
@@ -920,6 +1057,19 @@ func (m Model) View() string {
|
|
|
|
|
return m.style.Base.Render(m.viewport.View())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m Model) getPromptString(displayLine int) (prompt string) {
|
|
|
|
|
prompt = m.Prompt
|
|
|
|
|
if m.promptFunc == nil {
|
|
|
|
|
return prompt
|
|
|
|
|
}
|
|
|
|
|
prompt = m.promptFunc(displayLine)
|
|
|
|
|
pl := rw.StringWidth(prompt)
|
|
|
|
|
if pl < m.promptWidth {
|
|
|
|
|
prompt = fmt.Sprintf("%*s%s", m.promptWidth-pl, "", prompt)
|
|
|
|
|
}
|
|
|
|
|
return prompt
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// placeholderView returns the prompt and placeholder view, if any.
|
|
|
|
|
func (m Model) placeholderView() string {
|
|
|
|
|
var (
|
|
|
|
@@ -928,7 +1078,8 @@ func (m Model) placeholderView() string {
|
|
|
|
|
style = m.style.Placeholder.Inline(true)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
prompt := m.style.Prompt.Render(m.Prompt)
|
|
|
|
|
prompt := m.getPromptString(0)
|
|
|
|
|
prompt = m.style.Prompt.Render(prompt)
|
|
|
|
|
s.WriteString(m.style.CursorLine.Render(prompt))
|
|
|
|
|
|
|
|
|
|
if m.ShowLineNumbers {
|
|
|
|
@@ -945,6 +1096,8 @@ func (m Model) placeholderView() string {
|
|
|
|
|
// The rest of the new lines
|
|
|
|
|
for i := 1; i < m.height; i++ {
|
|
|
|
|
s.WriteRune('\n')
|
|
|
|
|
prompt := m.getPromptString(i)
|
|
|
|
|
prompt = m.style.Prompt.Render(prompt)
|
|
|
|
|
s.WriteString(prompt)
|
|
|
|
|
|
|
|
|
|
if m.ShowLineNumbers {
|
|
|
|
|