Separate a helper function from histogram code

sql-rework
Tomasz Golinski 2020-05-03 00:19:59 +02:00
rodzic fdd7652e52
commit ad30216a7f
1 zmienionych plików z 14 dodań i 10 usunięć

Wyświetl plik

@ -34,6 +34,18 @@ void htmlencode(std::string& data) {
data.swap(tmp);
}
static std::string string_mangle(const std::string& str) {
std::string tmp;
tmp.reserve(str.size());
for (size_t i = 0; i != str.size(); i++) {
if (str[i] == ' ') continue;
if (std::isupper(str[i])) tmp.push_back(std::tolower(str[i]));
else tmp.push_back(str[i]);
}
return tmp;
}
void show_histogram(const Caches& cc, std::string Cache::*ptr, const std::string& caption, bool html, bool sort_by_val) {
int HIST_MAX = 20;
@ -52,14 +64,6 @@ void show_histogram(const Caches& cc, std::string Cache::*ptr, const std::string
sort(pairs.begin(), pairs.end(), [&](std::pair<std::string, int>& a, std::pair<std::string, int>& b) { return a.first < b.first; });
if (html) {
std::string id;
id.reserve(caption.size());
for (size_t i = 0; i != caption.size(); i++) {
if (caption[i] == ' ') continue;
if (std::isupper(caption[i])) id.push_back(std::tolower(caption[i]));
else id.push_back(caption[i]);
}
int max;
if (sort_by_val)
max = pairs[0].second;
@ -67,7 +71,7 @@ void show_histogram(const Caches& cc, std::string Cache::*ptr, const std::string
max = std::max_element(pairs.begin(), pairs.end(), [&](std::pair<std::string, int>& a, std::pair<std::string, int>& b) { return a.second < b.second; })->second;
int i = 0;
std::cout << "<h2>" << caption << "</h2>\n";
std::cout << "<div class=\"histogram " << id << "\">\n";
std::cout << "<div class=\"histogram " << string_mangle(caption) << "\">\n";
for (auto own : pairs) {
htmlencode(own.first);
if (own.first.empty()) own.first = "[unknown]";
@ -76,7 +80,7 @@ void show_histogram(const Caches& cc, std::string Cache::*ptr, const std::string
std::cout << "<div class=\"bar\" style=\"--percent: " << 100 * own.second / max << "%;\"><span class=\"text\">" << own.first << ": " << own.second << "</span></div>\n";
else if (i == HIST_MAX) {
std::cout << "</div>\n";
std::cout << "<details class=\"histogram_others " << id << "\">\n<summary>See more</summary>\n<p>\n";
std::cout << "<details class=\"histogram_others " << string_mangle(caption) << "\">\n<summary>See more</summary>\n<p>\n";
std::cout << own.first << " (" << own.second << ")";
}
if (i > HIST_MAX)