Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -548,13 +565,28 @@ 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
// right.
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)
Expand Down
78 changes: 78 additions & 0 deletions buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down