Skip to content

Commit

Permalink
fix(textinput): Placeholder No Longer Changes Width + Paste Calculati…
Browse files Browse the repository at this point in the history
…on (charmbracelet#451)

* Fix pasting calculations

Available length calculation now correctly trims pasted text.

* fix(textInput): Width padding added when placeholder is used

When `placeholder` is set, padding from `Width` was not added within the `placeholderView()` function.

* Adding missing 'm.'

* Maintain Width no matter placeholder size delta

* Fixed Math

* Added Comments

* fix: lint

---------

Co-authored-by: Donovan Hubbard <37090676+donovanhubbard@users.noreply.github.com>
Co-authored-by: Maas Lalani <maas@lalani.dev>
  • Loading branch information
3 people committed Feb 28, 2024
1 parent 659be86 commit 63b2529
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions textinput/textinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func (m *Model) insertRunesFromUserInput(v []rune) {
// If there's not enough space to paste the whole thing cut the pasted
// runes down so they'll fit.
if availSpace < len(paste) {
paste = paste[:len(paste)-availSpace]
paste = paste[:availSpace]
}
}

Expand Down Expand Up @@ -713,8 +713,29 @@ func (m Model) placeholderView() string {
m.Cursor.SetChar(string(p[:1]))
v += m.Cursor.View()

// The rest of the placeholder text
v += style(string(p[1:]))
// If the entire placeholder is already set and no padding is needed, finish
if m.Width < 1 && len(p) <= 1 {
return m.PromptStyle.Render(m.Prompt) + v
}

// If Width is set then size placeholder accordingly
if m.Width > 0 {
// available width is width - len + cursor offset of 1
minWidth := lipgloss.Width(m.Placeholder)
availWidth := m.Width - minWidth + 1

// if width < len, 'subtract'(add) number to len and dont add padding
if availWidth < 0 {
minWidth += availWidth
availWidth = 0
}
// append placeholder[len] - cursor, append padding
v += style(string(p[1:minWidth]))
v += style(strings.Repeat(" ", availWidth))
} else {
// if there is no width, the placeholder can be any length
v += style(string(p[1:]))
}

return m.PromptStyle.Render(m.Prompt) + v
}
Expand Down

0 comments on commit 63b2529

Please sign in to comment.