@@ -4,7 +4,7 @@ const stripAnsi = require(`strip-ansi`)
44
55const terminalWidth = ( process . stdout && process . stdout . columns ) ? process . stdout . columns : 80
66
7- const colwidth = Math . floor ( ( terminalWidth - 2 ) / 2 )
7+ const colwidth = Math . floor ( ( terminalWidth ) / 2 )
88
99function leftPad ( str , width ) {
1010 let s = width - stripAnsi ( str ) . length
@@ -63,29 +63,6 @@ module.exports = {
6363
6464 const splitLines = str => str . match ( / \n ( [ ^ \n ] * ) / g) || [ ]
6565
66- let bufferLength = 4
67- let leftTruncateBuffer = new Array ( bufferLength )
68- let rightTruncateBuffer = new Array ( bufferLength )
69-
70- function pushBufferorLines ( line , lineBuffer , lines ) {
71-
72- if ( lineBuffer . length < bufferLength ) {
73- lines . push ( line )
74- }
75- lineBuffer . push ( line )
76-
77- if ( lineBuffer . length > bufferLength * 2 ) {
78- lineBuffer . splice ( 0 , 1 )
79- }
80- }
81-
82- function emptyBuffer ( lineBuffer ) {
83- let ret = lineBuffer . splice ( bufferLength , bufferLength )
84- lineBuffer . splice ( 0 )
85- return ret
86- }
87-
88-
8966 diff [ diffType ] ( one , two ) . forEach ( e => {
9067 if ( ! e . added && ! e . removed ) {
9168 alignLines ( )
@@ -100,50 +77,48 @@ module.exports = {
10077
10178 for ( let line of splitLines ( e . value ) ) {
10279 line = line . replace ( '\n' , '' )
103- pushBufferorLines ( { line, number : ++ leftLineNumber } , leftTruncateBuffer , left )
104- pushBufferorLines ( { line, number : ++ rightLineNumber } , rightTruncateBuffer , right )
80+ left . push ( { line, number : ++ leftLineNumber } )
81+ right . push ( { line, number : ++ rightLineNumber } )
10582 }
10683
10784 }
10885 if ( e . removed ) {
10986
110- let bufferResults = emptyBuffer ( leftTruncateBuffer )
111- if ( bufferResults . length > 0 ) left . push ( { endTruncation : true } )
112- left = left . concat ( bufferResults )
87+ let color = diffType === 'diffChars' ? chalk . bold . red : chalk . bold . white
11388
11489 for ( let chars of e . value . match ( / ^ ( [ ^ \n ] * ) / g) ) {
11590 let lastLine = left [ left . length - 1 ]
116- lastLine . line = ( lastLine . line ? lastLine . line : '' ) + chalk . bold . red ( chars )
91+ lastLine . line = ( lastLine . line ? lastLine . line : '' ) + color ( chars )
92+ lastLine . changed = true
11793 }
11894
11995 for ( let line of splitLines ( e . value ) ) {
120- line = chalk . bold . red ( line . replace ( '\n' , '' ) )
121- left . push ( { line, number : ++ leftLineNumber } )
96+ line = color ( line . replace ( '\n' , '' ) )
97+ left . push ( { line, number : ++ leftLineNumber , changed : ! ! line } )
12298 }
12399
124100 }
125101 if ( e . added ) {
126102
127- let bufferResults = emptyBuffer ( rightTruncateBuffer )
128- if ( bufferResults . length > 0 ) right . push ( { endTruncation : true } )
129- right = right . concat ( bufferResults )
103+ let color = diffType === 'diffChars' ? chalk . bold . green : chalk . bold . white
130104
131105 for ( let chars of e . value . match ( / ^ ( [ ^ \n ] * ) / g) ) {
132106 let lastLine = right [ right . length - 1 ]
133- lastLine . line = ( lastLine . line ? lastLine . line : '' ) + chalk . bold . green ( chars )
107+ lastLine . line = ( lastLine . line ? lastLine . line : '' ) + color ( chars )
108+ lastLine . changed = true
134109 }
135110
136111 for ( let line of splitLines ( e . value ) ) {
137- line = chalk . bold . green ( line . replace ( '\n' , '' ) )
138- right . push ( { line, number : ++ rightLineNumber } )
112+ line = color ( line . replace ( '\n' , '' ) )
113+ right . push ( { line, number : ++ rightLineNumber , changed : ! ! line } )
139114 }
140115 }
141116 } )
142117
143118 alignLines ( )
144119
145120 // draw
146- let output = ``
121+ let output = [ ]
147122
148123 let widestLineNumberLeft = String ( leftLineNumber ) . length
149124 let widestLineNumberRight = String ( rightLineNumber ) . length
@@ -153,38 +128,71 @@ module.exports = {
153128 chalk . bgRgb ( 15 , 15 , 15 ) ,
154129 ]
155130
131+ let bufferLength = 4
132+ let truncateBuffer = new Array ( bufferLength )
133+
134+ function pushBufferorLines ( line , lineBuffer , lines ) {
135+
136+ if ( lineBuffer . length < bufferLength ) {
137+ lines . push ( line )
138+ }
139+ lineBuffer . push ( line )
140+
141+ if ( lineBuffer . length > bufferLength * 2 ) {
142+ lineBuffer . splice ( 0 , 1 )
143+ }
144+ }
145+
146+ function emptyBuffer ( lineBuffer ) {
147+ let ret = lineBuffer . splice ( bufferLength , bufferLength )
148+ lineBuffer . splice ( 0 )
149+ return ret
150+ }
151+
152+
156153 for ( let i = 0 ; i < left . length ; i ++ ) {
157154 let leftString , rightString
158155
159- // truncation?
160- if ( left [ i ] . endTruncation ) {
161- leftString = backgrounds [ 2 ] ( fit ( ' ... ' , colwidth ) )
162- }
163156 // padding?
164- else if ( ! ( left [ i ] . number && left [ i ] . number > 0 ) ) {
157+ if ( ! ( left [ i ] . number && left [ i ] . number > 0 ) ) {
165158 leftString = backgrounds [ 2 ] ( fit ( left [ i ] . line ? left [ i ] . line : '' , colwidth ) )
166159 }
167160 else {
168- let leftLineNumber = chalk . grey ( leftPad ( String ( left [ i ] . number ) , widestLineNumberLeft ) )
169- leftString = backgrounds [ i % 2 ] ( fit ( backgrounds [ 2 ] ( ' ' + leftLineNumber + ' ' ) + left [ i ] . line , colwidth ) )
161+ let leftLineNumber = leftPad ( String ( left [ i ] . number ) , widestLineNumberLeft )
162+ if ( left [ i ] . changed ) {
163+ leftString = chalk . bgRgb ( 70 , 0 , 0 ) ( fit ( chalk . bgRgb ( 50 , 0 , 0 ) ( ' ' + chalk . white ( leftLineNumber ) + ' ' ) + left [ i ] . line , colwidth ) )
164+ } else {
165+ leftString = backgrounds [ 0 ] ( fit ( backgrounds [ 2 ] ( ' ' + chalk . grey ( leftLineNumber ) + ' ' ) + left [ i ] . line , colwidth ) )
166+ }
170167 }
171168
172- // truncation?
173- if ( right [ i ] . endTruncation ) {
174- rightString = backgrounds [ 2 ] ( fit ( ' ... ' , colwidth ) )
175- }
176169 // padding?
177- else if ( ! ( right [ i ] . number && right [ i ] . number > 0 ) ) {
170+ if ( ! ( right [ i ] . number && right [ i ] . number > 0 ) ) {
178171 rightString = backgrounds [ 2 ] ( fit ( right [ i ] . line ? right [ i ] . line : '' , colwidth ) )
179172 }
180173 else {
181174 let rightLineNumber = chalk . grey ( leftPad ( String ( right [ i ] . number ) , widestLineNumberRight ) )
182- rightString = backgrounds [ i % 2 ] ( fit ( backgrounds [ 2 ] ( ' ' + rightLineNumber + ' ' ) + right [ i ] . line , colwidth ) )
175+ if ( right [ i ] . changed ) {
176+ rightString = chalk . bgRgb ( 0 , 70 , 0 ) ( fit ( chalk . bgRgb ( 0 , 50 , 0 ) ( ' ' + chalk . white ( rightLineNumber ) + ' ' ) + right [ i ] . line , colwidth ) )
177+ } else {
178+ rightString = backgrounds [ 0 ] ( fit ( backgrounds [ 2 ] ( ' ' + chalk . grey ( rightLineNumber ) + ' ' ) + right [ i ] . line , colwidth ) )
179+ }
180+ }
181+
182+ let combined = leftString + rightString
183+
184+ if ( ! left [ i ] . changed && ! right [ i ] . changed ) {
185+ pushBufferorLines ( combined , truncateBuffer , output )
186+ } else {
187+ let bufferResults = emptyBuffer ( truncateBuffer )
188+ if ( bufferResults . length > 0 ) output . push ( ' ... ' )
189+ output = output . concat ( bufferResults )
190+
191+ output . push ( combined )
183192 }
184193
185- output += leftString + ' ' + rightString + '\n'
186194 }
187195
188- return output
196+ return output . join ( '\n' )
189197 } ,
190198}
0 commit comments