forked from cubicdaiya/dtl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uni_hunk_display_simple.hpp
143 lines (119 loc) · 4.06 KB
/
uni_hunk_display_simple.hpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#ifndef DTL_MODERN_EXTRA_UNI_HUNK_DISPLAY_SIMPLE_HPP
#define DTL_MODERN_EXTRA_UNI_HUNK_DISPLAY_SIMPLE_HPP
#include "dtl_modern/common.hpp"
namespace dtl_modern::extra
{
/**
* @struct UniHunkDisplaySimple
*
* @brief Wrapper type for displaying UniHunk in a simple format.
*/
template <Diffable E>
struct UniHunkDisplaySimple
{
const UniHunk<E>& m_hunk;
// only checks whether it points to the same unihunk
bool operator==(const UniHunkDisplaySimple& other) const { return &m_hunk == &other.m_hunk; }
};
/**
* @struct UniHunkSeqDisplaySimple
*
* @brief Wrapper type for displaying UniHunkSeq in a simple format.
*/
template <Diffable E>
struct UniHunkSeqDisplaySimple
{
const UniHunkSeq<E>& m_hunks;
// only checks whether it points to the same unihunk
bool operator==(const UniHunkSeqDisplaySimple& other) const { return &m_hunks == &other.m_hunks; }
};
/**
* @brief Create a wrapper for displaying UniHunk in a simple format.
*
* @param hunk The UniHunk to display.
* @return The wrapper for displaying UniHunk in a simple format.
*/
template <Diffable E>
UniHunkDisplaySimple<E> display(const UniHunk<E>& hunk)
{
return { hunk };
}
/**
* @brief Create a wrapper for displaying UniHunkSeq in a simple format.
*
* @param hunks The UniHunkSeq to display.
* @return The wrapper for displaying UniHunkSeq in a simple format.
*/
template <Diffable E>
UniHunkSeqDisplaySimple<E> display(const UniHunkSeq<E>& hunks)
{
return { hunks };
}
}
#ifdef DTL_MODERN_DISPLAY_FMTLIB
# define DTL_MODERN_FMT fmt
# include <fmt/core.h>
#else
# define DTL_MODERN_FMT std
# include <format>
#endif
#include <algorithm>
template <dtl_modern::Diffable E>
struct DTL_MODERN_FMT::formatter<dtl_modern::extra::UniHunkDisplaySimple<E>>
: public DTL_MODERN_FMT::formatter<std::string_view>
{
using UniHunk = dtl_modern::extra::UniHunkDisplaySimple<E>;
auto format(const UniHunk& uni_hunk, auto& fmt) const
{
using dtl_modern::ses_mark, dtl_modern::SesEdit;
using std::ranges::for_each;
const auto& [a, b, c, d, common_0, common_1, change, inc_dec_count] = uni_hunk.m_hunk;
auto f_ses_elem_change = [&](auto&& ses_elem) {
const auto& [elem, info] = ses_elem;
DTL_MODERN_FMT::format_to(fmt.out(), "{}{}\n", ses_mark(info.m_type), elem);
};
auto f_ses_elem_common = [&](auto&& ses_elem) {
DTL_MODERN_FMT::format_to(fmt.out(), "{}{}\n", ses_mark(SesEdit::Common), ses_elem.m_elem);
};
DTL_MODERN_FMT::format_to(fmt.out(), "@@ -{},{} +{},{} @@\n", a, b, c, d);
for_each(common_0, f_ses_elem_common);
for_each(change, f_ses_elem_change);
for_each(common_1, f_ses_elem_common);
return fmt.out();
}
};
template <dtl_modern::Diffable E>
struct DTL_MODERN_FMT::formatter<dtl_modern::extra::UniHunkSeqDisplaySimple<E>>
: public DTL_MODERN_FMT::formatter<std::string_view>
{
using UniHunks = dtl_modern::extra::UniHunkSeqDisplaySimple<E>;
auto format(const UniHunks& uni_hunks, auto& fmt) const
{
using dtl_modern::extra::display;
using dtl_modern::extra::UniHunkDisplaySimple;
for (const auto& uni_hunk : uni_hunks.m_hunks.m_inner) {
DTL_MODERN_FMT::format_to(fmt.out(), "{}", display(uni_hunk));
}
return fmt.out();
}
};
#include <ostream>
namespace dtl_modern::extra
{
template <Diffable E>
std::ostream& operator<<(std::ostream& os, const UniHunkDisplaySimple<E>& uni_hunk)
{
os << DTL_MODERN_FMT::format("{}", uni_hunk);
return os;
}
template <Diffable E>
std::ostream& operator<<(std::ostream& os, const std::vector<UniHunkDisplaySimple<E>>& hunks)
{
for (const auto& hunk : hunks) {
os << hunk;
}
return os;
}
}
#undef DTL_MODERN_FMT
#endif /* end of include guard: DTL_MODERN_EXTRA_UNI_HUNK_DISPLAY_SIMPLE_HPP */