From f72ab93067d5a974d912b44726dceac6125f9b75 Mon Sep 17 00:00:00 2001 From: Rose Thatcher <97619538+hopefulTex@users.noreply.github.com> Date: Tue, 29 Aug 2023 14:37:49 -0500 Subject: [PATCH 1/7] Fix pasting calculations Available length calculation now correctly trims pasted text. --- textinput/textinput.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/textinput/textinput.go b/textinput/textinput.go index 82341b97..c959ac98 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] } } From 02797404115c8ee03525db97d7e325e1ec11db6a Mon Sep 17 00:00:00 2001 From: Rose Thatcher <97619538+hopefulTex@users.noreply.github.com> Date: Wed, 13 Dec 2023 01:03:27 -0600 Subject: [PATCH 2/7] fix(textInput): Width padding added when placeholder is used When `placeholder` is set, padding from `Width` was not added within the `placeholderView()` function. --- textinput/textinput.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/textinput/textinput.go b/textinput/textinput.go index 964d28f1..f9b7d78f 100644 --- a/textinput/textinput.go +++ b/textinput/textinput.go @@ -716,6 +716,16 @@ func (m Model) placeholderView() string { // The rest of the placeholder text v += style(string(p[1:])) + // If Width is set then fill the placeholder with empty space accordingly + valWidth := rw.StringWidth(m.Placeholder) + if m.Width > 0 && valWidth <= m.Width { + padding := max(0, m.Width-valWidth) + if valWidth+padding <= m.Width && pos < len(v) { + padding++ + } + v += style(strings.Repeat(" ", padding)) + } + return m.PromptStyle.Render(m.Prompt) + v } From 297d30d9daa900ec43c46aad5f942da8eda95640 Mon Sep 17 00:00:00 2001 From: Donovan Hubbard <37090676+donovanhubbard@users.noreply.github.com> Date: Wed, 13 Dec 2023 19:28:04 -0600 Subject: [PATCH 3/7] Adding missing 'm.' --- textinput/textinput.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/textinput/textinput.go b/textinput/textinput.go index da90e002..27e92776 100644 --- a/textinput/textinput.go +++ b/textinput/textinput.go @@ -720,7 +720,7 @@ func (m Model) placeholderView() string { valWidth := rw.StringWidth(m.Placeholder) if m.Width > 0 && valWidth <= m.Width { padding := max(0, m.Width-valWidth) - if valWidth+padding <= m.Width && pos < len(v) { + if valWidth+padding <= m.Width && m.pos < len(v) { padding++ } v += style(strings.Repeat(" ", padding)) From ee43ac36e2acfd12e62625b3e60d1f49d1eb3ef6 Mon Sep 17 00:00:00 2001 From: Rose Thatcher <97619538+hopefulTex@users.noreply.github.com> Date: Sat, 23 Dec 2023 04:42:48 -0600 Subject: [PATCH 4/7] Maintain Width no matter placeholder size delta --- textinput/textinput.go | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/textinput/textinput.go b/textinput/textinput.go index 821f2aa9..0f6b8f7e 100644 --- a/textinput/textinput.go +++ b/textinput/textinput.go @@ -713,21 +713,25 @@ func (m Model) placeholderView() string { m.Cursor.SetChar(string(p[:1])) v += m.Cursor.View() - // The rest of the placeholder text - var spaces []rune - for i := 0; i < m.Width-lipgloss.Width(string(p[1:m.Width])); i++ { - spaces = append(spaces, ' ') + // 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 } - v += style(string(append(p[1:m.Width], spaces...))) - // If Width is set then fill the placeholder with empty space accordingly - valWidth := rw.StringWidth(m.Placeholder) - if m.Width > 0 && valWidth <= m.Width { - padding := max(0, m.Width-valWidth) - if valWidth+padding <= m.Width && m.pos < len(v) { - padding++ + // If Width is set then size placeholder accordingly + if m.Width > 0 { + minWidth := lipgloss.Width(string(p[1:])) + availWidth := m.Width - minWidth + + if availWidth < 0 { + minWidth += availWidth + availWidth = 0 } - v += style(strings.Repeat(" ", padding)) + v += style(string(p[1 : minWidth+1])) + v += style(strings.Repeat(" ", availWidth)) + + } else { + v += style(string(p[1:])) } return m.PromptStyle.Render(m.Prompt) + v From c4fe23b2e14dc852a972e234943b1d0017e9abe2 Mon Sep 17 00:00:00 2001 From: Rose Thatcher <97619538+hopefulTex@users.noreply.github.com> Date: Sat, 23 Dec 2023 04:58:27 -0600 Subject: [PATCH 5/7] Fixed Math --- textinput/textinput.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/textinput/textinput.go b/textinput/textinput.go index 0f6b8f7e..bd746bf6 100644 --- a/textinput/textinput.go +++ b/textinput/textinput.go @@ -720,14 +720,14 @@ func (m Model) placeholderView() string { // If Width is set then size placeholder accordingly if m.Width > 0 { - minWidth := lipgloss.Width(string(p[1:])) - availWidth := m.Width - minWidth + minWidth := lipgloss.Width(m.Placeholder) + availWidth := m.Width - minWidth + 1 if availWidth < 0 { minWidth += availWidth availWidth = 0 } - v += style(string(p[1 : minWidth+1])) + v += style(string(p[1:minWidth])) v += style(strings.Repeat(" ", availWidth)) } else { From 168c97438817fd88ccebce068e6f89b54ccde1bf Mon Sep 17 00:00:00 2001 From: Rose Thatcher <97619538+hopefulTex@users.noreply.github.com> Date: Sat, 23 Dec 2023 05:02:19 -0600 Subject: [PATCH 6/7] Added Comments --- textinput/textinput.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/textinput/textinput.go b/textinput/textinput.go index bd746bf6..944a4f9c 100644 --- a/textinput/textinput.go +++ b/textinput/textinput.go @@ -720,17 +720,21 @@ func (m Model) placeholderView() string { // 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:])) } From f7ce4df759df6480ad5061f28fa56ed18376a64b Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Sat, 23 Dec 2023 16:01:12 -0500 Subject: [PATCH 7/7] fix: lint --- textinput/textinput.go | 1 - 1 file changed, 1 deletion(-) diff --git a/textinput/textinput.go b/textinput/textinput.go index 944a4f9c..03ec9fc1 100644 --- a/textinput/textinput.go +++ b/textinput/textinput.go @@ -732,7 +732,6 @@ func (m Model) placeholderView() string { // 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:]))