-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Print CGO table (backported from Incentive)
- Loading branch information
1 parent
878cdd5
commit 9cefa38
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <iostream> | ||
#include <iomanip> | ||
#include <algorithm> | ||
#include "PrintUtils.h" | ||
|
||
display_table_t & display_table_t::begin_row() { | ||
_table.push_back(std::vector<std::string>()); | ||
_current_row++; | ||
return *this; | ||
} | ||
|
||
void display_table_t::display() { | ||
// Dimensions of tables | ||
const size_t num_rows = _table.size(); | ||
const size_t num_cols = ([&] () { | ||
size_t largest = 0; | ||
for (auto & v : _table) { | ||
largest = std::max(largest, v.size()); | ||
} | ||
return largest; | ||
})(); | ||
|
||
// Pad | ||
for (auto & row : _table) { | ||
while (row.size() < num_cols) { | ||
row.emplace_back(" "); | ||
} | ||
} | ||
|
||
// Get the largest string for each col | ||
std::vector<size_t> col_sizes(num_cols); | ||
for (size_t j = 0; j < num_cols; ++j) { | ||
size_t largest = 0; | ||
for (size_t i = 0; i < num_rows; ++i) { | ||
largest = std::max(largest, _table[i][j].size()); | ||
} | ||
col_sizes[j] = largest; | ||
} | ||
|
||
|
||
// construct the output | ||
std::stringstream ss; | ||
ss << std::left; | ||
auto insert_hr = [&]() { | ||
for (size_t j = 0; j < num_cols; ++j) { | ||
ss << std::setw(col_sizes[j] + 3) | ||
<< std::setfill('-') << "+"; | ||
} | ||
ss << "+" << std::setfill(' ') << std::endl; | ||
}; | ||
{ | ||
int i = 0, j = 0; | ||
for (auto & row : _table) { | ||
insert_hr(); | ||
ss << "| "; | ||
for (auto & col : row) { | ||
ss << std::setw(col_sizes[j]) | ||
<< col << " | "; | ||
j++; | ||
} | ||
ss << std::endl; | ||
i++; j = 0; | ||
} | ||
insert_hr(); | ||
} | ||
std::cout << ss.str(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
#include <type_traits> | ||
#include <vector> | ||
#include <string> | ||
#include <sstream> | ||
|
||
/*********************************************************************** | ||
* Table pretty printing utility class | ||
* --------------------------------------------------------------------- | ||
* Will autofill to the row with the largest column | ||
***********************************************************************/ | ||
class display_table_t { | ||
public: | ||
// both begins and ends a row | ||
display_table_t & begin_row(); | ||
|
||
// inserts a new cell with some printable type in it | ||
template <typename T> | ||
display_table_t & insert_cell(T s) { | ||
std::stringstream ss; | ||
ss << s; | ||
_table[_current_row].emplace_back(ss.str()); | ||
return *this; | ||
} | ||
|
||
// finalizes the table and prints it to std out | ||
void display(); | ||
private: | ||
int _current_row { -1 }; | ||
std::vector<std::vector<std::string>> _table; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters