1
0
mirror of https://github.com/lensapp/lens.git synced 2024-09-20 13:57:23 +03:00

Fix terminal fit errors (#5811)

This commit is contained in:
Sebastian Malton 2022-07-18 06:53:15 -07:00 committed by GitHub
parent ede8a2e91f
commit 5f746760c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 31 deletions

View File

@ -3,23 +3,35 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
export type Disposer = () => void;
interface Extendable<T> {
push(...vals: T[]): void;
export interface Disposer {
(): void;
}
export type ExtendableDisposer = Disposer & Extendable<Disposer>;
export function disposer(...args: (Disposer | undefined | null)[]): ExtendableDisposer {
const res = () => {
args.forEach(dispose => dispose?.());
args.length = 0;
};
res.push = (...vals: Disposer[]) => {
args.push(...vals);
};
return res;
export interface Disposable {
dispose(): void;
}
export interface ExtendableDisposer extends Disposer {
push(...vals: (Disposer | ExtendableDisposer | Disposable)[]): void;
}
export function disposer(...items: (Disposer | Disposable | undefined | null)[]): ExtendableDisposer {
return Object.assign(() => {
for (const item of items) {
if (!item) {
continue;
}
if (typeof item === "function") {
item();
} else {
item.dispose();
}
}
items.length = 0;
}, {
push: (...newItems) => items.push(...newItems),
} as Pick<ExtendableDisposer, "push">);
}

View File

@ -119,6 +119,9 @@ export class Terminal {
() => this.api.removeAllListeners(),
() => window.removeEventListener("resize", this.onResize),
() => this.elem.removeEventListener("contextmenu", this.onContextMenu),
this.xterm.onResize(({ cols, rows }) => {
this.api.sendTerminalSize(cols, rows);
}),
);
}
@ -127,21 +130,7 @@ export class Terminal {
this.xterm.dispose();
}
fit = () => {
try {
const { cols, rows } = this.fitAddon.proposeDimensions();
// attempt to resize/fit terminal when it's not visible in DOM will crash with exception
// see: https://github.com/xtermjs/xterm.js/issues/3118
if (isNaN(cols) || isNaN(rows)) return;
this.fitAddon.fit();
this.api.sendTerminalSize(cols, rows);
} catch (error) {
// see https://github.com/lensapp/lens/issues/1891
logger.error(`[TERMINAL]: failed to resize terminal to fit`, error);
}
};
fit = () => this.fitAddon.fit();
fitLazy = debounce(this.fit, 250);