@@ -4,60 +4,18 @@ import { sanitizeTerminalText } from "../../packages/terminal-core/src/safe-text
44import type { SpawnResult } from "./exec-result.js" ;
55import type { runCommandBuffered } from "./exec.js" ;
66
7- function normalizeCommandOutput ( output : string | Buffer ) : string {
8- const visible = stripAnsi ( output . toString ( ) )
7+ export function formatCommandOutput ( output : string | Buffer , maxChars = 800 ) : string {
8+ // CR redraws replace the current frame; trim before making edge tabs visible.
9+ const text = stripAnsi ( output . toString ( ) )
910 . replace ( / \r \n / g, "\n" )
1011 . split ( "\n" )
1112 . map ( ( line ) => line . replace ( / \r + $ / , "" ) . split ( "\r" ) . at ( - 1 ) ?? "" )
1213 . join ( "\n" )
13- . trim ( ) ;
14- return visible
15- . split ( "\n" )
16- . map ( ( line ) => sanitizeTerminalText ( line ) )
17- . join ( "\n" ) ;
18- }
19-
20- function renderCommandOutputTail ( output : string , maxChars : number , markerChars = 0 ) : string {
21- const tail = output . split ( "\n" ) . slice ( - 12 ) . join ( "\n" ) ;
22- if ( tail . length === output . length && tail . length <= maxChars ) {
23- return tail ;
24- }
25- const contentLimit = Math . max ( 0 , Math . floor ( maxChars ) ) - markerChars ;
26- return contentLimit < 0
27- ? ""
28- : `…\n${ contentLimit > 0 ? sliceUtf16Safe ( tail , - contentLimit ) : "" } ` ;
29- }
30-
31- function formatCommandErrorDetail ( stderr : string | Buffer , stdout : string | Buffer ) : string {
32- const streams = [ normalizeCommandOutput ( stderr ) , normalizeCommandOutput ( stdout ) ] ;
33- const visible = streams . filter ( Boolean ) ;
34- if ( visible . length < 2 ) {
35- return visible [ 0 ] ? renderCommandOutputTail ( visible [ 0 ] , 2_000 ) : "" ;
36- }
37-
38- const bodyBudget = 2_000 - "stderr: \nstdout: " . length ;
39- const demands = streams . map ( ( output ) => {
40- const tail = output . split ( "\n" ) . slice ( - 12 ) . join ( "\n" ) ;
41- return tail . length + ( tail . length < output . length ? 2 : 0 ) ;
42- } ) ;
43- let allocations = [ ...demands ] ;
44- if ( demands [ 0 ] ! + demands [ 1 ] ! > bodyBudget ) {
45- allocations = demands . map ( ( demand ) => Math . min ( demand , Math . floor ( bodyBudget / 2 ) ) ) ;
46- const remaining = bodyBudget - allocations [ 0 ] ! - allocations [ 1 ] ! ;
47- const stderrUnmet = demands [ 0 ] ! - allocations [ 0 ] ! ;
48- const stdoutUnmet = demands [ 1 ] ! - allocations [ 1 ] ! ;
49- const index = stderrUnmet >= stdoutUnmet ? 0 : 1 ;
50- allocations [ index ] = allocations [ index ] ! + remaining ;
51- }
52- return `stderr: ${ renderCommandOutputTail (
53- streams [ 0 ] ! ,
54- Math . max ( 0 , Math . floor ( allocations [ 0 ] ! ) ) ,
55- 2 ,
56- ) } \nstdout: ${ renderCommandOutputTail ( streams [ 1 ] ! , Math . max ( 0 , Math . floor ( allocations [ 1 ] ! ) ) , 2 ) } `;
57- }
58-
59- export function formatCommandOutput ( output : string | Buffer , maxChars = 800 ) : string {
60- return renderCommandOutputTail ( normalizeCommandOutput ( output ) , maxChars ) ;
14+ . trim ( )
15+ . replace ( / [ ^ \n ] + / g, sanitizeTerminalText ) ;
16+ const tail = text . split ( "\n" ) . slice ( - 12 ) . join ( "\n" ) ;
17+ const omitted = tail . length < text . length || tail . length > maxChars ;
18+ return `${ omitted ? "…\n" : "" } ${ sliceUtf16Safe ( tail , Math . max ( 0 , tail . length - maxChars ) ) } ` ;
6119}
6220
6321/** Use an operation label, never argv that may contain credentials. */
@@ -67,44 +25,44 @@ export function formatCommandResult(command: string, result: SpawnResult): strin
6725 const signal = result . signal ? `, signal=${ result . signal } ` : "" ;
6826 const killed = result . killed ? ", killed=true" : "" ;
6927 const status = result . code === 0 ? "exited" : "failed" ;
70- const lines = [
28+ return [
7129 `${ label } ${ status } (code=${ result . code } , termination=${ termination } ${ signal } ${ killed } )` ,
72- ] ;
73- for ( const stream of [ "stderr" , "stdout" ] as const ) {
74- const output = formatCommandOutput ( result [ stream ] ) ;
75- if ( output ) {
76- lines . push ( `${ stream } : ${ output } ` ) ;
77- }
78- }
79- return lines . join ( "\n" ) ;
30+ ...( [ "stderr" , "stdout" ] as const ) . flatMap ( ( stream ) => {
31+ const output = formatCommandOutput ( result [ stream ] ) ;
32+ return output ? [ `${ stream } : ${ output } ` ] : [ ] ;
33+ } ) ,
34+ ] . join ( "\n" ) ;
8035}
8136
8237export function createCommandError (
8338 command : string ,
8439 result : SpawnResult | Awaited < ReturnType < typeof runCommandBuffered > > ,
8540 options : { timeoutMs : number } ,
8641) : Error {
87- const detail = formatCommandErrorDetail ( result . stderr , result . stdout ) ;
88- const reasons : string [ ] = [ ] ;
89- if ( result . termination === "timeout" ) {
90- reasons . push ( `timed out after ${ options . timeoutMs / 1000 } seconds` ) ;
91- } else if ( result . termination === "no-output-timeout" ) {
92- reasons . push ( "timed out waiting for output" ) ;
93- } else if (
94- result . termination === "output-limit" ||
95- ( "outputLimitExceeded" in result && result . outputLimitExceeded )
96- ) {
97- reasons . push ( "output limit exceeded" ) ;
98- }
99- if ( result . signal ) {
100- reasons . push ( `signal ${ result . signal } ` ) ;
101- } else if ( result . termination === "signal" && reasons . length === 0 ) {
102- reasons . push ( "terminated" ) ;
103- }
104- if ( reasons . length === 0 && result . code !== null ) {
105- reasons . push ( `exit code ${ result . code } ` ) ;
42+ const stderr = formatCommandOutput ( result . stderr , 2_000 ) ;
43+ const stdout = formatCommandOutput ( result . stdout , 2_000 ) ;
44+ let detail = stderr || stdout ;
45+ if ( stderr && stdout ) {
46+ const budget = 2_000 - "stderr: \nstdout: " . length ;
47+ const first = Math . min ( stderr . length , Math . max ( Math . ceil ( budget / 2 ) , budget - stdout . length ) ) ;
48+ // Pure suffix fitting replaces any earlier marker; normalization runs only once.
49+ const tail = ( output : string , limit : number ) =>
50+ output . length <= limit ? output : `…\n${ sliceUtf16Safe ( output , 2 - limit ) } ` ;
51+ detail = `stderr: ${ tail ( stderr , first ) } \nstdout: ${ tail ( stdout , budget - first ) } ` ;
10652 }
53+ const signal = result . signal ? `signal ${ result . signal } ` : "" ;
54+ const limited =
55+ "outputLimitExceeded" in result && result . outputLimitExceeded ? "output limit exceeded" : "" ;
56+ const exitReason = limited || ( ! signal && result . code !== null ? `exit code ${ result . code } ` : "" ) ;
57+ const primary = {
58+ timeout : `timed out after ${ options . timeoutMs / 1000 } seconds` ,
59+ "no-output-timeout" : "timed out waiting for output" ,
60+ "output-limit" : "output limit exceeded" ,
61+ exit : exitReason ,
62+ error : exitReason ,
63+ signal : limited || ( ! signal ? "terminated" : "" ) ,
64+ } [ result . termination ] ;
65+ const reason = [ primary , signal ] . filter ( Boolean ) . join ( "; " ) ;
10766 const label = truncateUtf16Safe ( stripAnsi ( command ) . replace ( / [ \r \n ] + / g, " " ) , 256 ) ;
108- const reason = reasons . length > 0 ? ` (${ reasons . join ( "; " ) } )` : "" ;
109- return new Error ( `${ label } failed${ reason } ${ detail ? `:\n${ detail } ` : "" } ` ) ;
67+ return new Error ( `${ label } failed${ reason ? ` (${ reason } )` : "" } ${ detail ? `:\n${ detail } ` : "" } ` ) ;
11068}
0 commit comments