From 4173f2ffad7d67f52068a16e1f30348a86725ddf Mon Sep 17 00:00:00 2001 From: Mustafa Quraish Date: Fri, 17 Sep 2021 14:07:34 -0400 Subject: [PATCH] diff: Show start/end of line ranges in source/target files This behaves very much like the regular diff command, showing the start lines and ranges of additions/changes/deletions in both the source and target files. --- Userland/Utilities/diff.cpp | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/Userland/Utilities/diff.cpp b/Userland/Utilities/diff.cpp index d6fa356bf1..2ea0d7b8ee 100644 --- a/Userland/Utilities/diff.cpp +++ b/Userland/Utilities/diff.cpp @@ -37,11 +37,34 @@ int main(int argc, char** argv) auto hunks = Diff::from_text(file1->read_all(), file2->read_all()); for (const auto& hunk : hunks) { - outln("Hunk: {}, {}", hunk.original_start_line, hunk.target_start_line); - for (const auto& line : hunk.removed_lines) { + auto original_start = hunk.original_start_line; + auto target_start = hunk.target_start_line; + auto num_added = hunk.added_lines.size(); + auto num_removed = hunk.removed_lines.size(); + + StringBuilder sb; + // Source line(s) + sb.appendff("{}", original_start); + if (num_removed > 1) + sb.appendff(",{}", original_start + num_removed - 1); + + // Action + if (num_added > 0 && num_removed > 0) + sb.append("c"); + else if (num_added > 0) + sb.append("a"); + else + sb.append("d"); + + // Target line(s) + sb.appendff("{}", target_start); + if (num_added > 1) + sb.appendff(",{}", target_start + num_added - 1); + + outln("Hunk: {}", sb.build()); + for (const auto& line : hunk.removed_lines) outln("\033[31;1m< {}\033[0m", line); - } - if (hunk.added_lines.size() > 0 && hunk.removed_lines.size() > 0) + if (num_added > 0 && num_removed > 0) outln("---"); for (const auto& line : hunk.added_lines) outln("\033[32;1m> {}\033[0m", line);