@@ -17,7 +17,13 @@ import { useEmbedContext } from "./embedContext";
1717import { emptyMutex , langConstants , RuntimeLang , useRuntime } from "./runtime" ;
1818import clsx from "clsx" ;
1919
20- export type ReplOutputType = "stdout" | "stderr" | "error" | "return" | "trace" | "system" ;
20+ export type ReplOutputType =
21+ | "stdout"
22+ | "stderr"
23+ | "error"
24+ | "return"
25+ | "trace"
26+ | "system" ;
2127export interface ReplOutput {
2228 type : ReplOutputType ; // 出力の種類
2329 message : string ; // 出力メッセージ
@@ -30,47 +36,37 @@ export type SyntaxStatus = "complete" | "incomplete" | "invalid"; // 構文チ
3036
3137export function writeOutput (
3238 term : Terminal ,
33- outputs : ReplOutput [ ] ,
34- endNewLine : boolean ,
39+ output : ReplOutput ,
3540 returnPrefix : string | undefined ,
3641 Prism : typeof import ( "prismjs" ) | null ,
3742 language : RuntimeLang
3843) {
39- for ( let i = 0 ; i < outputs . length ; i ++ ) {
40- const output = outputs [ i ] ;
41- if ( i > 0 ) {
42- term . writeln ( "" ) ;
43- }
44- // 出力内容に応じて色を変える
45- const message = String ( output . message ) . replace ( / \n / g, "\r\n" ) ;
46- switch ( output . type ) {
47- case "error" :
48- term . write ( chalk . red ( message ) ) ;
49- break ;
50- case "trace" :
51- term . write ( chalk . blue . italic ( message ) ) ;
52- break ;
53- case "system" :
54- term . write ( systemMessageColor ( message ) ) ;
55- break ;
56- case "return" :
57- if ( returnPrefix ) {
58- term . write ( returnPrefix ) ;
59- }
60- if ( Prism ) {
61- term . write ( highlightCodeToAnsi ( Prism , message , language ) ) ;
62- } else {
63- console . warn ( "Prism is not loaded, cannot highlight return value" ) ;
64- term . write ( message ) ;
65- }
66- break ;
67- default :
68- term . write ( message ) ;
69- break ;
70- }
71- }
72- if ( endNewLine && outputs . length > 0 ) {
73- term . writeln ( "" ) ;
44+ // 出力内容に応じて色を変える
45+ const message = String ( output . message ) . replace ( / \n / g, "\r\n" ) ;
46+ switch ( output . type ) {
47+ case "error" :
48+ term . writeln ( chalk . red ( message ) ) ;
49+ break ;
50+ case "trace" :
51+ term . writeln ( chalk . blue . italic ( message ) ) ;
52+ break ;
53+ case "system" :
54+ term . writeln ( systemMessageColor ( message ) ) ;
55+ break ;
56+ case "return" :
57+ if ( returnPrefix ) {
58+ term . write ( returnPrefix ) ;
59+ }
60+ if ( Prism ) {
61+ term . writeln ( highlightCodeToAnsi ( Prism , message , language ) ) ;
62+ } else {
63+ console . warn ( "Prism is not loaded, cannot highlight return value" ) ;
64+ term . writeln ( message ) ;
65+ }
66+ break ;
67+ default :
68+ term . writeln ( message ) ;
69+ break ;
7470 }
7571}
7672
@@ -180,8 +176,7 @@ export function ReplTerminal({
180176 if ( terminalInstanceRef . current ) {
181177 writeOutput (
182178 terminalInstanceRef . current ,
183- [ output ] ,
184- false ,
179+ output ,
185180 returnPrefix ,
186181 Prism ,
187182 language
@@ -219,17 +214,12 @@ export function ReplTerminal({
219214 const command = inputBuffer . current . join ( "\n" ) . trim ( ) ;
220215 inputBuffer . current = [ ] ;
221216 const collectedOutputs : ReplOutput [ ] = [ ] ;
222- let isFirstOutput = true ;
223217 await runtimeMutex . runExclusive ( async ( ) => {
224218 await runCommand ( command , ( output ) => {
225219 collectedOutputs . push ( output ) ;
226220 handleOutput ( output ) ;
227- isFirstOutput = false ;
228221 } ) ;
229222 } ) ;
230- if ( ! isFirstOutput && terminalInstanceRef . current ) {
231- terminalInstanceRef . current . writeln ( "" ) ;
232- }
233223 updateBuffer ( ( ) => [ "" ] ) ;
234224 addReplOutput ?.( terminalId , command , collectedOutputs ) ;
235225 }
@@ -311,7 +301,6 @@ export function ReplTerminal({
311301 for ( const output of cmd . output ) {
312302 handleOutput ( output ) ;
313303 }
314- terminalInstanceRef . current ! . writeln ( "" ) ;
315304 updateBuffer ( ( ) => [ "" ] ) ;
316305 }
317306 } else {
@@ -352,7 +341,6 @@ export function ReplTerminal({
352341 for ( const output of cmd . output ) {
353342 handleOutput ( output ) ;
354343 }
355- terminalInstanceRef . current ! . writeln ( "" ) ;
356344 updateBuffer ( ( ) => [ "" ] ) ;
357345 }
358346 }
0 commit comments