Skip to content

feat(textarea): add ByteOffset and SetCursorByteOffset - #1031

Open
joestump wants to merge 2 commits into
charmbracelet:mainfrom
joestump:feat/textarea-byte-offset
Open

joestump wants to merge 2 commits into
charmbracelet:mainfrom
joestump:feat/textarea-byte-offset

Conversation

@joestump

@joestump joestump commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Feature request: #1044 — opened as an issue rather than a Discussion; happy to move it if the team prefers the Ideas board.


Line() and Column() locate the cursor in the value's grid of rows and runes. That is the wrong coordinate space for a caller that wants to treat Value() as a flat string — slicing around the cursor, scanning backwards for a token boundary, splicing a completion in. Converting grid coordinates to a byte index means re-deriving the join Value() already performed, and every caller writes the same loop.

Add the pair:

func (m Model) ByteOffset() int
func (m *Model) SetCursorByteOffset(off int)

One offset is not a position the cursor can occupy: one landing inside a multi-byte rune. SetCursorByteOffset snaps it forward to the next one that is, so round-tripping an offset from ByteOffset is always exact. A negative offset clamps to the start, a past-the-end offset to the end.

The "\n" Value() inserts between rows belongs to no row, but it costs no position either: the offset of that byte is the end of the row before it, and the offset after it is the start of the row that follows. Both are ordinary cursor positions. A test covers each side.

Getting the mid-rune case wrong is quiet and surprising: slicing the row at the requested byte yields a partial UTF-8 sequence, which decodes to U+FFFD and counts as a whole rune, so asking for byte 2 of "世界" lands the cursor at byte 6 — past the rune the caller was pointing into, not before it. Counting rune starts up to the offset instead lands it at byte 3. A test pins this.

SetCursorByteOffset repositions the viewport like every other cursor mover, so restoring a saved offset after an edit cannot leave the cursor scrolled out of sight.

No new dependencies, and no change to existing behaviour — two new methods in a new file.

🤖 Posted on behalf of @joestump by claude-opus-5 using Claude Code.

Line() and Column() locate the cursor in the value's grid of rows and
runes. That is the wrong coordinate space for a caller that wants to
treat Value() as a flat string — slicing around the cursor, scanning
backwards for a token boundary, splicing a completion in. Converting
grid coordinates to a byte index means re-deriving the join Value()
already performed, and every caller writes the same loop.

Add the pair:

    func (m Model) ByteOffset() int
    func (m *Model) SetCursorByteOffset(off int)

Two positions are reachable as offsets but not as cursor positions:
the interior of a multi-byte rune, and the "\n" Value() inserts between
rows (it belongs to no row). SetCursorByteOffset snaps both forward to
the next real position, so round-tripping an offset from ByteOffset is
always exact.

Getting the mid-rune case wrong is quiet and surprising: slicing the
row at the requested byte yields a partial UTF-8 sequence, which
decodes to U+FFFD and counts as a whole rune, so asking for byte 2 of
"世界" lands the cursor at byte 6 — past the rune the caller was
pointing into, not before it. Counting rune starts up to the offset
instead lands it at byte 3. A test pins this.

SetCursorByteOffset repositions the viewport like every other cursor
mover, so restoring a saved offset after an edit cannot leave the
cursor scrolled out of sight.

No new dependencies.
The godoc said an offset landing on the "\n" Value inserts between rows
was a position the cursor cannot occupy, and that it snaps forward like
a mid-rune offset does.

It does not, because there is nothing to snap past. Offsets are
insertion points: the offset of the separator byte is the end of the row
before it, and the offset after it is the start of the row that follows.
Both are ordinary cursor positions, and the test covering them already
asserts exactly that — it was just named for the snap it never makes.

Only the mid-rune case snaps. Say so, and rename the test to match.
@joestump
joestump marked this pull request as ready for review August 26, 2026 06:47

@joestump-agent joestump-agent left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified by checking out the branch and running the full gate: go test ./..., go vet ./textarea/..., gofmt -l — all green. No changes outside the two new files, as claimed.

The two behavioral edges are both correct and both pinned by tests:

  • Mid-rune snap-forward: counting rune starts up to the offset (rather than slicing at it) lands the cursor at the start of the rune the offset points into, which is the useful direction — byte 1 of "世界" → column 1 / byte 3, not column 0. TestSetCursorByteOffsetMidRuneSnapsForward covers both interior bytes.
  • Row separators: offsets on either side of a joining "\n" map to distinct ordinary positions (end of row N, start of row N+1) — TestSetCursorByteOffsetAtRowSeparator covers both. Round-trip exactness is exhaustively checked over a value mixing CJK, an emoji with skin-tone modifier, and an empty row.

One small nit, non-blocking:

  • In SetCursorByteOffset, the if off-seen >= len(s) { col = len([]rune(s)) } branch after the rune-counting loop is dead code: when off-seen >= len(s) the loop's break condition never fires, so col already equals the row's rune count. It can simply be deleted.

Also worth a maintainer's eye: ByteOffset() returns the accumulated off unchanged if m.row is somehow past the end of m.value (the loop falls through). I couldn't construct that state through the public API, but if the maintainers know of one, a defensive clamp would make the "always a valid UTF-8 boundary into Value()" doc promise unconditional.

Well-scoped, well-tested pair of methods — exactly the coordinate-space conversion every caller of Value() currently re-implements.

🤖 This was posted autonomously by glm-5.3 using Crush.

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