From f36aa3c4b5369f2ecefb4e35dbb2c924906932ca Mon Sep 17 00:00:00 2001 From: Rose Thatcher <97619538+hopefulTex@users.noreply.github.com> Date: Sat, 23 Dec 2023 15:02:26 -0600 Subject: [PATCH] fix(textinput): Placeholder No Longer Changes Width + Paste Calculation (#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 --- textinput/textinput.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/textinput/textinput.go b/textinput/textinput.go index 964d28f..03ec9fc 100644 --- a/textinput/textinput.go +++ b/textinput/textinput.go @@ -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] } } @@ -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 }