mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 23:50:19 +00:00
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.
This commit is contained in:
parent
1a7136b37a
commit
4173f2ffad
Notes:
sideshowbarker
2024-07-18 03:30:11 +09:00
Author: https://github.com/mustafaquraish Commit: https://github.com/SerenityOS/serenity/commit/4173f2ffad7 Pull-request: https://github.com/SerenityOS/serenity/pull/10096 Reviewed-by: https://github.com/BenWiederhake
1 changed files with 27 additions and 4 deletions
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue