-
Notifications
You must be signed in to change notification settings - Fork 14
/
TerminalViewer.cpp
178 lines (146 loc) · 5.87 KB
/
TerminalViewer.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
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
#include <iostream>
#include <cpp-terminal/private/conversion.hpp>
#include "TerminalViewer.h"
TerminalViewer::TerminalViewer() : terminal_(true, true, true, true) {
if (!Term::is_stdin_a_tty()) {
std::cout << "The terminal is not attached to a TTY and therefore "
"can't catch user input. Who knows what happens now?"
<< std::endl;
}
Term::get_term_size(rows_, cols_);
window_ = std::make_unique<Term::Window>(cols_, rows_);
}
std::pair<bool, bool> TerminalViewer::renderUpdate(
const std::map<uint64_t, std::map<std::string, std::pair<RingBuffer<float>, TableRow>>>&
cache) {
window_->clear();
int currRows, currCols = 0;
Term::get_term_size(currRows, currCols);
if (currCols != cols_ || currRows != rows_) {
cols_ = currCols;
rows_ = currRows;
window_ = std::make_unique<Term::Window>(cols_, rows_);
}
window_->print_border(true);
const std::u32string leftT = Term::Private::utf8_to_utf32("├");
const std::u32string rightT = Term::Private::utf8_to_utf32("┤");
const std::u32string upT = Term::Private::utf8_to_utf32("┴");
const std::u32string downT = Term::Private::utf8_to_utf32("┬");
const std::u32string vertical = Term::Private::utf8_to_utf32("│");
const std::u32string horizontal = Term::Private::utf8_to_utf32("─");
const std::u32string cross = Term::Private::utf8_to_utf32("┼");
const std::u32string upArrow = Term::Private::utf8_to_utf32("↑");
const std::u32string downArrow = Term::Private::utf8_to_utf32("↓");
const std::u32string upDownArrow = Term::Private::utf8_to_utf32("⇅");
// Print title
const std::string title = "StopwatchViewer";
if (cols_ >= (int)title.length()) {
window_->print_str(cols_ / 2 - title.length() / 2 + 1, 1, title);
}
auto drawHorizontalLine = [&](const int row) {
window_->set_char(1, row, leftT[0]);
for (int x = 2; x < cols_; x++) {
window_->set_char(x, row, horizontal[0]);
}
window_->set_char(cols_, row, rightT[0]);
};
constexpr int kNumColumns = 6;
bool firstRowVisible = cache.empty();
bool lastRowVisible = cache.empty();
if (cols_ >= 13 && rows_ >= 4) {
drawHorizontalLine(2);
drawHorizontalLine(4);
const int gapBetweenColumns = cols_ / kNumColumns;
auto drawCharEveryNth = [&](const int row, const auto character) {
for (int column = 0; column < kNumColumns - 1; column++) {
window_->set_char(gapBetweenColumns * (column + 1) + 1, row, character);
}
};
// Draw the boundaries
drawCharEveryNth(2, downT[0]);
drawCharEveryNth(3, vertical[0]);
drawCharEveryNth(4, cross[0]);
for (int row = 5; row < rows_; row++) {
drawCharEveryNth(row, vertical[0]);
}
drawCharEveryNth(rows_, upT[0]);
// Draw the headers
const std::array<std::string, 6> headers = {
"Item", "Last (ms)", "Min (ms)", "Max (ms)", "Avg (ms)", "Hz"};
auto drawStringsInColumns = [&](const std::array<std::string, 6>& strings, const int row) {
for (size_t i = 0; i < strings.size(); i++) {
const int startingX = gapBetweenColumns * i + 2;
const int endingX = (i == strings.size() - 1 ? cols_ : gapBetweenColumns * (i + 1) + 1);
const int leftPadding = std::max(0, endingX - startingX - (int)strings[i].length());
for (int c = 0; c < (int)strings[i].length() && c < gapBetweenColumns - 1; c++) {
if (leftPadding + startingX + c > 0) {
window_->set_char(
leftPadding + startingX + c,
row,
Term::Private::utf8_to_utf32(std::string(1, strings[i][c]))[0]);
}
}
}
};
drawStringsInColumns(headers, 3);
// Populate the timing strings
std::vector<std::array<std::string, 6>> timings;
for (const auto& [signature, stopwatch] : cache) {
for (const auto& [name, measurements] : stopwatch) {
auto& timing = timings.emplace_back();
timing[0] = name;
timing[1] = QString::number(measurements.first[0], 'f', 3).toStdString();
timing[2] = QString::number(measurements.first.getMinimum(), 'f', 3).toStdString();
timing[3] = QString::number(measurements.first.getMaximum(), 'f', 3).toStdString();
timing[4] = QString::number(measurements.first.getAverage(), 'f', 3).toStdString();
timing[5] =
QString::number(measurements.first.getReciprocal() * 1000.0, 'f', 3).toStdString();
}
}
for (int row = 5, timingIdx = scroll_; row < rows_ && timingIdx < (int)timings.size();
row++, timingIdx++) {
if (timingIdx == 0) {
firstRowVisible = true;
}
if (timingIdx == (int)timings.size() - 1) {
lastRowVisible = true;
}
drawStringsInColumns(timings[timingIdx], row);
}
if (!firstRowVisible && !lastRowVisible) {
window_->set_char(cols_ - 1, rows_, upDownArrow[0]);
window_->set_char(cols_, rows_, Term::Private::utf8_to_utf32(std::string(1, ' '))[0]);
} else if (!firstRowVisible) {
window_->set_char(cols_ - 1, rows_, upArrow[0]);
window_->set_char(cols_, rows_, Term::Private::utf8_to_utf32(std::string(1, ' '))[0]);
} else if (!lastRowVisible) {
window_->set_char(cols_ - 1, rows_, downArrow[0]);
window_->set_char(cols_, rows_, Term::Private::utf8_to_utf32(std::string(1, ' '))[0]);
}
}
std::cout << window_->render(1, 1, true) << std::flush;
const int key = Term::read_key0();
bool keepRunning = true;
bool flushCache = false;
switch (key) {
case 'k':
case Term::Key::ARROW_UP:
if (!firstRowVisible)
scroll_--;
break;
case 'j':
case Term::Key::ARROW_DOWN:
if (!lastRowVisible)
scroll_++;
break;
case 'c':
flushCache = true;
break;
case 'q':
case Term::Key::ESC:
case Term::Key::CTRL + 'c':
keepRunning = false;
break;
}
return std::make_pair(keepRunning, flushCache);
}