Skip to content
This repository has been archived by the owner on Dec 29, 2021. It is now read-only.

Commit

Permalink
fix panic on diff when addind new first line
Browse files Browse the repository at this point in the history
  • Loading branch information
livioribeiro committed Jun 6, 2018
1 parent 973ebcf commit 0c67020
Showing 1 changed file with 38 additions and 20 deletions.
58 changes: 38 additions & 20 deletions src/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,33 @@ pub fn render(&Changeset { ref diffs, .. }: &Changeset) -> Result<String, fmtErr
writeln!(t, "{}", format!("-{}", x).red())?;
}
Difference::Add(ref x) => {
match diffs[i - 1] {
Difference::Rem(ref y) => {
write!(t, "{}", "+".green())?;
let Changeset { diffs, .. } = Changeset::new(y, x, " ");
for c in diffs {
match c {
Difference::Same(ref z) if !z.is_empty() => {
write!(t, "{}", z.green())?;
write!(t, " ")?;
if i == 0 {
writeln!(t, "{}", format!("+{}", x).green())?;
} else {
match diffs[i - 1] {
Difference::Rem(ref y) => {
write!(t, "{}", "+".green())?;
let Changeset { diffs, .. } = Changeset::new(y, x, " ");
for c in diffs {
match c {
Difference::Same(ref z) if !z.is_empty() => {
write!(t, "{}", z.green())?;
write!(t, " ")?;
}
Difference::Add(ref z) if !z.is_empty() => {
write!(t, "{}", z.green().reverse())?;
write!(t, " ")?;
}
_ => (),
}
Difference::Add(ref z) if !z.is_empty() => {
write!(t, "{}", z.green().reverse())?;
write!(t, " ")?;
}
_ => (),
}
writeln!(t, "")?;
}
_ => {
writeln!(t, "{}", format!("+{}", x).green().dimmed())?;
}
writeln!(t, "")?;
}
_ => {
writeln!(t, "{}", format!("+{}", x).green().dimmed())?;
}
};
};
}
}
}
}
Expand Down Expand Up @@ -84,4 +88,18 @@ ullamco laboris nisi ut aliquip ex ea commodo consequat.",
commodo consequat.\n"
);
}

#[test]
fn added_first_line_diff() {
let diff = Changeset::new(
"Line 1\nLine 2\nLine 3",
"Line 0\nLine 1\nLine 2\nLine 3",
"\n",
);
println!("{}", render(&diff).unwrap());
assert_eq!(
render(&diff).unwrap(),
"\u{1b}[32m+Line 0\u{1b}[0m\n Line 1\nLine 2\nLine 3\n"
);
}
}

0 comments on commit 0c67020

Please sign in to comment.