@@ -81,21 +81,24 @@ function printAnnotation({
81
81
return `${ aColor ( a ) } \n${ bColor ( b ) } \n\n`
82
82
}
83
83
84
- export function printDiffLines ( diffs : Array < Diff > , options : DiffOptionsNormalized ) : string {
84
+ export function printDiffLines ( diffs : Array < Diff > , truncated : boolean , options : DiffOptionsNormalized ) : string {
85
85
return printAnnotation ( options , countChanges ( diffs ) )
86
- + ( options . expand
87
- ? joinAlignedDiffsExpand ( diffs , options )
88
- : joinAlignedDiffsNoExpand ( diffs , options ) )
86
+ + ( options . expand ? joinAlignedDiffsExpand ( diffs , options ) : joinAlignedDiffsNoExpand ( diffs , options ) )
87
+ + ( truncated ? options . truncateAnnotationColor ( `\n${ options . truncateAnnotation } ` ) : '' )
89
88
}
90
89
91
90
// Compare two arrays of strings line-by-line. Format as comparison lines.
92
91
export function diffLinesUnified ( aLines : Array < string > , bLines : Array < string > , options ?: DiffOptions ) : string {
92
+ const normalizedOptions = normalizeDiffOptions ( options )
93
+ const [ diffs , truncated ] = diffLinesRaw (
94
+ isEmptyString ( aLines ) ? [ ] : aLines ,
95
+ isEmptyString ( bLines ) ? [ ] : bLines ,
96
+ normalizedOptions ,
97
+ )
93
98
return printDiffLines (
94
- diffLinesRaw (
95
- isEmptyString ( aLines ) ? [ ] : aLines ,
96
- isEmptyString ( bLines ) ? [ ] : bLines ,
97
- ) ,
98
- normalizeDiffOptions ( options ) ,
99
+ diffs ,
100
+ truncated ,
101
+ normalizedOptions ,
99
102
)
100
103
}
101
104
@@ -120,7 +123,7 @@ export function diffLinesUnified2(aLinesDisplay: Array<string>, bLinesDisplay: A
120
123
return diffLinesUnified ( aLinesDisplay , bLinesDisplay , options )
121
124
}
122
125
123
- const diffs = diffLinesRaw ( aLinesCompare , bLinesCompare )
126
+ const [ diffs , truncated ] = diffLinesRaw ( aLinesCompare , bLinesCompare , options )
124
127
125
128
// Replace comparison lines with displayable lines.
126
129
let aIndex = 0
@@ -144,13 +147,16 @@ export function diffLinesUnified2(aLinesDisplay: Array<string>, bLinesDisplay: A
144
147
}
145
148
} )
146
149
147
- return printDiffLines ( diffs , normalizeDiffOptions ( options ) )
150
+ return printDiffLines ( diffs , truncated , normalizeDiffOptions ( options ) )
148
151
}
149
152
150
153
// Compare two arrays of strings line-by-line.
151
- export function diffLinesRaw ( aLines : Array < string > , bLines : Array < string > ) : Array < Diff > {
152
- const aLength = aLines . length
153
- const bLength = bLines . length
154
+ export function diffLinesRaw ( aLines : Array < string > , bLines : Array < string > , options ?: DiffOptions ) : [ Array < Diff > , boolean ] {
155
+ const truncate = options ?. truncateThreshold ?? false
156
+ const truncateThreshold = Math . max ( Math . floor ( options ?. truncateThreshold ?? 0 ) , 0 )
157
+ const aLength = truncate ? Math . min ( aLines . length , truncateThreshold ) : aLines . length
158
+ const bLength = truncate ? Math . min ( bLines . length , truncateThreshold ) : bLines . length
159
+ const truncated = aLength !== aLines . length || bLength !== bLines . length
154
160
155
161
const isCommon = ( aIndex : number , bIndex : number ) => aLines [ aIndex ] === bLines [ bIndex ]
156
162
@@ -185,5 +191,5 @@ export function diffLinesRaw(aLines: Array<string>, bLines: Array<string>): Arra
185
191
for ( ; bIndex !== bLength ; bIndex += 1 )
186
192
diffs . push ( new Diff ( DIFF_INSERT , bLines [ bIndex ] ) )
187
193
188
- return diffs
194
+ return [ diffs , truncated ]
189
195
}
0 commit comments