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
14 changes: 9 additions & 5 deletions textinput/textinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,15 +745,20 @@ func (m Model) placeholderView() string {
render = styles.Placeholder.Render
)

p := make([]rune, m.Width()+1)
copy(p, []rune(m.Placeholder))
placeholder := []rune(m.Placeholder)
bufLen := len(placeholder)
if w := m.Width() + 1; w > bufLen {
bufLen = w
}
p := make([]rune, bufLen)
copy(p, placeholder)

m.virtualCursor.TextStyle = styles.Placeholder
m.virtualCursor.SetChar(string(p[:1]))
v += m.virtualCursor.View()

// If the entire placeholder is already set and no padding is needed, finish
if m.Width() < 1 && len(p) <= 1 {
if m.Width() < 1 && len(placeholder) <= 1 {
return styles.Prompt.Render(m.Prompt) + v
}

Expand Down Expand Up @@ -921,8 +926,7 @@ func (m Model) Cursor() *tea.Cursor {
w := lipgloss.Width

promptWidth := w(m.promptView())
xOffset := m.Position() +
promptWidth
xOffset := promptWidth + uniseg.StringWidth(string(m.value[:m.pos]))
if m.width > 0 {
xOffset = min(xOffset, m.width+promptWidth)
}
Expand Down
32 changes: 32 additions & 0 deletions textinput/textinput_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package textinput

import (
"fmt"
"regexp"
"strconv"
"strings"
"testing"

tea "charm.land/bubbletea/v2"
)

var stripANSI = regexp.MustCompile(`\x1b\[[0-9;]*[a-zA-Z]`)

func Test_CurrentSuggestion(t *testing.T) {
textinput := New()
textinput.ShowSuggestions = true
Expand Down Expand Up @@ -61,6 +64,16 @@ func TestChinesePlaceholder(t *testing.T) {
}
}

func TestPlaceholderZeroWidth(t *testing.T) {
ti := New()
ti.Placeholder = "Nickname"
view := ti.View()
plain := stripANSI.ReplaceAllString(view, "")
if !strings.Contains(plain, "Nickname") {
t.Fatalf("expected full placeholder in view, got %q (plain %q)", view, plain)
}
}

func TestPlaceholderTruncate(t *testing.T) {
t.Skip("Skipping flaky test, the returned view seems incorrect. TODO: Needs investigation.")
textinput := New()
Expand Down Expand Up @@ -118,3 +131,22 @@ func sendString(m Model, str string) Model {

return m
}

func TestCursorWideCharacterOffset(t *testing.T) {
t.Parallel()

m := New()
m.Prompt = ""
m.Focus()
m.SetVirtualCursor(false)
m.SetValue("你好")
m.SetCursor(len([]rune("你好")))

c := m.Cursor()
if c == nil {
t.Fatal("expected cursor, got nil")
}
if c.Position.X != 4 {
t.Fatalf("expected cursor X offset 4 for two wide runes, got %d", c.Position.X)
}
}