Skip to content

fix: don't start a wrapped row with the space between words - #42

Merged
orhun merged 3 commits into
ratatui:mainfrom
lukaslumiere:fix/wrap-hanging-space
Nov 28, 2026
Merged

orhun merged 3 commits into
ratatui:mainfrom
lukaslumiere:fix/wrap-hanging-space

Conversation

@lukaslumiere

@lukaslumiere lukaslumiere commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Hi! I use ratatui-textarea with word wrapping in a notes editor and noticed that wrapped rows sometimes start with a space. When the space after a word doesn't fit at the end of a row, it moves to the start of the next one, so that row looks indented by one cell, or holds only the space. Now that space stays at the end of the row it follows.

Supersedes #41, which moved the last word down instead and left some of these cases open.


More from the agent:

  • To reproduce: run cargo run --example wrap in an 80-column terminal, which starts with empty textareas, and type word word word word word word word abc next. In the "Word Wrap" box, abc ends exactly at the right edge, so the second row should be next. On main it is next.
  • Environment: tmux 3.4 on Linux (WSL2), Rust 1.98.0, ratatui 0.30.1 (ratatui-core 0.1.1) as locked here, default features (crossterm).
  • Cause: wrap_word_chunks closes a row when a chunk doesn't fit, and the next row starts with that chunk. When the chunk is whitespace, the row starts with it. The same happens after a word longer than the row (Word) and after the last piece of a word split by glyph (WordOrGlyph), which is the omega row recorded in word_and_word_or_glyph_differ_for_long_words.
  • Fix:
    • In wrap_word_chunks, the first whitespace char that would start a wrapped row is added to the end of the previous row instead, when that row doesn't already end with whitespace. Any further whitespace starts the next row, as on main, so spaces typed before a wrapped word still push it once the row above is full. Rows of leading indentation, such as one tab per row, wrap as before.
    • A hanging space can sit past the right edge, so line_spans_segment draws a cursor past the edge of a wrapped row on the last char that fits (drawn_cursor_offset, which reuses char_offset_for_screen_col). screen_map_load's wrap width calculation moved unchanged into wrap_width so both can use it. This applies to every wrap mode, Glyph included, so it also shows the cursor in two places where main doesn't draw it: at the end of a row that fills the whole width (hello at width 5), and on the part of a long word that Word leaves past the edge (helloworld at width 4).
  • Tests: word_and_word_or_glyph_differ_for_long_words now expects omega without the leading space. Two unit tests in src/wrap.rs check that leading indentation wraps as on main and that spaces after the first one start the next row, and one test in tests/textarea.rs checks that the cursor on a hanging space is drawn on the last column. All but the indentation test fail on main and pass here. On Linux, the commands in CONTRIBUTING.md pass, and so do the lint job in ci.yml and the unit-test commands, run with cargo test instead of cargo llvm-cov. I didn't run macOS, Windows or the nightly cargo-doc job.
  • Open questions:
    • A hanging space has no cell of its own, so while the cursor is on it, it is drawn over the last char of the row. The cursor position itself is right: typing there inserts after that char.
    • When a combining mark is followed by a tab or a wide whitespace char that crosses the edge, the last char that fits is the combining mark, so the cursor past the edge isn't drawn, e.g. "helloe\u{301}\t" at width 7 with Word and the cursor at the end. On main that tab starts the next row and the cursor is drawn there. Skipping zero-width chars would need a new helper next to char_offset_for_screen_col; I left it out to keep the change small.
    • screen_cursor() still returns the cursor's own column, which can be past the right edge (column 7 at width 7), while the cursor is drawn on the last column.
    • With WordOrGlyph, the last piece of a split word still ends its row, as on main: ious and omega take two rows although ious omega fits in 10 columns. (WIP) fix: keep the space between words off the start of a wrapped row #41 joined them; I left that out to keep this change to the space.

lukaslumiere and others added 3 commits November 11, 2026 13:55
With Word and WordOrGlyph wrapping, whitespace that did not fit at the
end of a row started the next one, so that row began with a space, or
held only spaces. This happened after a word that filled a row, after a
word longer than the row, and after the last piece of a word split by
glyph.

Now whitespace that would start a wrapped row hangs at the end of the
previous row instead, when that row holds a word. Rows of leading
indentation wrap as before.

A hanging space can sit past the right edge, so in every wrap mode a
cursor past the edge of a wrapped row is now drawn on the last column.
This also keeps the cursor visible at the end of a row that fills the
whole width, and on the part of a long word that Word wrapping leaves
past the edge, where it was not drawn before.
@orhun
orhun merged commit 26ee42a into ratatui:main Nov 28, 2026
5 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 28, 2026
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.

2 participants