@@ -32,14 +32,16 @@ interface IEmbedContext {
3232 ) => Promise < Readonly < Record < Filename , string > > > ;
3333
3434 replOutputs : Readonly < Record < TerminalId , ReplCommand [ ] > > ;
35+ addReplCommand : ( terminalId : TerminalId , command : string ) => string ;
3536 addReplOutput : (
3637 terminalId : TerminalId ,
37- command : string ,
38- output : ReplOutput [ ]
38+ commandId : string ,
39+ output : ReplOutput
3940 ) => void ;
4041
4142 execResults : Readonly < Record < Filename , ReplOutput [ ] > > ;
42- setExecResult : ( filename : Filename , output : ReplOutput [ ] ) => void ;
43+ clearExecResult : ( filename : Filename ) => void ;
44+ addExecOutput : ( filename : Filename , output : ReplOutput ) => void ;
4345}
4446const EmbedContext = createContext < IEmbedContext > ( null ! ) ;
4547
@@ -63,6 +65,10 @@ export function EmbedContextProvider({ children }: { children: ReactNode }) {
6365 const [ replOutputs , setReplOutputs ] = useState <
6466 Record < TerminalId , ReplCommand [ ] >
6567 > ( { } ) ;
68+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
69+ const [ commandIdCounters , setCommandIdCounters ] = useState <
70+ Record < TerminalId , number >
71+ > ( { } ) ;
6672 const [ execResults , setExecResults ] = useState <
6773 Record < Filename , ReplOutput [ ] >
6874 > ( { } ) ;
@@ -71,6 +77,7 @@ export function EmbedContextProvider({ children }: { children: ReactNode }) {
7177 if ( pathname && pathname !== currentPathname ) {
7278 setCurrentPathname ( pathname ) ;
7379 setReplOutputs ( { } ) ;
80+ setCommandIdCounters ( { } ) ;
7481 setExecResults ( { } ) ;
7582 }
7683 } , [ pathname , currentPathname ] ) ;
@@ -100,26 +107,70 @@ export function EmbedContextProvider({ children }: { children: ReactNode }) {
100107 } ,
101108 [ pathname ]
102109 ) ;
103- const addReplOutput = useCallback (
104- ( terminalId : TerminalId , command : string , output : ReplOutput [ ] ) =>
110+ const addReplCommand = useCallback (
111+ ( terminalId : TerminalId , command : string ) : string => {
112+ let commandId = "" ;
113+ setCommandIdCounters ( ( counters ) => {
114+ const newCounters = { ...counters } ;
115+ const currentCount = newCounters [ terminalId ] ?? 0 ;
116+ commandId = String ( currentCount ) ;
117+ newCounters [ terminalId ] = currentCount + 1 ;
118+ return newCounters ;
119+ } ) ;
105120 setReplOutputs ( ( outs ) => {
106121 outs = { ...outs } ;
107122 if ( ! ( terminalId in outs ) ) {
108123 outs [ terminalId ] = [ ] ;
109124 }
110125 outs [ terminalId ] = [
111126 ...outs [ terminalId ] ,
112- { command : command , output : output } ,
127+ { command : command , output : [ ] , commandId } ,
113128 ] ;
114129 return outs ;
130+ } ) ;
131+ return commandId ;
132+ } ,
133+ [ ]
134+ ) ;
135+ const addReplOutput = useCallback (
136+ ( terminalId : TerminalId , commandId : string , output : ReplOutput ) =>
137+ setReplOutputs ( ( outs ) => {
138+ outs = { ...outs } ;
139+ if ( terminalId in outs ) {
140+ outs [ terminalId ] = [ ...outs [ terminalId ] ] ;
141+ // Find the command by commandId
142+ const commandIndex = outs [ terminalId ] . findIndex (
143+ ( cmd ) => cmd . commandId === commandId
144+ ) ;
145+ if ( commandIndex >= 0 ) {
146+ const command = outs [ terminalId ] [ commandIndex ] ;
147+ outs [ terminalId ] [ commandIndex ] = {
148+ ...command ,
149+ output : [ ...command . output , output ] ,
150+ } ;
151+ }
152+ }
153+ return outs ;
154+ } ) ,
155+ [ ]
156+ ) ;
157+ const clearExecResult = useCallback (
158+ ( filename : Filename ) =>
159+ setExecResults ( ( results ) => {
160+ results = { ...results } ;
161+ results [ filename ] = [ ] ;
162+ return results ;
115163 } ) ,
116164 [ ]
117165 ) ;
118- const setExecResult = useCallback (
119- ( filename : Filename , output : ReplOutput [ ] ) =>
166+ const addExecOutput = useCallback (
167+ ( filename : Filename , output : ReplOutput ) =>
120168 setExecResults ( ( results ) => {
121169 results = { ...results } ;
122- results [ filename ] = output ;
170+ if ( ! ( filename in results ) ) {
171+ results [ filename ] = [ ] ;
172+ }
173+ results [ filename ] = [ ...results [ filename ] , output ] ;
123174 return results ;
124175 } ) ,
125176 [ ]
@@ -131,9 +182,11 @@ export function EmbedContextProvider({ children }: { children: ReactNode }) {
131182 files : files [ pathname ] || { } ,
132183 writeFile,
133184 replOutputs,
185+ addReplCommand,
134186 addReplOutput,
135187 execResults,
136- setExecResult,
188+ clearExecResult,
189+ addExecOutput,
137190 } }
138191 >
139192 { children }
0 commit comments