@@ -213,6 +213,19 @@ fn color_multilines(color: AnsiColors, s: &str) -> String {
213213pub struct ContextConfig < ' a > {
214214 pub context_size : usize ,
215215 pub skipping_marker : & ' a str ,
216+ pub remove_color : Style ,
217+ pub insert_color : Style ,
218+ }
219+
220+ impl < ' a > Default for ContextConfig < ' a > {
221+ fn default ( ) -> Self {
222+ ContextConfig {
223+ context_size : 0 ,
224+ skipping_marker : "" ,
225+ remove_color : Style :: new ( ) . red ( ) . strikethrough ( ) ,
226+ insert_color : Style :: new ( ) . green ( ) ,
227+ }
228+ }
216229}
217230
218231/// Container for line-by-line text diff result. Can be pretty-printed by Display trait.
@@ -414,12 +427,8 @@ impl<'a> LineChangeset<'a> {
414427 table. print ( f)
415428 }
416429
417- fn remove_color ( & self , a : & str ) -> String {
418- a. red ( ) . strikethrough ( ) . to_string ( )
419- }
420-
421- fn insert_color ( & self , a : & str ) -> String {
422- a. green ( ) . to_string ( )
430+ fn apply_style ( & self , a : & str , style : Style ) -> String {
431+ a. style ( style) . to_string ( )
423432 }
424433
425434 /// Returns formatted string with colors
@@ -457,16 +466,17 @@ impl<'a> LineChangeset<'a> {
457466 display_line_numbers : bool ,
458467 prefix_size : usize ,
459468 line_counter : & mut usize ,
469+ remove_style : Style ,
460470 ) -> String {
461471 lines
462472 . iter ( )
463473 . map ( |line| {
464474 let res = if display_line_numbers {
465475 // Pad and align the line number to the right
466476 format ! ( "{:>size$} " , * line_counter, size = prefix_size - 1 )
467- + & self . remove_color ( line)
477+ + & self . apply_style ( line, remove_style )
468478 } else {
469- " " . repeat ( prefix_size) + & self . remove_color ( line)
479+ " " . repeat ( prefix_size) + & self . apply_style ( line, remove_style )
470480 } ;
471481 * line_counter += 1 ;
472482 res
@@ -476,10 +486,10 @@ impl<'a> LineChangeset<'a> {
476486 }
477487
478488 /// Formats lines in DiffOp::Insert
479- fn format_insert ( & self , lines : & [ & str ] , prefix_size : usize ) -> String {
489+ fn format_insert ( & self , lines : & [ & str ] , prefix_size : usize , insert_style : Style ) -> String {
480490 lines
481491 . iter ( )
482- . map ( |line| " " . repeat ( prefix_size) + & self . insert_color ( line) )
492+ . map ( |line| " " . repeat ( prefix_size) + & self . apply_style ( line, insert_style ) )
483493 . reduce ( |acc, line| acc + "\n " + & line)
484494 . unwrap ( )
485495 }
@@ -498,14 +508,20 @@ impl<'a> LineChangeset<'a> {
498508 } else {
499509 0
500510 } ;
501- let skipping_marker_size = if let Some ( ContextConfig {
502- skipping_marker, ..
511+
512+ let ( skipping_marker_size, remove_color, insert_color) = if let Some ( ContextConfig {
513+ skipping_marker,
514+ remove_color,
515+ insert_color,
516+ ..
503517 } ) = context_config
504518 {
505- skipping_marker. len ( )
519+ ( skipping_marker. len ( ) , remove_color , insert_color )
506520 } else {
507- 0
521+ let c = ContextConfig :: default ( ) ;
522+ ( c. skipping_marker . len ( ) , c. remove_color , c. insert_color )
508523 } ;
524+
509525 let prefix_size = max ( line_number_size, skipping_marker_size) + 1 ;
510526
511527 let mut next_line = 1 ;
@@ -520,6 +536,7 @@ impl<'a> LineChangeset<'a> {
520536 Some ( ContextConfig {
521537 context_size,
522538 skipping_marker,
539+ ..
523540 } ) => {
524541 let mut lines = a;
525542 if !at_beginning {
@@ -559,21 +576,25 @@ impl<'a> LineChangeset<'a> {
559576 }
560577 }
561578 } ,
562- basic:: DiffOp :: Insert ( a) => out. push ( self . format_insert ( a, prefix_size) ) ,
579+ basic:: DiffOp :: Insert ( a) => {
580+ out. push ( self . format_insert ( a, prefix_size, insert_color) )
581+ }
563582 basic:: DiffOp :: Remove ( a) => out. push ( self . format_remove (
564583 a,
565584 display_line_numbers,
566585 prefix_size,
567586 & mut next_line,
587+ remove_color,
568588 ) ) ,
569589 basic:: DiffOp :: Replace ( a, b) => {
570590 out. push ( self . format_remove (
571591 a,
572592 display_line_numbers,
573593 prefix_size,
574594 & mut next_line,
595+ remove_color,
575596 ) ) ;
576- out. push ( self . format_insert ( b, prefix_size) ) ;
597+ out. push ( self . format_insert ( b, prefix_size, insert_color ) ) ;
577598 }
578599 }
579600 at_beginning = false ;
@@ -869,6 +890,8 @@ fn test_format_with_context() {
869890 let context = |n| ContextConfig {
870891 context_size : n,
871892 skipping_marker : "..." ,
893+ remove_color : Default :: default ( ) ,
894+ insert_color : Default :: default ( ) ,
872895 } ;
873896 println ! (
874897 "diff_lines:\n {}\n {:?}" ,
0 commit comments