-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordBox.cpp
128 lines (101 loc) · 3.13 KB
/
WordBox.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
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
#include "WordBox.h"
WordBox::WordBox(wchar_t h, wchar_t v, wchar_t c)
: _h(h), _v(v), _c(c), _hasRuler(true) {
std::setlocale(LC_ALL, "");
}
void WordBox::setAlign(unsigned i, bool left) {
_align[i] = left;
}
bool WordBox::isAligned(unsigned i) const { return _align[i]; }
wchar_t WordBox::vertical() const { return _v; }
wchar_t WordBox::horizontal() const { return _h; }
void WordBox::addContent(const std::wstring& content) {
_current.push_back(format(content));
}
void WordBox::newRow() {
_rows.push_back(_current);
_current.clear();
}
template <typename Iterator>
void WordBox::addRow(Iterator begin, Iterator end) {
for (auto i = begin; i != end; ++i) {
addContent(*i);
}
newRow();
}
template <typename Container>
void WordBox::addRow(const Container& container) {
addRow(container.begin(), container.end());
}
const std::vector<WordBox::Row>& WordBox::getRows() const { return _rows; }
void WordBox::prepare() const {
calculateWidths();
configureAlignment();
}
std::wstring WordBox::ruler() const {
std::wstring result;
result += _c;
for (const auto& width : _width) {
result += repeat(width, _h);
result += _c;
}
return result;
}
int WordBox::getWidth(unsigned i) const { return _width[i]; }
bool WordBox::hasRuler() const { return _hasRuler; }
void WordBox::toggleRuler() { _hasRuler = !_hasRuler; }
void WordBox::print(void) {
int count = 0;
if (_rows.empty()) return;
prepare();
if (hasRuler()) {
std::wcout << ruler() << L"\n";
}
for (const auto& row : _rows) {
std::wcout << vertical();
for (unsigned i = 0; i < row.size(); ++i) {
auto alignment = isAligned(i) ? std::left : std::right;
std::wcout << std::setw(getWidth(i)) << alignment << row[i];
std::wcout << vertical();
}
std::wcout << L"\n";
if (hasRuler() && count == 0 ) {
std::wcout << ruler() << L"\n";
}
count++;
}
if (hasRuler() && _rows.size() > 1) {
std::wcout << ruler() << L"\n";
}
}
std::wstring WordBox::repeat(unsigned times, wchar_t c) {
return std::wstring(times, c);
}
unsigned WordBox::columnCount() const { return _rows.empty() ? 0 : _rows[0].size(); }
std::wstring WordBox::format(const std::wstring& content) const {
std::wstring formatted = L" " + content + L" ";
if (formatted.length() > maxrowlength+2) {
formatted = formatted.substr(0, maxrowlength) + L"...";
}
return formatted;
}
void WordBox::textLength(int l) {
maxrowlength = l;
}
void WordBox::calculateWidths() const {
if (_rows.empty()) return;
_width.assign(columnCount(), 0);
for (const auto& row : _rows) {
for (unsigned i = 0; i < row.size(); ++i) {
_width[i] = std::max(_width[i], static_cast<unsigned>(row[i].length()));
}
}
}
void WordBox::configureAlignment() const {
if (_rows.empty()) return;
for (unsigned i = 0; i < columnCount(); ++i) {
if (_align.find(i) == _align.end()) {
_align[i] = true; // Default to left alignment
}
}
}