2020-09-12 20:38:55 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
2023-06-21 18:48:17 +12:00
|
|
|
* Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
|
|
|
|
* Copyright (c) 2023, Shannon Booth <shannon.ml.booth@gmail.com>
|
2020-09-12 20:38:55 +03:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-09-12 20:38:55 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Format.h"
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2023-06-21 18:48:17 +12:00
|
|
|
#include <AK/Stream.h>
|
2020-09-12 20:38:55 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2021-11-10 11:05:21 +01:00
|
|
|
#include <AK/Vector.h>
|
2020-09-12 20:38:55 +03:00
|
|
|
|
|
|
|
namespace Diff {
|
2022-12-18 19:52:25 +01:00
|
|
|
DeprecatedString generate_only_additions(StringView text)
|
2020-09-12 20:38:55 +03:00
|
|
|
{
|
2022-12-18 19:52:25 +01:00
|
|
|
auto lines = text.split_view('\n', SplitBehavior::KeepEmpty);
|
2020-09-12 20:38:55 +03:00
|
|
|
StringBuilder builder;
|
2021-05-07 11:58:21 +02:00
|
|
|
builder.appendff("@@ -0,0 +1,{} @@\n", lines.size());
|
2022-04-01 20:58:27 +03:00
|
|
|
for (auto const& line : lines) {
|
2021-05-07 11:58:21 +02:00
|
|
|
builder.appendff("+{}\n", line);
|
2020-09-12 20:38:55 +03:00
|
|
|
}
|
2022-12-06 01:12:49 +00:00
|
|
|
return builder.to_deprecated_string();
|
2020-09-12 20:38:55 +03:00
|
|
|
}
|
2023-06-21 18:48:17 +12:00
|
|
|
|
|
|
|
ErrorOr<void> write_normal(Hunk const& hunk, Stream& stream, ColorOutput color_output)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
|
|
|
// Source line(s)
|
|
|
|
TRY(stream.write_formatted("{}", original_start));
|
|
|
|
|
|
|
|
if (num_removed > 1)
|
|
|
|
TRY(stream.write_formatted(",{}", original_start + num_removed - 1));
|
|
|
|
|
|
|
|
// Action
|
|
|
|
if (num_added > 0 && num_removed > 0)
|
|
|
|
TRY(stream.write_formatted("c"));
|
|
|
|
else if (num_added > 0)
|
|
|
|
TRY(stream.write_formatted("a"));
|
|
|
|
else
|
|
|
|
TRY(stream.write_formatted("d"));
|
|
|
|
|
|
|
|
// Target line(s)
|
|
|
|
TRY(stream.write_formatted("{}", target_start));
|
|
|
|
if (num_added > 1)
|
|
|
|
TRY(stream.write_formatted(",{}", target_start + num_added - 1));
|
|
|
|
|
|
|
|
TRY(stream.write_formatted("\n"));
|
|
|
|
|
|
|
|
for (auto const& line : hunk.removed_lines) {
|
|
|
|
if (color_output == ColorOutput::Yes)
|
|
|
|
TRY(stream.write_formatted("\033[31;1m< {}\033[0m\n", line));
|
|
|
|
else
|
|
|
|
TRY(stream.write_formatted("< {}\n", line));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_added > 0 && num_removed > 0)
|
|
|
|
TRY(stream.write_formatted("---\n"));
|
|
|
|
|
|
|
|
for (auto const& line : hunk.added_lines) {
|
|
|
|
if (color_output == ColorOutput::Yes)
|
|
|
|
TRY(stream.write_formatted("\033[32;1m> {}\033[0m\n", line));
|
|
|
|
else
|
|
|
|
TRY(stream.write_formatted("> {}\n", line));
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|