Skip to content

feat(runtime): add terminal.input for writing to stdin #350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/runtime/src/utils/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface ITerminal {

reset: () => void;
write: (data: string) => void;
input: (data: string) => void;
onData: (cb: (data: string) => void) => void;
}

Expand Down
25 changes: 21 additions & 4 deletions packages/runtime/src/webcontainer/terminal-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class TerminalPanel implements ITerminal {

private _terminal?: ITerminal;
private _process?: WebContainerProcess;
private _data: string[] = [];
private _data: { data: string; type: 'input' | 'echo' }[] = [];
private _onData?: (data: string) => void;

constructor(
Expand Down Expand Up @@ -130,11 +130,24 @@ export class TerminalPanel implements ITerminal {
}
}

/** @internal*/
write(data: string) {
if (this._terminal) {
this._terminal.write(data);
} else {
this._data.push(data);
this._data.push({ data, type: 'echo' });
}
}

input(data: string) {
if (this.type !== 'terminal') {
throw new Error('Cannot write data to output-only terminal');
}

if (this._terminal) {
this._terminal.input(data);
} else {
this._data.push({ data, type: 'input' });
}
}

Expand Down Expand Up @@ -166,8 +179,12 @@ export class TerminalPanel implements ITerminal {
* @param terminal The terminal.
*/
attachTerminal(terminal: ITerminal) {
for (const data of this._data) {
terminal.write(data);
for (const { type, data } of this._data) {
if (type === 'echo') {
terminal.write(data);
} else {
terminal.input(data);
}
}

this._data = [];
Expand Down