diff --git a/buffer.go b/buffer.go index 7b2094f..d3cd14b 100644 --- a/buffer.go +++ b/buffer.go @@ -106,6 +106,23 @@ func (l Line) At(x int) *Cell { return &l[x] } +// wideOwner returns the position of the wide cell that covers the given x, +// if there is one. A wide cell covers the cells that follow it, up to its +// width. +func (l Line) wideOwner(x int) (int, bool) { + for j := 1; x-j >= 0; j++ { + c := l.At(x - j) + if c == nil || c.Width == 1 { + // A narrow cell: no wide cell reaches past it. + return 0, false + } + if c.Width > j { + return x - j, true + } + } + return 0, false +} + // String returns the string representation of the line. Any trailing spaces // are removed. func (l Line) String() string { @@ -548,6 +565,14 @@ func (b *Buffer) InsertCellArea(x, y, n int, c *Cell, area Rectangle) { n = area.Max.X - x } + // An insert that lands inside a wide cell splits it: the cell keeps a + // tail that is no longer next to it, and the placeholder that moves + // right belongs to nothing. The wide cell is replaced by blank cells, + // the same way writing over one does. + if owner, ok := b.Lines[y].wideOwner(x); ok { + b.SetCell(owner, y, nil) + } + // 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 @@ -555,6 +580,13 @@ func (b *Buffer) InsertCellArea(x, y, n int, c *Cell, area Rectangle) { b.Lines[y][i] = b.Lines[y][i-n] } + // The cells the insert replaces still hold the content that has just + // moved right, and a wide cell among them would clear the cell that has + // moved next to it. They are written below, so they are emptied first. + for i := x; i < x+n && i < area.Max.X; i++ { + b.Lines[y][i] = EmptyCell + } + // Clear the newly inserted cells within rectangle bounds for i := x; i < x+n && i < area.Max.X; i++ { b.SetCell(i, y, c) diff --git a/buffer_test.go b/buffer_test.go index ccdb462..db5adff 100644 --- a/buffer_test.go +++ b/buffer_test.go @@ -512,6 +512,84 @@ func TestBufferLineOperations(t *testing.T) { } // TestBufferCellOperations tests cell insertion and deletion +func TestBufferInsertCellWideCell(t *testing.T) { + // An insert that lands inside a wide cell used to leave the placeholder + // of that cell behind, where it belonged to nothing and shifted the rest + // of the row one column to the left. + t.Run("InsertCell inside a wide cell", func(t *testing.T) { + b := NewBuffer(10, 2) + b.SetCell(0, 0, &Cell{Content: "\u6f22", Width: 2}) + b.SetCell(4, 0, &Cell{Content: "B", Width: 1}) + + b.InsertCell(1, 0, 1, &Cell{Content: "x", Width: 1}) + + // The cell after the wide one keeps its place: the row is not + // shifted to the left. + if c := b.CellAt(5, 0); c == nil || c.Content != "B" { + t.Errorf("InsertCell moved the cell after the wide one: got %v", c) + } + + // The inserted cell is where it was asked for. + if c := b.CellAt(1, 0); c == nil || c.Content != "x" { + t.Errorf("InsertCell did not insert the cell: got %v", c) + } + + // No placeholder is left without the wide cell it belongs to. + line := b.Line(0) + for x, c := range line { + if c.Width != 0 { + continue + } + owner := false + for j := 1; x-j >= 0; j++ { + if w := line[x-j]; w.Width > j { + owner = true + break + } + } + if !owner { + t.Errorf("placeholder at %d belongs to no wide cell", x) + } + } + + // The row still fills the buffer. + if w := ansi.StringWidth(line.Render()); w != b.Width() { + t.Errorf("row renders %d columns, buffer is %d wide", w, b.Width()) + } + }) + + t.Run("InsertCell before a wide cell", func(t *testing.T) { + b := NewBuffer(10, 2) + b.SetCell(2, 0, &Cell{Content: "\u6f22", Width: 2}) + b.SetCell(6, 0, &Cell{Content: "B", Width: 1}) + + b.InsertCell(2, 0, 1, &Cell{Content: "x", Width: 1}) + + // The wide cell moves right as a whole, so the cell after it moves + // right too. + if c := b.CellAt(7, 0); c == nil || c.Content != "B" { + t.Errorf("InsertCell moved the cell after the wide one: got %v", c) + } + + line := b.Line(0) + for x, c := range line { + if c.Width != 0 { + continue + } + owner := false + for j := 1; x-j >= 0; j++ { + if w := line[x-j]; w.Width > j { + owner = true + break + } + } + if !owner { + t.Errorf("placeholder at %d belongs to no wide cell", x) + } + } + }) +} + func TestBufferCellOperations(t *testing.T) { t.Run("InsertCell", func(t *testing.T) { b := NewBuffer(5, 2)