Skip to content

fix(buffer): keep the wide-cell bookkeeping when a cell is inserted - #236

Open
shounakjoshi88-a11y wants to merge 1 commit into
charmbracelet:mainfrom
shounakjoshi88-a11y:fix/insert-cell-wide-cell
Open

shounakjoshi88-a11y wants to merge 1 commit into
charmbracelet:mainfrom
shounakjoshi88-a11y:fix/insert-cell-wide-cell

Conversation

@shounakjoshi88-a11y

Copy link
Copy Markdown

Description

Buffer.InsertCell left the buffer in a state where a placeholder belongs to no wide cell, and the rest of the row is shifted one column to the left.

The insert moves the cells to the right with a plain copy:

	// Move existing cells within rectangle bounds to the right
	for i := area.Max.X - 1; i >= x+n && i-n >= area.Min.X; i-- {
		// We don't need to clone c here because we're just moving cells to the
		// right.
		b.Lines[y][i] = b.Lines[y][i-n]
	}

A wide cell is a head plus zero-width placeholders that mark the columns it covers. The copy moves the head and its placeholders apart as soon as the insert lands between them, and the cells that the insert replaces keep their old content until they are written - a wide cell among them then clears the cell that has just moved next to it.

Two cases, both from the same insert:

  • the insert lands inside a wide cell: the wide cell keeps a tail that is no longer next to it, and the placeholder that moved right belongs to nothing;
  • the insert lands right before a wide cell: the stale head at the insert position clears the wide cell that has just moved next to it, so the wide character is lost.

Root cause

In InsertCellArea (buffer.go), the shift moves cells without the wide-cell bookkeeping, and the write that follows (SetCell) reads the cells the shift left behind.

SetCell treats the stale cell at the insert position as a wide cell that covers the cell next to it, and clears that one:

	if prev = l.At(x); prev != nil { //nolint:nestif
		if pw := prev.Width; pw > 1 {
			// Writing to the first wide cell
			for j := 0; j < pw && x+j < lineWidth; j++ {
				l[x+j] = *prev
				l[x+j].Empty()
			}
		}

So the fix is in two parts:

  • when the insert lands inside a wide cell, that wide cell is replaced by blank cells, the same way writing over one does - otherwise its tail stays behind and the placeholder that moves right belongs to nothing;
  • the cells the insert replaces are emptied before they are written, so the write does not read the content that has just moved right.

Reproduction

package main

import (
	"fmt"

	"github.com/charmbracelet/x/ansi"
	"github.com/charmbracelet/ultraviolet"
)

func main() {
	b := ultraviolet.NewBuffer(10, 2)
	b.SetCell(0, 0, &ultraviolet.Cell{Content: "漢", Width: 2})
	b.SetCell(4, 0, &ultraviolet.Cell{Content: "B", Width: 1})

	b.InsertCell(1, 0, 1, &ultraviolet.Cell{Content: "x", Width: 1})

	line := b.Line(0)
	for x, c := range line {
		if c.Width == 0 {
			fmt.Printf("placeholder at %d belongs to no wide cell\n", x)
		}
	}
	fmt.Printf("row renders %d columns, buffer is %d wide\n", ansi.StringWidth(line.Render()), b.Width())
	fmt.Printf("%q\n", line.Render())
}
placeholder at 2 belongs to no wide cell
row renders 9 columns, buffer is 10 wide
" z       "

The cell that was at column 4 is now at column 3, and the row is one column short: the terminal keeps whatever was in the last column of the row.

The same insert before a wide cell loses the wide character:

	b := ultraviolet.NewBuffer(10, 2)
	b.SetCell(2, 0, &ultraviolet.Cell{Content: "漢", Width: 2})
	b.SetCell(6, 0, &ultraviolet.Cell{Content: "B", Width: 1})

	b.InsertCell(2, 0, 1, &ultraviolet.Cell{Content: "x", Width: 1})
placeholder at 4 belongs to no wide cell

Impact

The buffer's own invariant - a placeholder belongs to the wide cell before it - is broken, and the row is rendered one column short, so the columns after the insert are shifted and the last column of the row keeps stale content on screen.

Buffer.InsertCell is the [ansi.ICH] path, so this is reachable from anything that inserts a cell into a row that holds a wide character.

Environment

  • ultraviolet main @ a45b7c8
  • go1.27 windows/amd64

InsertCell moved the cells to the right with a plain copy, so a wide cell
was split when the insert landed between its head and its placeholders:
the cell kept a tail that was no longer next to it, and the placeholder
that moved right belonged to nothing.

The cells the insert replaces also kept the content that had just moved
right until they were written, and the write treated a wide cell among
them as covering the cell that had moved next to it - which cleared the
wide character that had just moved there.

An insert that lands inside a wide cell now replaces that wide cell with
blank cells, the same way writing over one does, and the cells the insert
replaces are emptied before they are written.

Fixes charmbracelet#235

Signed-off-by: Shounak Joshi <shounakjoshi88@gmail.com>
@shounakjoshi88-a11y

Copy link
Copy Markdown
Author

The workflows on this pull request (build, coverage, lint, conformance) are in action_required, which needs a maintainer to approve them before they can run on a fork PR. If you approve them, they should pass - the whole suite is green locally:

$ go test ./...
ok  	github.com/charmbracelet/ultraviolet	2.831s
ok  	github.com/charmbracelet/ultraviolet/internal/casso	1.802s
ok  	github.com/charmbracelet/ultraviolet/internal/lru	1.802s
ok  	github.com/charmbracelet/ultraviolet/layout	2.050s
ok  	github.com/charmbracelet/ultraviolet/screen	1.939s

go vet ./... is clean and gofmt reports nothing on the two changed files.

The new test fails on the unfixed tree, with the two symptoms from the issue:

--- FAIL: TestBufferInsertCellWideCell/InsertCell_inside_a_wide_cell
    buffer_test.go:551: placeholder at 2 belongs to no wide cell
    buffer_test.go:557: row renders 9 columns, buffer is 10 wide
--- FAIL: TestBufferInsertCellWideCell/InsertCell_before_a_wide_cell
    buffer_test.go:587: placeholder at 4 belongs to no wide cell

and passes with the fix.

The issue is #235, with the root cause and the two-part fix.

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.

1 participant