@@ -185,6 +185,60 @@ function createWorkspaceTerminalSession(sdk: ReturnType<typeof useSDK>, dir: str
185185 } )
186186 onCleanup ( unsub )
187187
188+ const update = ( client : ReturnType < typeof useSDK > [ "client" ] , pty : Partial < LocalPTY > & { id : string } ) => {
189+ const index = store . all . findIndex ( ( x ) => x . id === pty . id )
190+ const previous = index >= 0 ? store . all [ index ] : undefined
191+ if ( index >= 0 ) {
192+ setStore ( "all" , index , ( item ) => ( { ...item , ...pty } ) )
193+ }
194+ client . pty
195+ . update ( {
196+ ptyID : pty . id ,
197+ title : pty . title ,
198+ size : pty . cols && pty . rows ? { rows : pty . rows , cols : pty . cols } : undefined ,
199+ } )
200+ . catch ( ( error : unknown ) => {
201+ if ( previous ) {
202+ const currentIndex = store . all . findIndex ( ( item ) => item . id === pty . id )
203+ if ( currentIndex >= 0 ) setStore ( "all" , currentIndex , previous )
204+ }
205+ console . error ( "Failed to update terminal" , error )
206+ } )
207+ }
208+
209+ const clone = async ( client : ReturnType < typeof useSDK > [ "client" ] , id : string ) => {
210+ const index = store . all . findIndex ( ( x ) => x . id === id )
211+ const pty = store . all [ index ]
212+ if ( ! pty ) return
213+ const next = await client . pty
214+ . create ( {
215+ title : pty . title ,
216+ } )
217+ . catch ( ( error : unknown ) => {
218+ console . error ( "Failed to clone terminal" , error )
219+ return undefined
220+ } )
221+ if ( ! next ?. data ) return
222+
223+ const active = store . active === pty . id
224+
225+ batch ( ( ) => {
226+ setStore ( "all" , index , {
227+ id : next . data . id ,
228+ title : next . data . title ?? pty . title ,
229+ titleNumber : pty . titleNumber ,
230+ buffer : undefined ,
231+ cursor : undefined ,
232+ scrollY : undefined ,
233+ rows : undefined ,
234+ cols : undefined ,
235+ } )
236+ if ( active ) {
237+ setStore ( "active" , next . data . id )
238+ }
239+ } )
240+ }
241+
188242 return {
189243 ready,
190244 all : createMemo ( ( ) => store . all ) ,
@@ -216,24 +270,7 @@ function createWorkspaceTerminalSession(sdk: ReturnType<typeof useSDK>, dir: str
216270 } )
217271 } ,
218272 update ( pty : Partial < LocalPTY > & { id : string } ) {
219- const index = store . all . findIndex ( ( x ) => x . id === pty . id )
220- const previous = index >= 0 ? store . all [ index ] : undefined
221- if ( index >= 0 ) {
222- setStore ( "all" , index , ( item ) => ( { ...item , ...pty } ) )
223- }
224- sdk . client . pty
225- . update ( {
226- ptyID : pty . id ,
227- title : pty . title ,
228- size : pty . cols && pty . rows ? { rows : pty . rows , cols : pty . cols } : undefined ,
229- } )
230- . catch ( ( error : unknown ) => {
231- if ( previous ) {
232- const currentIndex = store . all . findIndex ( ( item ) => item . id === pty . id )
233- if ( currentIndex >= 0 ) setStore ( "all" , currentIndex , previous )
234- }
235- console . error ( "Failed to update terminal" , error )
236- } )
273+ update ( sdk . client , pty )
237274 } ,
238275 trim ( id : string ) {
239276 const index = store . all . findIndex ( ( x ) => x . id === id )
@@ -248,37 +285,23 @@ function createWorkspaceTerminalSession(sdk: ReturnType<typeof useSDK>, dir: str
248285 } )
249286 } ,
250287 async clone ( id : string ) {
251- const index = store . all . findIndex ( ( x ) => x . id === id )
252- const pty = store . all [ index ]
253- if ( ! pty ) return
254- const clone = await sdk . client . pty
255- . create ( {
256- title : pty . title ,
257- } )
258- . catch ( ( error : unknown ) => {
259- console . error ( "Failed to clone terminal" , error )
260- return undefined
261- } )
262- if ( ! clone ?. data ) return
263-
264- const active = store . active === pty . id
265-
266- batch ( ( ) => {
267- setStore ( "all" , index , {
268- id : clone . data . id ,
269- title : clone . data . title ?? pty . title ,
270- titleNumber : pty . titleNumber ,
271- // New PTY process, so start clean.
272- buffer : undefined ,
273- cursor : undefined ,
274- scrollY : undefined ,
275- rows : undefined ,
276- cols : undefined ,
277- } )
278- if ( active ) {
279- setStore ( "active" , clone . data . id )
280- }
281- } )
288+ await clone ( sdk . client , id )
289+ } ,
290+ bind ( ) {
291+ const client = sdk . client
292+ return {
293+ trim ( id : string ) {
294+ const index = store . all . findIndex ( ( x ) => x . id === id )
295+ if ( index === - 1 ) return
296+ setStore ( "all" , index , ( pty ) => trimTerminal ( pty ) )
297+ } ,
298+ update ( pty : Partial < LocalPTY > & { id : string } ) {
299+ update ( client , pty )
300+ } ,
301+ async clone ( id : string ) {
302+ await clone ( client , id )
303+ } ,
304+ }
282305 } ,
283306 open ( id : string ) {
284307 setStore ( "active" , id )
@@ -403,6 +426,7 @@ export const { use: useTerminal, provider: TerminalProvider } = createSimpleCont
403426 trim : ( id : string ) => workspace ( ) . trim ( id ) ,
404427 trimAll : ( ) => workspace ( ) . trimAll ( ) ,
405428 clone : ( id : string ) => workspace ( ) . clone ( id ) ,
429+ bind : ( ) => workspace ( ) ,
406430 open : ( id : string ) => workspace ( ) . open ( id ) ,
407431 close : ( id : string ) => workspace ( ) . close ( id ) ,
408432 move : ( id : string , to : number ) => workspace ( ) . move ( id , to ) ,
0 commit comments