2020-09-12 20:38:55 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
|
2023-06-21 09:48:17 +03: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 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-09-12 20:38:55 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Format.h"
|
2022-12-04 21:02:33 +03:00
|
|
|
#include <AK/DeprecatedString.h>
|
2023-06-21 09:48:17 +03:00
|
|
|
#include <AK/Stream.h>
|
2020-09-12 20:38:55 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2021-11-10 13:05:21 +03:00
|
|
|
#include <AK/Vector.h>
|
2020-09-12 20:38:55 +03:00
|
|
|
|
|
|
|
namespace Diff {
|
2022-12-18 21:52:25 +03:00
|
|
|
DeprecatedString generate_only_additions(StringView text)
|
2020-09-12 20:38:55 +03:00
|
|
|
{
|
2022-12-18 21:52:25 +03:00
|
|
|
auto lines = text.split_view('\n', SplitBehavior::KeepEmpty);
|
2020-09-12 20:38:55 +03:00
|
|
|
StringBuilder builder;
|
2021-05-07 12:58:21 +03: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 12:58:21 +03:00
|
|
|
builder.appendff("+{}\n", line);
|
2020-09-12 20:38:55 +03:00
|
|
|
}
|
2022-12-06 04:12:49 +03:00
|
|
|
return builder.to_deprecated_string();
|
2020-09-12 20:38:55 +03:00
|
|
|
}
|
2023-06-21 09:48:17 +03:00
|
|
|
|
|
|
|
ErrorOr<void> write_normal(Hunk const& hunk, Stream& stream, ColorOutput color_output)
|
|
|
|
{
|
|
|
|
// Source line(s)
|
2023-06-24 08:33:04 +03:00
|
|
|
TRY(stream.write_formatted("{}", hunk.location.old_range.start_line));
|
|
|
|
if (hunk.location.old_range.number_of_lines > 1)
|
|
|
|
TRY(stream.write_formatted(",{}", (hunk.location.old_range.start_line + hunk.location.old_range.number_of_lines - 1)));
|
2023-06-21 09:48:17 +03:00
|
|
|
|
|
|
|
// Action
|
2023-06-24 08:33:04 +03:00
|
|
|
if (hunk.location.old_range.number_of_lines > 0 && hunk.location.new_range.number_of_lines > 0)
|
2023-06-21 09:48:17 +03:00
|
|
|
TRY(stream.write_formatted("c"));
|
2023-06-24 08:33:04 +03:00
|
|
|
else if (hunk.location.new_range.number_of_lines > 0)
|
2023-06-21 09:48:17 +03:00
|
|
|
TRY(stream.write_formatted("a"));
|
|
|
|
else
|
|
|
|
TRY(stream.write_formatted("d"));
|
|
|
|
|
|
|
|
// Target line(s)
|
2023-06-24 08:33:04 +03:00
|
|
|
TRY(stream.write_formatted("{}", hunk.location.new_range.start_line));
|
|
|
|
if (hunk.location.new_range.number_of_lines > 1)
|
|
|
|
TRY(stream.write_formatted(",{}", (hunk.location.new_range.start_line + hunk.location.new_range.number_of_lines - 1)));
|
2023-06-21 09:48:17 +03:00
|
|
|
|
|
|
|
TRY(stream.write_formatted("\n"));
|
|
|
|
|
2023-06-24 08:33:04 +03:00
|
|
|
for (auto const& line : hunk.lines) {
|
|
|
|
VERIFY(line.operation == Line::Operation::Removal || line.operation == Line::Operation::Addition);
|
2023-06-21 09:48:17 +03:00
|
|
|
|
2023-06-24 08:33:04 +03:00
|
|
|
if (line.operation == Line::Operation::Addition) {
|
|
|
|
if (color_output == ColorOutput::Yes)
|
|
|
|
TRY(stream.write_formatted("\033[32;1m> {}\033[0m\n", line.content));
|
|
|
|
else
|
|
|
|
TRY(stream.write_formatted("> {}\n", line.content));
|
|
|
|
} else {
|
|
|
|
if (color_output == ColorOutput::Yes)
|
|
|
|
TRY(stream.write_formatted("\033[31;1m< {}\033[0m\n", line.content));
|
|
|
|
else
|
|
|
|
TRY(stream.write_formatted("< {}\n", line.content));
|
|
|
|
}
|
2023-06-21 09:48:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|