@@ -15,29 +15,33 @@ pub fn render(&Changeset { ref diffs, .. }: &Changeset) -> Result<String, fmtErr
15
15
writeln ! ( t, "{}" , format!( "-{}" , x) . red( ) ) ?;
16
16
}
17
17
Difference :: Add ( ref x) => {
18
- match diffs[ i - 1 ] {
19
- Difference :: Rem ( ref y) => {
20
- write ! ( t, "{}" , "+" . green( ) ) ?;
21
- let Changeset { diffs, .. } = Changeset :: new ( y, x, " " ) ;
22
- for c in diffs {
23
- match c {
24
- Difference :: Same ( ref z) if !z. is_empty ( ) => {
25
- write ! ( t, "{}" , z. green( ) ) ?;
26
- write ! ( t, " " ) ?;
18
+ if i == 0 {
19
+ writeln ! ( t, "{}" , format!( "+{}" , x) . green( ) ) ?;
20
+ } else {
21
+ match diffs[ i - 1 ] {
22
+ Difference :: Rem ( ref y) => {
23
+ write ! ( t, "{}" , "+" . green( ) ) ?;
24
+ let Changeset { diffs, .. } = Changeset :: new ( y, x, " " ) ;
25
+ for c in diffs {
26
+ match c {
27
+ Difference :: Same ( ref z) if !z. is_empty ( ) => {
28
+ write ! ( t, "{}" , z. green( ) ) ?;
29
+ write ! ( t, " " ) ?;
30
+ }
31
+ Difference :: Add ( ref z) if !z. is_empty ( ) => {
32
+ write ! ( t, "{}" , z. green( ) . reverse( ) ) ?;
33
+ write ! ( t, " " ) ?;
34
+ }
35
+ _ => ( ) ,
27
36
}
28
- Difference :: Add ( ref z) if !z. is_empty ( ) => {
29
- write ! ( t, "{}" , z. green( ) . reverse( ) ) ?;
30
- write ! ( t, " " ) ?;
31
- }
32
- _ => ( ) ,
33
37
}
38
+ writeln ! ( t, "" ) ?;
39
+ }
40
+ _ => {
41
+ writeln ! ( t, "{}" , format!( "+{}" , x) . green( ) . dimmed( ) ) ?;
34
42
}
35
- writeln ! ( t, "" ) ?;
36
- }
37
- _ => {
38
- writeln ! ( t, "{}" , format!( "+{}" , x) . green( ) . dimmed( ) ) ?;
39
- }
40
- } ;
43
+ } ;
44
+ }
41
45
}
42
46
}
43
47
}
@@ -84,4 +88,18 @@ ullamco laboris nisi ut aliquip ex ea commodo consequat.",
84
88
commodo consequat.\n "
85
89
) ;
86
90
}
91
+
92
+ #[ test]
93
+ fn added_first_line_diff ( ) {
94
+ let diff = Changeset :: new (
95
+ "Line 1\n Line 2\n Line 3" ,
96
+ "Line 0\n Line 1\n Line 2\n Line 3" ,
97
+ "\n " ,
98
+ ) ;
99
+ println ! ( "{}" , render( & diff) . unwrap( ) ) ;
100
+ assert_eq ! (
101
+ render( & diff) . unwrap( ) ,
102
+ "\u{1b} [32m+Line 0\u{1b} [0m\n Line 1\n Line 2\n Line 3\n "
103
+ ) ;
104
+ }
87
105
}
0 commit comments