|
1 #pragma once |
|
2 |
|
3 #include <string> |
|
4 #include <vector> |
|
5 #include <iostream> |
|
6 #include <sstream> |
|
7 #include <locale> |
|
8 #include <codecvt> |
|
9 #include <regex> |
|
10 |
|
11 #include <relpipe/reader/typedefs.h> |
|
12 #include <relpipe/reader/TypeId.h> |
|
13 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
14 |
|
15 namespace relpipe { |
|
16 namespace out { |
|
17 namespace tabular { |
|
18 |
|
19 using namespace relpipe::reader; |
|
20 |
|
21 class TabularPrefetchingHandler : public handlers::RelationalReaderStringHadler { |
|
22 private: |
|
23 std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. |
|
24 const char* INDENTATION = " "; |
|
25 const char* ESC_BRIGHT = "\u001b[1m"; |
|
26 const char* ESC_RED = "\u001b[31m"; |
|
27 const char* ESC_GREEN = "\u001b[32m"; |
|
28 const char* ESC_YELLOW = "\u001b[33m"; |
|
29 const char* ESC_CYAN = "\u001b[36m"; |
|
30 const char* ESC_RESET = "\u001b[0m"; |
|
31 |
|
32 const char* ESC_HEADER = ESC_BRIGHT; |
|
33 const char* ESC_BORDER = ESC_GREEN; |
|
34 const char* ESC_VALUE = ESC_CYAN; |
|
35 const char* ESC_REPLACEMENT = ESC_RED; |
|
36 |
|
37 const char* INDENT = " "; // table indent from the left |
|
38 |
|
39 std::ostream &output; |
|
40 |
|
41 std::vector<TypeId> columnTypes; |
|
42 std::vector<string_t> columnTypeCodes; |
|
43 std::vector<string_t> columnNames; |
|
44 std::vector<integer_t> columnWidths; |
|
45 std::vector<string_t> values; // all values are saved here and processed at the end of the relation |
|
46 integer_t columnCount = 0; |
|
47 |
|
48 const string_t colorizeReplacement(const string_t &replacement, const char* valueColor) { |
|
49 return convertor.from_bytes(ESC_RESET) + convertor.from_bytes(ESC_REPLACEMENT) + replacement + convertor.from_bytes(ESC_RESET) + convertor.from_bytes(valueColor); |
|
50 } |
|
51 |
|
52 /** |
|
53 * Sanitizes whitespace that could broke table layout. |
|
54 * |
|
55 * TODO: sanitize also escape sequences and emoji (resp. properly measure their width) |
|
56 * |
|
57 * @param value original value |
|
58 * @param color value foreground color |
|
59 * @return value with replaced whitespaces |
|
60 */ |
|
61 const string_t formatValue(const string_t &value, const char* color) { |
|
62 std::wstringstream result; |
|
63 |
|
64 result << convertor.from_bytes(color); |
|
65 |
|
66 for (auto & ch : value) { |
|
67 switch (ch) { |
|
68 case L'\n': result << colorizeReplacement(L"↲", color); |
|
69 break; |
|
70 case L'\r': result << colorizeReplacement(L"⏎", color); |
|
71 break; |
|
72 case L'\t': result << colorizeReplacement(L"↹", color); |
|
73 break; |
|
74 case L' ': result << colorizeReplacement(L"⎵", color); |
|
75 break; |
|
76 default: result << ch; |
|
77 } |
|
78 } |
|
79 |
|
80 result << convertor.from_bytes(ESC_RESET); |
|
81 |
|
82 return result.str(); |
|
83 } |
|
84 |
|
85 void printHorizontalLine(const string_t &left, const string_t &middle, const string_t &right) { |
|
86 const string_t bar = L"─"; |
|
87 // TODO: support also ASCII nostalgia: |
|
88 // border = border.replaceAll("─", "-"); |
|
89 // border = border.replaceAll("│", "|"); |
|
90 // border = border.replaceAll("[╭┬╮├┼┤╰┴╯]", "+"); |
|
91 |
|
92 output << INDENT << ESC_BORDER; |
|
93 output << convertor.to_bytes(left); |
|
94 for (size_t c = 0; c < columnCount; c++) { |
|
95 integer_t width = columnWidths[c]; |
|
96 for (integer_t w = 0; w < (width + 2); w++) output << convertor.to_bytes(bar); // 2 = left and right padding of the value |
|
97 if (c < (columnCount - 1)) output << convertor.to_bytes(middle); |
|
98 } |
|
99 output << convertor.to_bytes(right); |
|
100 output << ESC_RESET << std::endl; |
|
101 |
|
102 } |
|
103 |
|
104 void printCachedData() { |
|
105 // Compute column widths and paddings: |
|
106 vector<integer_t> paddings(columnCount); |
|
107 for (size_t i = 0; i < columnCount; i++) { |
|
108 string_t typeCode = columnTypeCodes[i]; |
|
109 string_t columnName = columnNames[i]; |
|
110 integer_t minWidth = columnName.size() + typeCode.size() + 3; // 3 = " ()" in "columnName (typeCode)" |
|
111 columnWidths[i] = max(columnWidths[i], minWidth); |
|
112 paddings[i] = columnWidths[i] - minWidth; |
|
113 } |
|
114 |
|
115 printHorizontalLine(L"╭", L"┬", L"╮"); |
|
116 |
|
117 // Print column headers: |
|
118 output << INDENT << ESC_BORDER << "│" << ESC_RESET; |
|
119 for (size_t i = 0; i < columnCount; i++) { |
|
120 output << " " << convertor.to_bytes(formatValue(columnNames[i], ESC_HEADER)); |
|
121 for (integer_t p = 0; p < paddings[i]; p++) { |
|
122 output << " "; |
|
123 } |
|
124 output << " (" << convertor.to_bytes(columnTypeCodes[i]) << ")"; |
|
125 output << ESC_BORDER << " │" << ESC_RESET; |
|
126 } |
|
127 output << std::endl; |
|
128 printHorizontalLine(L"├", L"┼", L"┤"); |
|
129 |
|
130 // Print particular rows: |
|
131 for (size_t i = 0; i < values.size(); i++) { |
|
132 integer_t columnIndex = i % columnCount; |
|
133 if (columnIndex == 0) output << INDENT << ESC_BORDER << "│" << ESC_RESET; |
|
134 string_t stringValue = values[i]; |
|
135 integer_t padding = columnWidths[columnIndex] - stringValue.size(); |
|
136 boolean_t alignRight = columnTypes[columnIndex] == TypeId::BOOLEAN || columnTypes[columnIndex] == TypeId::INTEGER; |
|
137 |
|
138 if (alignRight) for (integer_t p = 0; p < padding; p++) output << " "; |
|
139 output << " " << convertor.to_bytes(formatValue(stringValue, ESC_VALUE)); |
|
140 if (!alignRight) for (integer_t p = 0; p < padding; p++) output << " "; |
|
141 |
|
142 output << ESC_BORDER << " │" << ESC_RESET; |
|
143 if (columnIndex == (columnCount - 1)) output << std::endl; |
|
144 } |
|
145 printHorizontalLine(L"╰", L"┴", L"╯"); |
|
146 integer_t recordCount = values.size() / columnCount; |
|
147 output << ESC_YELLOW << "Record count: " << ESC_RESET << recordCount << std::endl; |
|
148 |
|
149 values.clear(); |
|
150 } |
|
151 |
|
152 public: |
|
153 |
|
154 TabularPrefetchingHandler(std::ostream& output) : output(output) { |
|
155 } |
|
156 |
|
157 void startRelation(string_t name, std::vector<std::pair<string_t, TypeId> > attributes) override { |
|
158 if (columnCount) printCachedData(); |
|
159 |
|
160 output << ESC_RED << convertor.to_bytes(name) << ":" << ESC_RESET << endl; |
|
161 columnCount = attributes.size(); |
|
162 columnTypes.resize(columnCount); |
|
163 columnTypeCodes.resize(columnCount); |
|
164 columnNames.resize(columnCount); |
|
165 columnWidths.resize(columnCount, 0); |
|
166 for (int i = 0; i < attributes.size(); i++) { |
|
167 columnNames[i] = attributes[i].first; |
|
168 columnTypes[i] = attributes[i].second; |
|
169 columnTypeCodes[i] = L"TODO"; // TODO: type codes/names |
|
170 } |
|
171 } |
|
172 |
|
173 void attribute(const string_t& value) override { |
|
174 integer_t i = values.size() % columnCount; |
|
175 values.push_back(value); |
|
176 columnWidths[i] = max(columnWidths[i], value.length()); |
|
177 } |
|
178 |
|
179 void endOfPipe() { |
|
180 if (columnCount) printCachedData(); |
|
181 } |
|
182 |
|
183 }; |
|
184 |
|
185 } |
|
186 } |
|
187 } |