Skip to content

Commit 30c97a2

Browse files
committed
Handle more edge cases
1 parent 1186d09 commit 30c97a2

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

addons/addon-image/src/kitty/KittyGraphicsHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,8 @@ export class KittyGraphicsHandler implements IApcHandler, IResetHandler, IDispos
530530

531531
const cropX = Math.max(0, cmd.x ?? 0);
532532
const cropY = Math.max(0, cmd.y ?? 0);
533-
const cropW = cmd.sourceWidth ?? (bitmap.width - cropX);
534-
const cropH = cmd.sourceHeight ?? (bitmap.height - cropY);
533+
const cropW = cmd.sourceWidth || (bitmap.width - cropX);
534+
const cropH = cmd.sourceHeight || (bitmap.height - cropY);
535535

536536
const maxCropW = Math.max(0, bitmap.width - cropX);
537537
const maxCropH = Math.max(0, bitmap.height - cropY);

addons/addon-image/test/KittyGraphics.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,6 +1431,55 @@ test.describe('Kitty Graphics Protocol', () => {
14311431
deepStrictEqual(await getPixel(0, 0, 4, 2), [0, 0, 0, 0]);
14321432
deepStrictEqual(await getPixel(0, 0, 5, 3), [0, 0, 0, 255]);
14331433
});
1434+
1435+
test('w=0 is treated as unset (displays full width)', async () => {
1436+
await ctx.proxy.write(`\x1b_Ga=T,f=100,w=0;${KITTY_MULTICOLOR_200X100_BASE64}\x1b\\`);
1437+
await timeout(200);
1438+
1439+
deepStrictEqual(await getOrigSize(1), [200, 100]);
1440+
deepStrictEqual(await getPixel(0, 0, 0, 0), [255, 0, 0, 255]);
1441+
deepStrictEqual(await getPixel(0, 0, 199, 99), [255, 255, 255, 255]);
1442+
});
1443+
1444+
test('h=0 is treated as unset (displays full height)', async () => {
1445+
await ctx.proxy.write(`\x1b_Ga=T,f=100,h=0;${KITTY_MULTICOLOR_200X100_BASE64}\x1b\\`);
1446+
await timeout(200);
1447+
1448+
deepStrictEqual(await getOrigSize(1), [200, 100]);
1449+
deepStrictEqual(await getPixel(0, 0, 0, 0), [255, 0, 0, 255]);
1450+
deepStrictEqual(await getPixel(0, 0, 0, 50), [255, 192, 203, 255]);
1451+
});
1452+
1453+
test('x exceeding image width produces no display', async () => {
1454+
await ctx.proxy.write(`\x1b_Ga=T,f=100,x=999;${KITTY_MULTICOLOR_200X100_BASE64}\x1b\\`);
1455+
await timeout(200);
1456+
1457+
strictEqual(await getImageStorageLength(), 0);
1458+
});
1459+
1460+
test('negative x/y values are clamped to 0', async () => {
1461+
await ctx.proxy.write(`\x1b_Ga=T,f=100,x=-10,y=-10;${KITTY_MULTICOLOR_200X100_BASE64}\x1b\\`);
1462+
await timeout(200);
1463+
1464+
deepStrictEqual(await getOrigSize(1), [200, 100]);
1465+
deepStrictEqual(await getPixel(0, 0, 0, 0), [255, 0, 0, 255]);
1466+
});
1467+
1468+
test('combined crop and sub-cell offset', async () => {
1469+
await ctx.proxy.write(`\x1b_Ga=T,f=100,x=20,y=0,w=20,h=50,X=5,Y=3;${KITTY_MULTICOLOR_200X100_BASE64}\x1b\\`);
1470+
await timeout(200);
1471+
1472+
deepStrictEqual(await getPixel(0, 0, 0, 0), [0, 0, 0, 0]);
1473+
deepStrictEqual(await getPixel(0, 0, 4, 2), [0, 0, 0, 0]);
1474+
deepStrictEqual(await getPixel(0, 0, 5, 3), [255, 128, 0, 255]);
1475+
});
1476+
1477+
test('sub-cell offset with explicit c/r advances cursor correctly', async () => {
1478+
await ctx.proxy.write(`\x1b_Ga=T,f=100,X=5,Y=3,c=4,r=2;${KITTY_BLACK_1X1_BASE64}\x1b\\`);
1479+
await timeout(100);
1480+
1481+
deepStrictEqual(await getCursor(), [4, 1]);
1482+
});
14341483
});
14351484

14361485
test.describe('Query support', () => {

0 commit comments

Comments
 (0)