From 3489a3151caa47794eeb6d038fdac234f0e7392a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Breitbart?= Date: Sat, 10 Dec 2022 14:39:50 +0100 Subject: [PATCH] revert to partial #37 fix, remove workerPath from arguments --- src/ImageAddon.ts | 2 +- src/ImageStorage.ts | 18 ++++++++++++++++++ src/SixelHandler.ts | 12 +++++++++++- test/ImageAddon.api.ts | 12 +++++------- typings/xterm-addon-image.d.ts | 2 +- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/src/ImageAddon.ts b/src/ImageAddon.ts index 760d644..aa034d1 100644 --- a/src/ImageAddon.ts +++ b/src/ImageAddon.ts @@ -54,7 +54,7 @@ export class ImageAddon implements ITerminalAddon { private _terminal: ITerminalExt | undefined; private _handlers: Map = new Map(); - constructor(workerPath: string, opts: Partial) { + constructor(opts?: Partial) { this._opts = Object.assign({}, DEFAULT_OPTIONS, opts); this._defaultOpts = Object.assign({}, DEFAULT_OPTIONS, opts); } diff --git a/src/ImageStorage.ts b/src/ImageStorage.ts index b3b21ca..b78c36b 100644 --- a/src/ImageStorage.ts +++ b/src/ImageStorage.ts @@ -190,6 +190,24 @@ export class ImageStorage implements IDisposable { this._fullyCleared = false; } + /** + * Only advance text cursor. + * This is an edge case from empty sixels carrying only a height but no pixels. + * Partially fixes https://github.com/jerch/xterm-addon-image/issues/37. + */ + public advanceCursor(height: number): void { + if (this._opts.sixelScrolling) { + let cellSize = this._renderer.cellSize; + if (cellSize.width === -1 || cellSize.height === -1) { + cellSize = CELL_SIZE_DEFAULT; + } + const rows = Math.ceil(height / cellSize.height); + for (let i = 1; i < rows; ++i) { + this._terminal._core._inputHandler.lineFeed(); + } + } + } + /** * Method to add an image to the storage. */ diff --git a/src/SixelHandler.ts b/src/SixelHandler.ts index 8177ddd..1a35981 100644 --- a/src/SixelHandler.ts +++ b/src/SixelHandler.ts @@ -83,11 +83,21 @@ export class SixelHandler implements IDcsHandler, IResetHandler { } public unhook(success: boolean): boolean | Promise { - if (this._aborted || !success || !this._dec) return true; + if (this._aborted || !success || !this._dec) { + return true; + } const width = this._dec.width; const height = this._dec.height; + // partial fix for https://github.com/jerch/xterm-addon-image/issues/37 + if (!width || ! height) { + if (height) { + this._storage.advanceCursor(height); + } + return true; + } + const canvas = ImageRenderer.createCanvas(this._coreTerminal._core._coreBrowserService.window, width, height); canvas.getContext('2d')?.putImageData(new ImageData(this._dec.data8, width, height), 0, 0); if (this._dec.memoryUsage > MEM_PERMA_LIMIT) { diff --git a/test/ImageAddon.api.ts b/test/ImageAddon.api.ts index df479c3..a6fef72 100644 --- a/test/ImageAddon.api.ts +++ b/test/ImageAddon.api.ts @@ -20,7 +20,7 @@ const height = 600; // eslint-disable-next-line declare const ImageAddon: { - new(workerPath: string, options?: Partial): any; + new(options?: Partial): any; }; interface ITestData { @@ -38,8 +38,6 @@ interface IDimensions { height: number; } -const IMAGE_WORKER_PATH = '/workers/xterm-addon-image-worker.js'; - // image: 640 x 80, 512 color const TESTDATA: ITestData = (() => { const pngImage = PNG.load(readFileSync('./addons/xterm-addon-image/fixture/palette.png')); @@ -76,9 +74,9 @@ describe.only('ImageAddon', () => { await page.goto(APP); await openTerminal(page); await page.evaluate(opts => { - (window as any).imageAddon = new ImageAddon(opts.workerPath, opts.opts); + (window as any).imageAddon = new ImageAddon(opts.opts); (window as any).term.loadAddon((window as any).imageAddon); - }, { workerPath: IMAGE_WORKER_PATH, opts: { sixelPaletteLimit: 512 } }); + }, {opts: { sixelPaletteLimit: 512 } }); }); it('test for private accessors', async () => { @@ -133,9 +131,9 @@ describe.only('ImageAddon', () => { showPlaceholder: false }; await page.evaluate(opts => { - (window as any).imageAddonCustom = new ImageAddon(opts.workerPath, opts.opts); + (window as any).imageAddonCustom = new ImageAddon(opts.opts); (window as any).term.loadAddon((window as any).imageAddonCustom); - }, { workerPath: IMAGE_WORKER_PATH, opts: customSettings }); + }, {opts: customSettings }); assert.deepEqual(await page.evaluate(`window.imageAddonCustom._opts`), customSettings); }); }); diff --git a/typings/xterm-addon-image.d.ts b/typings/xterm-addon-image.d.ts index 8d266fd..b1598f4 100644 --- a/typings/xterm-addon-image.d.ts +++ b/typings/xterm-addon-image.d.ts @@ -70,7 +70,7 @@ declare module 'xterm-addon-image' { } export class ImageAddon implements ITerminalAddon { - constructor(workerPath: string, options?: IImageAddonOptions); + constructor(options?: IImageAddonOptions); public activate(terminal: Terminal): void; public dispose(): void;