Skip to content

Commit 6a281bd

Browse files
authored
Merge pull request #4953 from arencoskun/master
Expose API method for writing to application side (#4948)
2 parents fb54bb0 + 1c4e011 commit 6a281bd

File tree

8 files changed

+42
-0
lines changed

8 files changed

+42
-0
lines changed

src/browser/TestUtils.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ export class MockTerminal implements ITerminal {
7272
public focus(): void {
7373
throw new Error('Method not implemented.');
7474
}
75+
public input(data: string, wasUserInput: boolean = true): void {
76+
throw new Error('Method not implemented.');
77+
}
7578
public resize(columns: number, rows: number): void {
7679
throw new Error('Method not implemented.');
7780
}

src/browser/public/Terminal.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ export class Terminal extends Disposable implements ITerminalApi {
138138
public focus(): void {
139139
this._core.focus();
140140
}
141+
public input(data: string, wasUserInput: boolean = true): void {
142+
this._core.input(data, wasUserInput);
143+
}
141144
public resize(columns: number, rows: number): void {
142145
this._verifyIntegers(columns, rows);
143146
this._core.resize(columns, rows);

src/common/CoreTerminal.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ export abstract class CoreTerminal extends Disposable implements ICoreTerminal {
168168
this._writeBuffer.writeSync(data, maxSubsequentCalls);
169169
}
170170

171+
public input(data: string, wasUserInput: boolean = true): void {
172+
this.coreService.triggerDataEvent(data, wasUserInput);
173+
}
174+
171175
public resize(x: number, y: number): void {
172176
if (isNaN(x) || isNaN(y)) {
173177
return;

src/headless/Terminal.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ export class Terminal extends CoreTerminal {
8181
this._onBell.fire();
8282
}
8383

84+
public input(data: string, wasUserInput: boolean = true): void {
85+
this.coreService.triggerDataEvent(data, wasUserInput);
86+
}
87+
8488
/**
8589
* Resizes the terminal.
8690
*

src/headless/public/Terminal.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ export class Terminal extends Disposable implements ITerminalApi {
134134
this._publicOptions[propName] = options[propName];
135135
}
136136
}
137+
public input(data: string, wasUserInput: boolean = true): void {
138+
this._core.input(data, wasUserInput);
139+
}
137140
public resize(columns: number, rows: number): void {
138141
this._verifyIntegers(columns, rows);
139142
this._core.resize(columns, rows);

test/playwright/TestUtils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ export class TerminalProxy implements ITerminalProxyCustomMethods, PlaywrightApi
216216
return new Promise(r => term.writeln(typeof data === 'string' ? data : new Uint8Array(data), r));
217217
}, [await this.getHandle(), typeof data === 'string' ? data : Array.from(data)] as const);
218218
}
219+
public async input(data: string, wasUserInput: boolean = true): Promise<void> { return this.evaluate(([term]) => term.input(data, wasUserInput)); }
219220
public async resize(cols: number, rows: number): Promise<void> { return this._page.evaluate(([term, cols, rows]) => term.resize(cols, rows), [await this.getHandle(), cols, rows] as const); }
220221
public async registerMarker(y?: number | undefined): Promise<IMarker> { return this._page.evaluate(([term, y]) => term.registerMarker(y), [await this.getHandle(), y] as const); }
221222
public async registerDecoration(decorationOptions: IDecorationOptions): Promise<IDecoration | undefined> { return this._page.evaluate(([term, decorationOptions]) => term.registerDecoration(decorationOptions), [await this.getHandle(), decorationOptions] as const); }

typings/xterm-headless.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,18 @@ declare module '@xterm/headless' {
718718
*/
719719
onTitleChange: IEvent<string>;
720720

721+
/**
722+
* Input data to application side. The data is treated the same way input
723+
* typed into the terminal would (ie. the {@link onData} event will fire).
724+
* @param data The data to forward to the application.
725+
* @param wasUserInput Whether the input is genuine user input. This is true
726+
* by default and triggers additionalbehavior like focus or selection
727+
* clearing. Set this to false if the data sent should not be treated like
728+
* user input would, for example passing an escape sequence to the
729+
* application.
730+
*/
731+
input(data: string, wasUserInput?: boolean): void;
732+
721733
/**
722734
* Resizes the terminal. It's best practice to debounce calls to resize,
723735
* this will help ensure that the pty can respond to the resize event

typings/xterm.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,18 @@ declare module '@xterm/xterm' {
963963
*/
964964
focus(): void;
965965

966+
/**
967+
* Input data to application side. The data is treated the same way input
968+
* typed into the terminal would (ie. the {@link onData} event will fire).
969+
* @param data The data to forward to the application.
970+
* @param wasUserInput Whether the input is genuine user input. This is true
971+
* by default and triggers additionalbehavior like focus or selection
972+
* clearing. Set this to false if the data sent should not be treated like
973+
* user input would, for example passing an escape sequence to the
974+
* application.
975+
*/
976+
input(data: string, wasUserInput?: boolean): void;
977+
966978
/**
967979
* Resizes the terminal. It's best practice to debounce calls to resize,
968980
* this will help ensure that the pty can respond to the resize event

0 commit comments

Comments
 (0)