Skip to content

Commit

Permalink
feat(runtime): add terminal.input for writing to stdin (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriPerkkio authored Sep 30, 2024
1 parent f49e910 commit c0b8f41
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
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

0 comments on commit c0b8f41

Please sign in to comment.