-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathTableOutput.h
220 lines (185 loc) · 6.69 KB
/
TableOutput.h
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#pragma once
#include "ExecutionReporter.h"
#include "Resources.h"
#include <array>
#include <ostream>
#include <string>
#include <vector>
namespace AppInstaller::CLI::Execution
{
// Enables output data in a table format.
// TODO: Improve for use with sparse data.
template <size_t FieldCount>
struct TableOutput
{
using header_t = std::array<Resource::LocString, FieldCount>;
using line_t = std::array<std::string, FieldCount>;
TableOutput(Reporter& reporter, header_t&& header, size_t sizingBuffer = 50) :
m_reporter(reporter), m_sizingBuffer(sizingBuffer)
{
for (size_t i = 0; i < FieldCount; ++i)
{
m_columns[i].Name = std::move(header[i]);
m_columns[i].MinLength = Utility::UTF8ColumnWidth(m_columns[i].Name.get());
m_columns[i].MaxLength = 0;
}
}
void OutputLine(line_t&& line)
{
m_empty = false;
if (m_buffer.size() < m_sizingBuffer)
{
m_buffer.emplace_back(std::move(line));
}
else
{
EvaluateAndFlushBuffer();
OutputLineToStream(line);
}
}
void Complete()
{
if (!m_empty)
{
EvaluateAndFlushBuffer();
}
}
bool IsEmpty()
{
return m_empty;
}
private:
// A column in the table.
struct Column
{
Resource::LocString Name;
size_t MinLength = 0;
size_t MaxLength = 0;
bool SpaceAfter = true;
};
Reporter& m_reporter;
std::array<Column, FieldCount> m_columns;
size_t m_sizingBuffer;
std::vector<line_t> m_buffer;
bool m_bufferEvaluated = false;
bool m_empty = true;
void EvaluateAndFlushBuffer()
{
if (m_bufferEvaluated)
{
return;
}
// Determine the maximum length for all columns
for (const auto& line : m_buffer)
{
for (size_t i = 0; i < FieldCount; ++i)
{
m_columns[i].MaxLength = std::max(m_columns[i].MaxLength, Utility::UTF8ColumnWidth(line[i]));
}
}
// If there are actually columns with data, then also bring in the minimum size
for (size_t i = 0; i < FieldCount; ++i)
{
if (m_columns[i].MaxLength)
{
m_columns[i].MaxLength = std::max(m_columns[i].MaxLength, m_columns[i].MinLength);
}
}
// Only output the extra space if:
// 1. Not the last field
m_columns[FieldCount - 1].SpaceAfter = false;
// 2. Not empty (taken care of by not doing anything if empty)
// 3. There are non-empty fields after
for (size_t i = FieldCount - 1; i > 0; --i)
{
if (m_columns[i].MaxLength)
{
break;
}
else
{
m_columns[i - 1].SpaceAfter = false;
}
}
// Determine the total width required to not truncate any columns
size_t totalRequired = 0;
for (size_t i = 0; i < FieldCount; ++i)
{
totalRequired += m_columns[i].MaxLength + (m_columns[i].SpaceAfter ? 1 : 0);
}
size_t consoleWidth = GetConsoleWidth();
// If the total space would be too big, shrink them.
// We don't want to use the last column, lest we auto-wrap
if (totalRequired >= consoleWidth)
{
size_t extra = (totalRequired - consoleWidth) + 1;
while (extra)
{
size_t targetIndex = 0;
size_t targetVal = m_columns[0].MaxLength;
for (size_t j = 1; j < FieldCount; ++j)
{
if (m_columns[j].MaxLength > targetVal)
{
targetIndex = j;
targetVal = m_columns[j].MaxLength;
}
}
m_columns[targetIndex].MaxLength -= 1;
extra -= 1;
}
totalRequired = consoleWidth - 1;
}
// Header line
line_t headerLine;
for (size_t i = 0; i < FieldCount; ++i)
{
headerLine[i] = m_columns[i].Name.get();
}
OutputLineToStream(headerLine);
m_reporter.Info() << std::string(totalRequired, '-') << std::endl;
for (const auto& line : m_buffer)
{
OutputLineToStream(line);
}
m_bufferEvaluated = true;
}
void OutputLineToStream(const line_t& line)
{
auto out = m_reporter.Info();
for (size_t i = 0; i < FieldCount; ++i)
{
const auto& col = m_columns[i];
if (col.MaxLength)
{
size_t valueLength = Utility::UTF8ColumnWidth(line[i]);
if (valueLength > col.MaxLength)
{
size_t actualWidth;
out << Utility::UTF8TrimRightToColumnWidth(line[i], col.MaxLength - 1, actualWidth) << "\xE2\x80\xA6"; // UTF8 encoding of ellipsis (…) character
// Some characters take 2 unit space, the trimmed string length might be 1 less than the expected length.
if (actualWidth != col.MaxLength - 1)
{
out << ' ';
}
if (col.SpaceAfter)
{
out << ' ';
}
}
else
{
out << line[i];
if (col.SpaceAfter)
{
out << std::string(col.MaxLength - valueLength + 1, ' ');
}
}
}
}
out << std::endl;
}
};
}