-
-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathViewTask.cpp
More file actions
387 lines (328 loc) · 13 KB
/
ViewTask.cpp
File metadata and controls
387 lines (328 loc) · 13 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2006 - 2021, Tomas Babej, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// https://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#include <cmake.h>
// cmake.h include header must come first
#include <Context.h>
#include <ViewTask.h>
#include <format.h>
#include <rules.h>
#include <utf8.h>
#include <util.h>
#include <numeric>
////////////////////////////////////////////////////////////////////////////////
ViewTask::ViewTask()
: _width(0),
_left_margin(0),
_header(0),
_sort_header(0),
_odd(0),
_even(0),
_intra_padding(1),
_intra_odd(0),
_intra_even(0),
_extra_padding(0),
_extra_odd(0),
_extra_even(0),
_truncate_lines(0),
_truncate_rows(0),
_lines(0),
_rows(0) {}
////////////////////////////////////////////////////////////////////////////////
ViewTask::~ViewTask() {
for (auto& col : _columns) delete col;
_columns.clear();
}
////////////////////////////////////////////////////////////////////////////////
// |<---------- terminal width ---------->|
//
// +-------+ +-------+ +-------+
// |header | |header | |header |
// +--+--+-------+--+-------+--+-------+--+
// |ma|ex|cell |in|cell |in|cell |ex|
// +--+--+-------+--+-------+--+-------+--+
// |ma|ex|cell |in|cell |in|cell |ex|
// +--+--+-------+--+-------+--+-------+--+
//
// margin - indentation for the whole table
// extrapadding - left and right padding for the whole table
// intrapadding - padding between columns
//
//
// Layout Algorithm:
// - Height is irrelevant
// - Determine the usable horizontal space for N columns:
//
// usable = width - ma - (ex * 2) - (in * (N - 1))
//
// - Look at every column, for every task, and determine the minimum and
// maximum widths. The minimum is the length of the largest indivisible
// word, and the maximum is the full length of the value.
// - If there is sufficient terminal width to display every task using the
// maximum width, then do so.
// - If there is insufficient terminal width to display every task using the
// minimum width, then there is no layout solution. Error.
// - Otherwise there is a need for column wrapping. Calculate the overage,
// which is the difference between the sum of the minimum widths and the
// usable width.
// - Start by using all the minimum column widths, and distribute the overage
// among all columns, one character at a time, while the column width is
// less than the maximum width, and while there is overage remaining.
//
// Note: a possible enhancement is to proportionally distribute the overage
// according to average data length.
//
// Note: an enhancement to the 'no solution' problem is to simply force-break
// the larger fields. If the widest field is W0, and the second widest
// field is W1, then a solution may be achievable by reducing W0 --> W1.
//
std::string ViewTask::render(std::vector<Task>& data, std::vector<int>& sequence) {
Timer timer;
bool const obfuscate = Context::getContext().config.getBoolean("obfuscate");
bool const print_empty_columns = Context::getContext().config.getBoolean("print.empty.columns");
std::vector<Column*> nonempty_columns;
std::vector<bool> nonempty_sort;
// Determine minimal, ideal column widths.
std::vector<int> minimal;
std::vector<int> ideal;
for (unsigned int i = 0; i < _columns.size(); ++i) {
// Headers factor in to width calculations.
unsigned int global_min = 0;
unsigned int global_ideal = global_min;
for (unsigned int s = 0; s < sequence.size(); ++s) {
if ((int)s >= _truncate_lines && _truncate_lines != 0) break;
if ((int)s >= _truncate_rows && _truncate_rows != 0) break;
// Determine minimum and ideal width for this column.
unsigned int min = 0;
unsigned int ideal = 0;
_columns[i]->measure(data[sequence[s]], min, ideal);
if (min > global_min) global_min = min;
if (ideal > global_ideal) global_ideal = ideal;
// If a fixed-width column was just measured, there is no point repeating
// the measurement for all tasks.
if (_columns[i]->is_fixed_width()) break;
}
if (print_empty_columns || global_min != 0) {
unsigned int label_length = utf8_width(_columns[i]->label());
if (label_length > global_min) global_min = label_length;
if (label_length > global_ideal) global_ideal = label_length;
minimal.push_back(global_min);
ideal.push_back(global_ideal);
}
if (!print_empty_columns) {
if (global_min != 0) // Column is nonempty
{
nonempty_columns.push_back(_columns[i]);
nonempty_sort.push_back(_sort[i]);
} else // Column is empty, drop it
{
// Note: This is safe to do because we set _columns = nonempty_columns
// after iteration over _columns is finished.
delete _columns[i];
}
}
}
if (!print_empty_columns) {
_columns = nonempty_columns;
_sort = nonempty_sort;
}
int all_extra = _left_margin + (2 * _extra_padding) + ((_columns.size() - 1) * _intra_padding);
// Sum the widths.
int sum_minimal = std::accumulate(minimal.begin(), minimal.end(), 0);
int sum_ideal = std::accumulate(ideal.begin(), ideal.end(), 0);
// Calculate final column widths.
int overage = _width - sum_minimal - all_extra;
Context::getContext().debug(format("ViewTask::render min={1} ideal={2} overage={3} width={4}",
sum_minimal + all_extra, sum_ideal + all_extra, overage,
_width));
std::vector<int> widths;
// Ideal case. Everything fits.
if (_width == 0 || sum_ideal + all_extra <= _width) {
widths = ideal;
}
// Not enough for minimum. Decrease certain columns.
else if (overage < 0) {
// Determine which columns are the longest.
unsigned int longest = 0;
unsigned int second_longest = 0;
for (unsigned int j = 0; j < minimal.size(); j++) {
if (minimal[j] > minimal[longest]) {
second_longest = longest;
longest = j;
} else if (minimal[j] > minimal[second_longest]) {
second_longest = j;
}
}
// Case 1: Shortening longest column still keeps it longest. Let it bear
// all the shortening.
widths = minimal;
if (minimal[longest] + overage >= minimal[second_longest]) widths[longest] += overage;
// Case 2: Shorten the longest column to second longest length. Try to
// split shortening them evenly.
else {
int decrease = minimal[second_longest] - minimal[longest];
widths[longest] += decrease;
overage = overage - decrease;
// Attempt to decrease the two longest columns (at most to two characters)
if (-overage <= widths[longest] + widths[second_longest] - 4) {
// Compute half of the overage, rounding up
int half_overage = overage / 2 + overage % 2;
// Decrease both larges columns by this amount
widths[longest] += half_overage;
widths[second_longest] += half_overage;
} else
// If reducing two of the longest solumns to 2 characters is not sufficient, then give up.
Context::getContext().error(format(
"The report has a minimum width of {1} and does not fit in the available width of {2}.",
sum_minimal + all_extra, _width));
}
}
// Perfect minimal width.
else if (overage == 0) {
widths = minimal;
}
// Extra space to share.
else if (overage > 0) {
widths = minimal;
// Spread 'overage' among columns where width[i] < ideal[i]
bool needed = true;
while (overage && needed) {
needed = false;
for (unsigned int i = 0; i < _columns.size() && overage; ++i) {
if (widths[i] < ideal[i]) {
++widths[i];
--overage;
needed = true;
}
}
}
}
// Compose column headers.
unsigned int max_lines = 0;
std::vector<std::vector<std::string>> headers;
for (unsigned int c = 0; c < _columns.size(); ++c) {
headers.emplace_back();
_columns[c]->renderHeader(headers[c], widths[c], _sort[c] ? _sort_header : _header);
if (headers[c].size() > max_lines) max_lines = headers[c].size();
}
// Render column headers.
std::string left_margin = std::string(_left_margin, ' ');
std::string extra = std::string(_extra_padding, ' ');
std::string intra = std::string(_intra_padding, ' ');
std::string extra_odd = Context::getContext().color() ? _extra_odd.colorize(extra) : extra;
std::string extra_even = Context::getContext().color() ? _extra_even.colorize(extra) : extra;
std::string intra_odd = Context::getContext().color() ? _intra_odd.colorize(intra) : intra;
std::string intra_even = Context::getContext().color() ? _intra_even.colorize(intra) : intra;
std::string out;
_lines = 0;
for (unsigned int i = 0; i < max_lines; ++i) {
out += left_margin + extra;
for (unsigned int c = 0; c < _columns.size(); ++c) {
if (c) out += intra;
if (headers[c].size() < max_lines - i)
out += _header.colorize(std::string(widths[c], ' '));
else
out += headers[c][i];
}
out += extra;
// Trim right.
out.erase(out.find_last_not_of(' ') + 1);
out += "\n";
// Stop if the line limit is exceeded.
if (++_lines >= _truncate_lines && _truncate_lines != 0) {
Context::getContext().time_render_us += timer.total_us();
return out;
}
}
// Compose, render columns, in sequence.
_rows = 0;
std::vector<std::vector<std::string>> cells;
for (unsigned int s = 0; s < sequence.size(); ++s) {
max_lines = 0;
// Apply color rules to task.
Color rule_color;
autoColorize(data[sequence[s]], rule_color);
// Alternate rows based on |s % 2|
bool odd = (s % 2) ? true : false;
Color row_color;
if (Context::getContext().color()) {
row_color = odd ? _odd : _even;
row_color.blend(rule_color);
}
for (unsigned int c = 0; c < _columns.size(); ++c) {
cells.emplace_back();
_columns[c]->render(cells[c], data[sequence[s]], widths[c], row_color);
if (cells[c].size() > max_lines) max_lines = cells[c].size();
if (obfuscate)
if (_columns[c]->type() == "string" && _columns[c]->name() != "depends")
for (auto& line : cells[c]) line = obfuscateText(line);
}
// Listing breaks are simply blank lines inserted when a column value
// changes.
if (s > 0 && _breaks.size() > 0) {
for (const auto& b : _breaks) {
if (data[sequence[s - 1]].get(b) != data[sequence[s]].get(b)) {
out += "\n";
++_lines;
// Only want one \n, regardless of how many values change.
break;
}
}
}
for (unsigned int i = 0; i < max_lines; ++i) {
out += left_margin + (odd ? extra_odd : extra_even);
for (unsigned int c = 0; c < _columns.size(); ++c) {
if (c) {
if (row_color.nontrivial())
row_color._colorize(out, intra);
else
out += (odd ? intra_odd : intra_even);
}
if (i < cells[c].size())
out += cells[c][i];
else
row_color._colorize(out, std::string(widths[c], ' '));
}
out += (odd ? extra_odd : extra_even);
// Trim right.
out.erase(out.find_last_not_of(' ') + 1);
out += "\n";
// Stop if the line limit is exceeded.
if (++_lines >= _truncate_lines && _truncate_lines != 0) {
Context::getContext().time_render_us += timer.total_us();
return out;
}
}
cells.clear();
// Stop if the row limit is exceeded.
if (++_rows >= _truncate_rows && _truncate_rows != 0) {
Context::getContext().time_render_us += timer.total_us();
return out;
}
}
Context::getContext().time_render_us += timer.total_us();
return out;
}
////////////////////////////////////////////////////////////////////////////////