@@ -43,10 +43,14 @@ type ClawBannerOptions = {
4343 env ?: NodeJS . ProcessEnv ;
4444 /** Injectable randomness for the animation garnish (tests pin it). */
4545 rng ?: ( ) => number ;
46+ /** Ends the animation on its static frame when parallel startup work settles. */
47+ settleWhen ?: PromiseLike < unknown > ;
4648 sleep ?: ( ms : number ) => Promise < void > ;
4749 write ?: ( chunk : string ) => void ;
4850} ;
4951
52+ export type ClawBannerResult = "static" | "completed" | "settled" ;
53+
5054type CellTint = ( col : number ) => ( text : string ) => string ;
5155
5256const identityTint : ( text : string ) => string = ( text ) => text ;
@@ -101,10 +105,30 @@ const defaultSleep = (ms: number) =>
101105// canned; every sequence ends on the exact static banner.
102106async function animateBanner ( opts : {
103107 rng : ( ) => number ;
108+ settleWhen ?: PromiseLike < unknown > ;
104109 sleep : ( ms : number ) => Promise < void > ;
105110 write : ( chunk : string ) => void ;
106- } ) : Promise < void > {
107- const { rng, sleep, write } = opts ;
111+ } ) : Promise < Exclude < ClawBannerResult , "static" > > {
112+ const { rng, settleWhen, sleep, write } = opts ;
113+ let settleRequested = false ;
114+ const settleSignal = settleWhen
115+ ? Promise . resolve ( settleWhen ) . then (
116+ ( ) => {
117+ settleRequested = true ;
118+ } ,
119+ ( ) => {
120+ settleRequested = true ;
121+ } ,
122+ )
123+ : null ;
124+ const pause = async ( ms : number ) : Promise < boolean > => {
125+ if ( ! settleSignal ) {
126+ await sleep ( ms ) ;
127+ return true ;
128+ }
129+ await Promise . race ( [ sleep ( ms ) , settleSignal ] ) ;
130+ return ! settleRequested ;
131+ } ;
108132 let drewFrame = false ;
109133 const draw = ( lines : string [ ] ) => {
110134 const prefix = drewFrame ? `\x1b[${ ROWS } F` : "" ;
@@ -139,7 +163,9 @@ async function animateBanner(opts: {
139163 wordmarkTint : tintAt ( identityTint ) ,
140164 } ) ,
141165 ) ;
142- await sleep ( 45 ) ;
166+ if ( ! ( await pause ( 45 ) ) ) {
167+ return "settled" ;
168+ }
143169 }
144170 // Shimmer: a bright band sweeps the wordmark; rarely it runs twice.
145171 const shimmerPasses = rng ( ) < 0.2 ? 2 : 1 ;
@@ -148,22 +174,37 @@ async function animateBanner(opts: {
148174 const band : CellTint = ( col ) =>
149175 col >= x && col < x + 6 ? theme . accentBright : identityTint ;
150176 draw ( composeFrame ( { wordmarkTint : band } ) ) ;
151- await sleep ( 40 ) ;
177+ if ( ! ( await pause ( 40 ) ) ) {
178+ return "settled" ;
179+ }
152180 }
153181 }
154182 // Snip: claws open and close once, sometimes twice.
155183 const snips = rng ( ) < 0.4 ? 2 : 1 ;
156184 for ( let snip = 0 ; snip < snips ; snip ++ ) {
157185 draw ( composeFrame ( { mascotRows : [ ...MASCOT_OPEN_ROWS , ...MASCOT_ART . slice ( 2 ) ] } ) ) ;
158- await sleep ( 95 ) ;
186+ if ( ! ( await pause ( 95 ) ) ) {
187+ return "settled" ;
188+ }
159189 draw ( staticBannerLines ( ) ) ;
160- await sleep ( 115 ) ;
190+ if ( ! ( await pause ( 115 ) ) ) {
191+ return "settled" ;
192+ }
161193 }
162194 draw ( staticBannerLines ( ) ) ;
195+ return "completed" ;
163196 } finally {
164- process . off ( "SIGINT" , onSigint ) ;
165- process . off ( "SIGTERM" , onSigterm ) ;
166- write ( "\x1b[?25h" ) ;
197+ try {
198+ // Parallel work owns startup latency; leave a complete banner instead of
199+ // an interrupted frame before its logs or errors take over the terminal.
200+ if ( settleRequested && drewFrame ) {
201+ draw ( staticBannerLines ( ) ) ;
202+ }
203+ } finally {
204+ process . off ( "SIGINT" , onSigint ) ;
205+ process . off ( "SIGTERM" , onSigterm ) ;
206+ write ( "\x1b[?25h" ) ;
207+ }
167208 }
168209}
169210
@@ -174,11 +215,11 @@ async function animateBanner(opts: {
174215export async function printClawBanner (
175216 runtime : RuntimeEnv ,
176217 options : ClawBannerOptions = { } ,
177- ) : Promise < void > {
218+ ) : Promise < ClawBannerResult > {
178219 const columns = options . columns ?? process . stdout . columns ?? 80 ;
179220 if ( columns < BANNER_WIDTH ) {
180221 runtime . log ( `${ plainTitleLine ( ) } \n` ) ;
181- return ;
222+ return "static" ;
182223 }
183224 const env = options . env ?? process . env ;
184225 const animate =
@@ -188,12 +229,14 @@ export async function printClawBanner(
188229 ! env . VITEST ;
189230 if ( ! animate ) {
190231 runtime . log ( `${ staticBannerLines ( ) . join ( "\n" ) } \n` ) ;
191- return ;
232+ return "static" ;
192233 }
193- await animateBanner ( {
234+ const result = await animateBanner ( {
194235 rng : options . rng ?? Math . random ,
236+ settleWhen : options . settleWhen ,
195237 sleep : options . sleep ?? defaultSleep ,
196238 write : options . write ?? ( ( chunk ) => process . stdout . write ( chunk ) ) ,
197239 } ) ;
198240 ( options . write ?? ( ( chunk : string ) => process . stdout . write ( chunk ) ) ) ( "\n" ) ;
241+ return result ;
199242}
0 commit comments