Skip to content
This repository was archived by the owner on Aug 5, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added support for pretty print to a console
  • Loading branch information
towel42-com committed Jan 31, 2024
commit d4dbdeab9d7839ea803fd8c3e4bd20a8449df541
27 changes: 27 additions & 0 deletions cpp17/diff_match_patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,33 @@ std::wstring diff_match_patch::diff_prettyHtml(const TDiffVector &diffs) {
return html;
}

std::wstring diff_match_patch::diff_prettyConsole(const TDiffVector &diffs) {
static std::wstring kRed{L"\033[0;31m"};
static std::wstring kGreen{L"\033[0;32m"};
static std::wstring kYellow{L"\033[0;33m"};
static std::wstring kReset{L"\033[m"};
static std::wstring kEOL{NUtils::fromPercentEncoding(L"%C2%B6") + L"\n"};

std::wstring retVal;
std::wstring text;
for (auto &&aDiff : diffs) {
text = aDiff.text;
NUtils::replace(text, L"\n", kEOL);
switch (aDiff.operation) {
case INSERT:
retVal += kGreen + text + kReset;
break;
case DELETE:
retVal += kRed + text + kReset;
break;
case EQUAL:
retVal += text;
break;
}
}
return retVal;
}

std::wstring diff_match_patch::diff_text1(const TDiffVector &diffs) {
std::wstring text;
for (auto &&aDiff : diffs) {
Expand Down
8 changes: 8 additions & 0 deletions cpp17/diff_match_patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,14 @@ class diff_match_patch {
public:
std::wstring diff_prettyHtml(const TDiffVector &diffs);

/**
* Convert a Diff list into a pretty Console report. Red for delete, and green for insert
* @param diffs LinkedList of Diff objects.
* @return Console representation.
*/
public:
std::wstring diff_prettyConsole(const TDiffVector &diffs);

/**
* Compute and return the source text (all equalities and deletions).
* @param diffs LinkedList of Diff objects.
Expand Down
31 changes: 31 additions & 0 deletions cpp17/diff_match_patch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ int diff_match_patch_test::run_all_tests() {
runTest(std::bind(&diff_match_patch_test::testDiffCleanupSemantic, this));
runTest(std::bind(&diff_match_patch_test::testDiffCleanupEfficiency, this));
runTest(std::bind(&diff_match_patch_test::testDiffPrettyHtml, this));
runTest(std::bind(&diff_match_patch_test::testDiffPrettyConsole, this));
runTest(std::bind(&diff_match_patch_test::testDiffText, this));
runTest(std::bind(&diff_match_patch_test::testDiffDelta, this));
runTest(std::bind(&diff_match_patch_test::testDiffXIndex, this));
Expand Down Expand Up @@ -583,6 +584,23 @@ TEST_F(diff_match_patch_test, testDiffPrettyHtml) {
dmp.diff_prettyHtml(diffs));
}

TEST_F(diff_match_patch_test, testDiffPrettyConsole) {
// Pretty print.
static std::wstring kRed{L"\033[0;31m"};
static std::wstring kGreen{L"\033[0;32m"};
static std::wstring kYellow{L"\033[0;33m"};
static std::wstring kReset{L"\033[m"};
static std::wstring kEOL{NUtils::fromPercentEncoding(L"%C2%B6") + L"\n"};

auto diffs = TDiffVector(
{Diff(EQUAL, "a\n"), Diff(DELETE, "<B>b</B>"), Diff(INSERT, "c&d")});
auto results = dmp.diff_prettyConsole(diffs);
assertEquals(
"diff_prettyConsole:",
L"a" + kEOL + kRed + L"<B>b</B>" + kReset + kGreen + L"c&d" + kReset,
results);
}

TEST_F(diff_match_patch_test, testDiffText) {
// Compute the source and destination texts.
auto diffs = {Diff(EQUAL, "jump"), Diff(DELETE, "s"), Diff(INSERT, "ed"),
Expand Down Expand Up @@ -1308,11 +1326,24 @@ TEST_F(diff_match_patch_test, fromGitHubExamples) {
L"your head.";
auto diffs = dmp.diff_main(lhs, rhs);
dmp.diff_cleanupSemantic(diffs);
auto console = dmp.diff_prettyConsole(diffs);
auto html = dmp.diff_prettyHtml(diffs);
auto delta = dmp.diff_toDelta(diffs);

auto consoleGolden =
L"I am the very model of a \x1B[0;31mmodern Major-General, I've "
L"information vegetable, animal, and mineral, I know the kings of "
L"England, and I quote the fights historical, From Marathon to Waterloo, "
L"in order categorical\x1B[m\x1B[0;32mcartoon individual, My animation's "
L"comical, unusual, and whimsical, I'm quite adept at funny gags, "
L"comedic theory I have read, From wicked puns and stupid jokes to "
L"anvils that drop on your head\x1B[m.";
assertEquals("gitHubDemos", consoleGolden, console);

auto htmlGolden =
LR"(<span>I am the very model of a </span><del style="background:#ffe6e6;">modern Major-General, I've information vegetable, animal, and mineral, I know the kings of England, and I quote the fights historical, From Marathon to Waterloo, in order categorical</del><ins style="background:#e6ffe6;">cartoon individual, My animation's comical, unusual, and whimsical, I'm quite adept at funny gags, comedic theory I have read, From wicked puns and stupid jokes to anvils that drop on your head</ins><span>.</span>)";
assertEquals("gitHubDemos", htmlGolden, html);

auto deltaGolden =
L"=25\t-182\t+cartoon individual, My animation's comical, unusual, and "
L"whimsical, I'm quite adept at funny gags, comedic theory I have read, "
Expand Down
2 changes: 2 additions & 0 deletions cpp17/diff_match_patch_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class diff_match_patch_test PUBLIC_TESTING {
void testDiffCleanupSemantic();
void testDiffCleanupEfficiency();
void testDiffPrettyHtml();
void testDiffPrettyConsole();
void testDiffText();
void testDiffDelta();
void testDiffXIndex();
Expand All @@ -84,6 +85,7 @@ class diff_match_patch_test PUBLIC_TESTING {
void testPatchApply();

void fromGitHubExamples();

private:
bool runTest(std::function<void()> test);
std::size_t numPassedTests{0};
Expand Down