Skip to content

Commit c0b8f41

Browse files
authored
feat(runtime): add terminal.input for writing to stdin (#350)
1 parent f49e910 commit c0b8f41

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

packages/runtime/src/utils/terminal.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface ITerminal {
44

55
reset: () => void;
66
write: (data: string) => void;
7+
input: (data: string) => void;
78
onData: (cb: (data: string) => void) => void;
89
}
910

packages/runtime/src/webcontainer/terminal-config.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class TerminalPanel implements ITerminal {
6666

6767
private _terminal?: ITerminal;
6868
private _process?: WebContainerProcess;
69-
private _data: string[] = [];
69+
private _data: { data: string; type: 'input' | 'echo' }[] = [];
7070
private _onData?: (data: string) => void;
7171

7272
constructor(
@@ -130,11 +130,24 @@ export class TerminalPanel implements ITerminal {
130130
}
131131
}
132132

133+
/** @internal*/
133134
write(data: string) {
134135
if (this._terminal) {
135136
this._terminal.write(data);
136137
} else {
137-
this._data.push(data);
138+
this._data.push({ data, type: 'echo' });
139+
}
140+
}
141+
142+
input(data: string) {
143+
if (this.type !== 'terminal') {
144+
throw new Error('Cannot write data to output-only terminal');
145+
}
146+
147+
if (this._terminal) {
148+
this._terminal.input(data);
149+
} else {
150+
this._data.push({ data, type: 'input' });
138151
}
139152
}
140153

@@ -166,8 +179,12 @@ export class TerminalPanel implements ITerminal {
166179
* @param terminal The terminal.
167180
*/
168181
attachTerminal(terminal: ITerminal) {
169-
for (const data of this._data) {
170-
terminal.write(data);
182+
for (const { type, data } of this._data) {
183+
if (type === 'echo') {
184+
terminal.write(data);
185+
} else {
186+
terminal.input(data);
187+
}
171188
}
172189

173190
this._data = [];

0 commit comments

Comments
 (0)