MCPcopy Create free account
hub / github.com/p-ranav/tabulate / Table

Class Table

include/tabulate/table.hpp:66–314  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

64enum class BorderStyle { Light, Heavy, Double };
65
66class Table {
67public:
68 Table() : table_(TableInternal::create()) {}
69
70 using Row_t = std::vector<variant<std::string, const char *, string_view, Table>>;
71
72 Table &add_row(const Row_t &cells) {
73
74 if (rows_ == 0) {
75 // This is the first row added
76 // cells.size() is the number of columns
77 cols_ = cells.size();
78 }
79
80 std::vector<std::string> cell_strings;
81 if (cells.size() < cols_) {
82 cell_strings.resize(cols_);
83 std::fill(cell_strings.begin(), cell_strings.end(), "");
84 } else {
85 cell_strings.resize(cells.size());
86 std::fill(cell_strings.begin(), cell_strings.end(), "");
87 }
88
89 std::vector<size_t> nested_table_indices;
90 for (size_t i = 0; i < cells.size(); ++i) {
91 auto cell = cells[i];
92 if (holds_alternative<std::string>(cell)) {
93 cell_strings[i] = *get_if<std::string>(&cell);
94 } else if (holds_alternative<const char *>(cell)) {
95 cell_strings[i] = *get_if<const char *>(&cell);
96 } else if (holds_alternative<string_view>(cell)) {
97 cell_strings[i] = std::string{*get_if<string_view>(&cell)};
98 } else {
99 auto table = *get_if<Table>(&cell);
100 std::stringstream stream;
101 // termcolor emits nothing on a stream that is not a terminal, which
102 // would strip every color the nested table asked for. The destination
103 // is not known until the outer table is printed, so follow std::cout.
104 if (termcolor::_internal::is_colorized(std::cout))
105 stream << termcolor::colorize;
106 table.print(stream);
107 cell_strings[i] = stream.str();
108 nested_table_indices.push_back(i);
109 }
110 }
111
112 table_->add_row(cell_strings);
113 // A nested table's text is pre-formatted ASCII art: trimming would corrupt its layout.
114 for (auto i : nested_table_indices)
115 row(rows_)[i].format().trim_mode(Format::TrimMode::kNone);
116 rows_ += 1;
117 return *this;
118 }
119
120 Row &operator[](size_t index) { return row(index); }
121
122 Row &row(size_t index) { return (*table_)[index]; }
123

Callers

nothing calls this directly

Calls 9

is_colorizedFunction · 0.70
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
printMethod · 0.45
strMethod · 0.45
add_rowMethod · 0.45
formatMethod · 0.45
erase_columnMethod · 0.45

Tested by

no test coverage detected