@@ -130,7 +130,7 @@ export function ReplTerminal({
130130
131131 // inputBufferを更新し、画面に描画する
132132 const updateBuffer = useCallback (
133- ( newBuffer : ( ) => string [ ] ) => {
133+ ( newBuffer : ( ( ) => string [ ] ) | null , insertBefore ?: ( ) => void ) => {
134134 if ( terminalInstanceRef . current ) {
135135 hideCursor ( terminalInstanceRef . current ) ;
136136 // バッファの行数分カーソルを戻す
@@ -142,8 +142,12 @@ export function ReplTerminal({
142142 terminalInstanceRef . current . write ( "\r" ) ;
143143 // バッファの内容をクリア
144144 terminalInstanceRef . current . write ( "\x1b[0J" ) ;
145- // 新しいバッファの内容を表示
146- inputBuffer . current = newBuffer ( ) ;
145+ // バッファの前に追加で出力する内容(前のコマンドの出力)があればここで書き込む
146+ insertBefore ?.( ) ;
147+ // 新しいバッファの内容を表示、nullなら現状維持
148+ if ( newBuffer ) {
149+ inputBuffer . current = newBuffer ( ) ;
150+ }
147151 for ( let i = 0 ; i < inputBuffer . current . length ; i ++ ) {
148152 terminalInstanceRef . current . write (
149153 ( i === 0 ? prompt : ( promptMore ?? prompt ) ) ?? "> "
@@ -214,12 +218,22 @@ export function ReplTerminal({
214218 const command = inputBuffer . current . join ( "\n" ) . trim ( ) ;
215219 inputBuffer . current = [ ] ;
216220 const collectedOutputs : ReplOutput [ ] = [ ] ;
221+ let executionDone = false ;
217222 await runtimeMutex . runExclusive ( async ( ) => {
218223 await runCommand ( command , ( output ) => {
219- collectedOutputs . push ( output ) ;
220- handleOutput ( output ) ;
224+ if ( executionDone ) {
225+ // すでに完了していて次のコマンドのプロンプトが出ている場合、その前に挿入
226+ updateBuffer ( null , ( ) => {
227+ handleOutput ( output ) ;
228+ } ) ;
229+ // TODO: embedContextのaddReplOutputにも出力を追加する必要があるが、それに対応したAPIになっていない
230+ } else {
231+ collectedOutputs . push ( output ) ;
232+ handleOutput ( output ) ;
233+ }
221234 } ) ;
222235 } ) ;
236+ executionDone = true ;
223237 updateBuffer ( ( ) => [ "" ] ) ;
224238 addReplOutput ?.( terminalId , command , collectedOutputs ) ;
225239 }
0 commit comments