From 6f871407030d3167952b8a332da5ed890d9f10c8 Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Wed, 7 Jul 2021 22:19:46 -0700 Subject: [PATCH] Don't crash if file is unchanged Fixes #4 --- CHANGELOG.md | 2 ++ src/main.rs | 16 +++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8141c8fa0..715866da1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ Improved punctuation parsing for OCaml and JS. Fixed an issue where the diff calculated may not be minimal. +Fixed a crash on files with no changes. + ## 0.2 First version using Dijkstra's algorithm for calculating diffs. diff --git a/src/main.rs b/src/main.rs index 8d0d1d5fa..90a8a2ab4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -93,7 +93,18 @@ fn main() { let lhs_matched_lines = matching_lines(&lhs); + let lang_name = match &lang { + Some(lang) => lang.name.clone(), + None => "plain text".to_string(), + }; + println!("{}", style::header(rhs_path, &lang_name)); + let mut groups = visible_groups(&lhs_positions, &rhs_positions); + if groups.is_empty() { + println!("No changes found."); + return; + } + for group in &mut groups { group.pad(3, lhs_src.max_line(), rhs_src.max_line()); } @@ -118,11 +129,6 @@ fn main() { let lhs_colored = apply_colors(&lhs_src, true, &lhs_positions); let rhs_colored = apply_colors(&rhs_src, false, &rhs_positions); - let lang_name = match &lang { - Some(lang) => lang.name.clone(), - None => "plain text".to_string(), - }; - println!("{}", style::header(rhs_path, &lang_name)); print!( "{}", apply_groups(