Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions textinput/textinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,8 +745,19 @@ func (m Model) placeholderView() string {
render = styles.Placeholder.Render
)

p := make([]rune, m.Width()+1)
copy(p, []rune(m.Placeholder))
// Size the buffer to the longer of Width and the placeholder
// itself so that, when Width is 0 ("no width") or shorter than
// the placeholder, copy still pulls in every rune. The old
// `m.Width()+1` left exactly one slot when Width was 0, so all
// but the first character of the placeholder fell on the floor.
// See #950.
placeholderRunes := []rune(m.Placeholder)
size := m.Width() + 1
if size < len(placeholderRunes) {
size = len(placeholderRunes)
}
p := make([]rune, size)
copy(p, placeholderRunes)

m.virtualCursor.TextStyle = styles.Placeholder
m.virtualCursor.SetChar(string(p[:1]))
Expand Down
22 changes: 22 additions & 0 deletions textinput/textinput_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@ func Test_SlicingOutsideCap(t *testing.T) {
textinput.View()
}

// Regression for #950. When SetWidth(0) is used (the documented
// "no width" mode), the placeholder buffer was sized to exactly 1
// rune and copy lost everything past the first character. The
// rendered placeholder was a single letter regardless of length.
func TestPlaceholderZeroWidth(t *testing.T) {
ti := New()
ti.Placeholder = "Something"
ti.SetWidth(0)

got := ti.View()
// View output carries ANSI styling, so check each rune of the
// placeholder appears somewhere in the string in order.
idx := 0
for _, r := range "Something" {
i := strings.IndexRune(got[idx:], r)
if i < 0 {
t.Fatalf("expected full placeholder %q to appear in view %q", "Something", got)
}
idx += i + 1
}
}

func TestChinesePlaceholder(t *testing.T) {
t.Skip("Skipping flaky test, the returned view seems incorrect. TODO: Needs investigation.")
textinput := New()
Expand Down