@@ -279,6 +279,7 @@ interface TerminalStartInput {
279279 readonly cols : number ;
280280 readonly rows : number ;
281281 readonly initialCommand ?: string ;
282+ readonly initialCommandShellHistory ?: "preserve" | "suppress" ;
282283}
283284
284285interface TerminalSessionState {
@@ -608,18 +609,34 @@ function formatShellCandidate(candidate: ShellCandidate): string {
608609}
609610
610611function quotePosixShellArgument ( value : string ) : string {
611- return `'${ value . replaceAll ( "'" , `'\\''` ) } '` ;
612+ return `'${ value . replaceAll ( "'" , "'\"'\"'" ) } '` ;
612613}
613614
614615function posixShellCandidateWithInitialCommand (
615616 candidate : ShellCandidate ,
616617 initialCommand : string ,
618+ shellHistory : "preserve" | "suppress" | undefined ,
617619) : ShellCandidate {
618620 const baseArgs = candidate . args ?? [ ] ;
619621 const reenterShell = [ candidate . shell , ...baseArgs ] . map ( quotePosixShellArgument ) . join ( " " ) ;
622+ const shellName = basenameForPlatform ( candidate . shell , "linux" ) . toLowerCase ( ) ;
623+ const quotedCommand = quotePosixShellArgument ( initialCommand ) ;
624+ const recordHistory =
625+ shellHistory !== "preserve"
626+ ? ""
627+ : shellName === "bash"
628+ ? `history -s -- ${ quotedCommand } ; history -a\n`
629+ : shellName === "zsh"
630+ ? `print -s -- ${ quotedCommand } ; fc -AI\n`
631+ : "" ;
620632 return {
621633 shell : candidate . shell ,
622- args : [ ...baseArgs , "-i" , "-c" , `trap ':' INT\n${ initialCommand } \nexec ${ reenterShell } ` ] ,
634+ args : [
635+ ...baseArgs ,
636+ "-i" ,
637+ "-c" ,
638+ `trap ':' INT\n${ recordHistory } ${ initialCommand } \nexec ${ reenterShell } ` ,
639+ ] ,
623640 } ;
624641}
625642
@@ -2495,7 +2512,11 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
24952512 ) {
24962513 initialCommandHandled = true ;
24972514 shellCandidates = resolvedShellCandidates . map ( ( candidate ) =>
2498- posixShellCandidateWithInitialCommand ( candidate , initialCommand ) ,
2515+ posixShellCandidateWithInitialCommand (
2516+ candidate ,
2517+ initialCommand ,
2518+ input . initialCommandShellHistory ,
2519+ ) ,
24992520 ) ;
25002521 }
25012522 const terminalEnv = createTerminalSpawnEnv (
@@ -2953,6 +2974,9 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
29532974 cols,
29542975 rows,
29552976 ...( input . initialCommand ? { initialCommand : input . initialCommand } : { } ) ,
2977+ ...( input . initialCommandShellHistory
2978+ ? { initialCommandShellHistory : input . initialCommandShellHistory }
2979+ : { } ) ,
29562980 } ,
29572981 "started" ,
29582982 ) ;
@@ -3004,6 +3028,9 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
30043028 cols : targetCols ,
30053029 rows : targetRows ,
30063030 ...( input . initialCommand ? { initialCommand : input . initialCommand } : { } ) ,
3031+ ...( input . initialCommandShellHistory
3032+ ? { initialCommandShellHistory : input . initialCommandShellHistory }
3033+ : { } ) ,
30073034 } ,
30083035 "started" ,
30093036 ) ;
0 commit comments