forked from cubicdaiya/dtl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretty_print.cpp
60 lines (48 loc) · 1.99 KB
/
pretty_print.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <dtl_modern/dtl_modern.hpp>
#include <dtl_modern/extra/ses_display_pretty.hpp> // requires fmt
#include <fmt/core.h>
#include <fmt/color.h>
#include <iterator>
int main()
{
using namespace std::literals; // s and sv UDL
auto hello1 = "hello World!"sv; // notice the different type, this one is std::string_view
auto hello2 = "Hell word"s; // this one is std::string
auto [lcs, ses, edit_distance] = dtl_modern::diff(hello1, hello2);
// diy
// ---
auto hello1_color = std::string{};
auto hello2_color = std::string{};
auto hello1_out = std::back_inserter(hello1_color);
auto hello2_out = std::back_inserter(hello2_color);
const auto red = fmt::bg(fmt::color::red);
const auto green = fmt::bg(fmt::color::green);
const auto dark_red = fmt::bg(fmt::color::dark_red);
const auto dark_green = fmt::bg(fmt::color::dark_green);
for (const auto& [elem, info] : ses.get()) {
using Edit = dtl_modern::SesEdit;
switch (info.m_type) {
case Edit::Delete: fmt::format_to(hello1_out, red, "{}", elem); break;
case Edit::Add: fmt::format_to(hello2_out, green, "{}", elem); break;
case Edit::Common: {
fmt::format_to(hello1_out, dark_red, "{}", elem);
fmt::format_to(hello2_out, dark_green, "{}", elem);
} break;
}
}
fmt::println("\n--- diy:");
fmt::println("{}", hello1_color);
fmt::println("{}", hello2_color);
// ---
// extra dtl_modern functionality
// ---
using dtl_modern::extra::display_pretty;
fmt::println("\n--- extra:");
fmt::println("\n> <nothing> :\n{}", display_pretty(ses));
fmt::println("\n> l :\n{:l}", display_pretty(ses));
fmt::println("\n> r :\n{:r}", display_pretty(ses));
fmt::println("\n> f :\n{:f}", display_pretty(ses));
fmt::println("\n> lf :\n{:lf}", display_pretty(ses));
fmt::println("\n> rf :\n{:rf}", display_pretty(ses));
// ---
}