Skip to content
This repository was archived by the owner on Jun 18, 2025. It is now read-only.

Commit 0db251b

Browse files
committed
fix: 🐛 cells do not have properly initialized coordinates
Each virtual cell has a default (0, 0) coordinate. When using methods like eraseScreen() it modifies all the cells, but misses the coordinates, which remains (0, 0). It leads to unusual behaviour sometimes and was fixed with this commit. Now, when Canvas initializes an array of cells it sets its coordinates immediately based on index in the array.
1 parent a6e5024 commit 0db251b

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/canvas/Canvas.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ export class Canvas implements ICanvasOptions {
5353
this.height = options.height;
5454
}
5555

56-
this.cells = Array.from<Cell>({ length: this.width * this.height }).map(() => new Cell(' '));
56+
this.cells = Array
57+
.from<Cell>({ length: this.width * this.height })
58+
.map((_, index) => new Cell(' ', { x: this.getXYFromPointer(index)[0], y: this.getXYFromPointer(index)[1] }));
59+
5760
this.lastFrame = Array.from<string>({ length: this.width * this.height }).fill('');
5861
}
5962

test/Canvas.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ describe('Canvas', () => {
5656
expect(canvas.height).toEqual(10);
5757
});
5858

59+
it('Should properly initialize the coordinates for the cells', () => {
60+
const canvas = new Canvas({ width: 2, height: 2 });
61+
62+
expect(canvas.cells[0].x).toBe(0);
63+
expect(canvas.cells[0].y).toBe(0);
64+
65+
expect(canvas.cells[1].x).toBe(1);
66+
expect(canvas.cells[1].y).toBe(0);
67+
68+
expect(canvas.cells[2].x).toBe(0);
69+
expect(canvas.cells[2].y).toBe(1);
70+
71+
expect(canvas.cells[3].x).toBe(1);
72+
expect(canvas.cells[3].y).toBe(1);
73+
});
74+
5975
it('Should properly write to the canvas', () => {
6076
const canvas = new Canvas({ width: 20, height: 10 });
6177
expect(canvas.cells[0].getChar()).toEqual(' ');

0 commit comments

Comments
 (0)