1
0
mirror of https://github.com/lensapp/lens.git synced 2024-11-10 10:36:25 +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. * 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 interface Disposable {
dispose(): void;
export function disposer(...args: (Disposer | undefined | null)[]): ExtendableDisposer { }
const res = () => {
args.forEach(dispose => dispose?.()); export interface ExtendableDisposer extends Disposer {
args.length = 0; push(...vals: (Disposer | ExtendableDisposer | Disposable)[]): void;
}; }
res.push = (...vals: Disposer[]) => { export function disposer(...items: (Disposer | Disposable | undefined | null)[]): ExtendableDisposer {
args.push(...vals); return Object.assign(() => {
}; for (const item of items) {
if (!item) {
return res; 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(), () => this.api.removeAllListeners(),
() => window.removeEventListener("resize", this.onResize), () => window.removeEventListener("resize", this.onResize),
() => this.elem.removeEventListener("contextmenu", this.onContextMenu), () => 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(); this.xterm.dispose();
} }
fit = () => { fit = () => this.fitAddon.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);
}
};
fitLazy = debounce(this.fit, 250); fitLazy = debounce(this.fit, 250);