@@ -226,19 +226,15 @@ impl DiffComponent {
226
226
. bg ( if selected { select_color } else { Color :: Reset } )
227
227
. modifier ( Modifier :: BOLD ) ;
228
228
229
+ let trimmed =
230
+ line. content . trim_matches ( |c| c == '\n' || c == '\r' ) ;
231
+
229
232
let filled = if selected {
230
233
// selected line
231
- format ! (
232
- "{:w$}\n " ,
233
- line. content. trim_matches( '\n' ) ,
234
- w = width as usize
235
- )
236
- } else if line. content . matches ( '\n' ) . count ( ) == 1 {
237
- // regular line, no selection (cheapest)
238
- line. content . clone ( )
234
+ format ! ( "{:w$}\n " , trimmed, w = width as usize )
239
235
} else {
240
236
// weird eof missing eol line
241
- format ! ( "{}\n " , line . content . trim_matches ( '\n' ) )
237
+ format ! ( "{}\n " , trimmed )
242
238
} ;
243
239
//TODO: allow customize tabsize
244
240
let content = Cow :: from ( filled. replace ( "\t " , " " ) ) ;
@@ -371,3 +367,33 @@ impl Component for DiffComponent {
371
367
self . focused = focus
372
368
}
373
369
}
370
+
371
+ #[ cfg( test) ]
372
+ mod tests {
373
+ use super :: * ;
374
+
375
+ #[ test]
376
+ fn test_lineendings ( ) {
377
+ let mut text = Vec :: new ( ) ;
378
+
379
+ DiffComponent :: add_line (
380
+ & mut text,
381
+ 10 ,
382
+ & DiffLine {
383
+ content : String :: from ( "line 1\r \n " ) ,
384
+ line_type : DiffLineType :: None ,
385
+ } ,
386
+ false ,
387
+ false ,
388
+ false ,
389
+ ) ;
390
+
391
+ assert_eq ! ( text. len( ) , 2 ) ;
392
+
393
+ if let Text :: Styled ( c, _) = & text[ 1 ] {
394
+ assert_eq ! ( c, "line 1\n " ) ;
395
+ } else {
396
+ panic ! ( "err" )
397
+ }
398
+ }
399
+ }
0 commit comments