Skip to content

fix(textarea): keep a last word that exactly fills the line - #1070

Open
abdull-ah-med wants to merge 1 commit into
charmbracelet:mainfrom
abdull-ah-med:fix/textarea-wrap-exact-fit
Open

abdull-ah-med wants to merge 1 commit into
charmbracelet:mainfrom
abdull-ah-med:fix/textarea-wrap-exact-fit

Conversation

@abdull-ah-med

Copy link
Copy Markdown
  • I have read CONTRIBUTING.md.
  • I have created a discussion that was approved by a maintainer (for new features). (n/a, bug fix)

Fixes #333

What was wrong

With a width-10 textarea, typing 1234567890 keeps all ten characters on the first row, but typing 1 2 3 4 56 (also ten cells) moves 56 to the next row as soon as the 6 is typed. So a line of spaced words could only hold width-1 cells.

The cause is the final check in wrap(): >= width moved the last word to a new row whenever it made the line exactly full. A single unbroken word that fills the width takes a different path. It stays on its row, and only the trailing cursor cell goes to the next row.

What changed

Before / after (width 10, no prompt or line numbers)

input before after
1234567890 1234567890 / `` unchanged
1 2 3 4 56 1 2 3 4 / 56 1 2 3 4 56 / ``
1 2 3 4 567 1 2 3 4 / 567 unchanged

How it was tested

  • New TestWrapLastWordFillsWidth (table test of wrap, including a double-width rune) and TestTypingLastWordFillsWidthDoesNotReflow (typing through Update, then one more character to confirm the reflow). Both fail on main and pass with this change.
  • LineInfo() row/column for the cursor at the end and at column 9 are now identical for 1234567890 and 1 2 3 4 56.
  • go test ./..., go vet ./..., gofmt -l ., and golangci-lint run on the changed code all pass.

I prepared this change with AI assistance, then reviewed and tested it myself.

When the final word of a line made it exactly as wide as the textarea,
wrap moved that word to the next row, so a line of spaced words could
only hold width-1 characters while a single unbroken word could hold
the full width.

Keep the word on its row and move only the trailing cursor cell to the
next row, matching the unbroken-word case. The word still reflows once
it grows past the width.

Fixes charmbracelet#333
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

textarea: Text reflow off by one

1 participant