Skip to content

Commit

Permalink
Merge pull request #94 from aschnell/master
Browse files Browse the repository at this point in the history
- restructured data structures
  • Loading branch information
aschnell authored Mar 7, 2024
2 parents 611991f + e3b63d6 commit 470596b
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 97 deletions.
170 changes: 95 additions & 75 deletions barrel/Utils/Table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,47 @@ namespace barrel
using namespace std;


struct Table::OutputInfo
{
OutputInfo(const Table& table);

void calculate_hidden(const Table& table, const Table::Row& row);
void calculate_widths(const Table& table, const Table::Row& row, unsigned indent);
size_t calculate_total_width(const Table& table) const;
void calculate_abbriviated_widths(const Table& table);

struct ColumnVars
{
bool hidden = false;
size_t width = 0;
};

vector<ColumnVars> column_vars;
};


Table::OutputInfo::OutputInfo(const Table& table)
{
// calculate hidden, default to false
if (table.column_params.size() != table.header.get_columns().size())
throw runtime_error("column_params.size != header.size");

hidden.resize(table.header.get_columns().size());
column_vars.resize(table.header.get_columns().size());

for (size_t i = 0; i < table.visibilities.size(); ++i)
// calculate hidden, default to false

for (size_t idx = 0; idx < table.column_params.size(); ++idx)
{
if (table.visibilities[i] == Visibility::AUTO || table.visibilities[i] == Visibility::OFF)
hidden[i] = true;
if (table.column_params[idx].visibility == Visibility::AUTO || table.column_params[idx].visibility == Visibility::OFF)
column_vars[idx].hidden = true;
}

for (const Table::Row& row : table.rows)
calculate_hidden(table, row);

// calculate widths

widths = table.min_widths;
for (size_t idx = 0; idx < table.column_params.size(); ++idx)
column_vars[idx].width = table.column_params[idx].min_width;

if (table.show_header)
calculate_widths(table, table.header, 0);
Expand All @@ -67,10 +90,13 @@ namespace barrel
{
const vector<string>& columns = row.get_columns();

for (size_t i = 0; i < min(columns.size(), table.visibilities.size()); ++i)
if (columns.size() > table.column_params.size())
throw runtime_error("too many columns");

for (size_t idx = 0; idx < columns.size(); ++idx)
{
if (table.visibilities[i] == Visibility::AUTO && !columns[i].empty())
hidden[i] = false;
if (table.column_params[idx].visibility == Visibility::AUTO && !columns[idx].empty())
column_vars[idx].hidden = false;
}

for (const Table::Row& subrow : row.get_subrows())
Expand All @@ -83,20 +109,17 @@ namespace barrel
{
const vector<string>& columns = row.get_columns();

if (columns.size() > widths.size())
widths.resize(columns.size());

for (size_t i = 0; i < columns.size(); ++i)
for (size_t idx = 0; idx < columns.size(); ++idx)
{
if (hidden[i])
if (column_vars[idx].hidden)
continue;

size_t width = mbs_width(columns[i]);
size_t width = mbs_width(columns[idx]);

if (i == table.tree_index)
if (idx == table.tree_idx)
width += 2 * indent;

widths[i] = max(widths[i], width);
column_vars[idx].width = max(column_vars[idx].width, width);
}

for (const Table::Row& subrow : row.get_subrows())
Expand All @@ -114,17 +137,17 @@ namespace barrel

bool first = true;

for (size_t i = 0; i < widths.size(); ++i)
for (size_t idx = 0; idx < column_vars.size(); ++idx)
{
if (hidden[i])
if (column_vars[idx].hidden)
continue;

if (first)
first = false;
else
total_width += 2 + (table.show_grid ? 1 : 0);

total_width += widths[i];
total_width += column_vars[idx].width;
}

return total_width;
Expand All @@ -144,43 +167,43 @@ namespace barrel

size_t too_much = total_width - table.screen_width;

for (size_t i = 0; i < table.abbreviates.size(); ++i)
for (size_t idx = 0; idx < table.column_params.size(); ++idx)
{
if (table.abbreviates[i])
if (table.column_params[idx].abbreviate)
{
widths[i] = max(widths[i] - too_much, (size_t) 5);
column_vars[idx].width = max(column_vars[idx].width - too_much, (size_t) 5);
break;
}
}
}


void
Table::output(std::ostream& s, const Table::Row& row, const OutputInfo& output_info, const vector<bool>& lasts) const
Table::output(std::ostream& s, const OutputInfo& output_info, const Table::Row& row, const vector<bool>& lasts) const
{
s << string(global_indent, ' ');

const vector<string>& columns = row.get_columns();

for (size_t i = 0; i < output_info.widths.size(); ++i)
for (size_t idx = 0; idx < output_info.column_vars.size(); ++idx)
{
if (output_info.hidden[i])
if (output_info.column_vars[idx].hidden)
continue;

string column = i < columns.size() ? columns[i] : "";
string column = idx < columns.size() ? columns[idx] : "";

bool first = i == 0;
bool last = i == output_info.widths.size() - 1;
bool first = idx == 0;
bool last = idx == output_info.column_vars.size() - 1;

size_t extra = (i == tree_index) ? 2 * lasts.size() : 0;
size_t extra = (idx == tree_idx) ? 2 * lasts.size() : 0;

if (last && column.empty())
break;

if (!first)
s << " ";

if (i == tree_index)
if (idx == tree_idx)
{
for (size_t tl = 0; tl < lasts.size(); ++tl)
{
Expand All @@ -193,21 +216,21 @@ namespace barrel

size_t width = mbs_width(column);

if (aligns[i] == Align::RIGHT)
if (column_params[idx].align == Align::RIGHT)
{
if (width < output_info.widths[i] - extra)
s << string(output_info.widths[i] - width - extra, ' ');
if (width < output_info.column_vars[idx].width - extra)
s << string(output_info.column_vars[idx].width - width - extra, ' ');
}

s << column;

if (last)
break;

if (aligns[i] == Align::LEFT)
if (column_params[idx].align == Align::LEFT)
{
if (width < output_info.widths[i] - extra)
s << string(output_info.widths[i] - width - extra, ' ');
if (width < output_info.column_vars[idx].width - extra)
s << string(output_info.column_vars[idx].width - width - extra, ' ');
}

s << " ";
Expand All @@ -223,7 +246,7 @@ namespace barrel
{
vector<bool> sub_lasts = lasts;
sub_lasts.push_back(i == subrows.size() - 1);
output(s, subrows[i], output_info, sub_lasts);
output(s, output_info, subrows[i], sub_lasts);
}
}

Expand All @@ -236,15 +259,15 @@ namespace barrel
{
s << string(global_indent, ' ');

for (size_t i = 0; i < output_info.widths.size(); ++i)
for (size_t idx = 0; idx < output_info.column_vars.size(); ++idx)
{
if (output_info.hidden[i])
if (output_info.column_vars[idx].hidden)
continue;

for (size_t j = 0; j < output_info.widths[i]; ++j)
for (size_t j = 0; j < output_info.column_vars[idx].width; ++j)
s << glyph(1);

if (i == output_info.widths.size() - 1)
if (idx == output_info.column_vars.size() - 1)
break;

s << glyph(1) << glyph(2) << glyph(1);
Expand All @@ -254,12 +277,23 @@ namespace barrel
}


bool
Table::has_id(Id id) const
{
for (size_t idx = 0; idx < column_params.size(); ++idx)
if (column_params[idx].id == id)
return true;

return false;
}


size_t
Table::id_to_index(Id id) const
Table::id_to_idx(Id id) const
{
for (size_t i = 0; i < ids.size(); ++i)
if (ids[i] == id)
return i;
for (size_t idx = 0; idx < column_params.size(); ++idx)
if (column_params[idx].id == id)
return idx;

throw runtime_error("id not found");
}
Expand All @@ -268,12 +302,12 @@ namespace barrel
string&
Table::Row::operator[](Id id)
{
size_t i = table.id_to_index(id);
size_t idx = table.id_to_idx(id);

if (columns.size() < i + 1)
columns.resize(i + 1);
if (columns.size() < idx + 1)
columns.resize(idx + 1);

return columns[i];
return columns[idx];
}


Expand All @@ -296,8 +330,7 @@ namespace barrel
for (const Cell& cell : init)
{
header.add(cell.name);
ids.push_back(cell.id);
aligns.push_back(cell.align);
column_params.emplace_back(cell.id, cell.align);
}
}

Expand All @@ -308,52 +341,39 @@ namespace barrel
for (const Cell& cell : init)
{
header.add(cell.name);
ids.push_back(cell.id);
aligns.push_back(cell.align);
column_params.emplace_back(cell.id, cell.align);
}
}


void
Table::set_min_width(Id id, size_t min_width)
{
size_t i = id_to_index(id);

if (min_widths.size() < i + 1)
min_widths.resize(i + 1);

min_widths[i] = min_width;
size_t idx = id_to_idx(id);
column_params[idx].min_width = min_width;
}


void
Table::set_visibility(Id id, Visibility visibility)
{
size_t i = id_to_index(id);

if (visibilities.size() < i + 1)
visibilities.resize(i + 1);

visibilities[i] = visibility;
size_t idx = id_to_idx(id);
column_params[idx].visibility = visibility;
}


void
Table::set_abbreviate(Id id, bool abbreviate)
{
size_t i = id_to_index(id);

if (abbreviates.size() < i + 1)
abbreviates.resize(i + 1);

abbreviates[i] = abbreviate;
size_t idx = id_to_idx(id);
column_params[idx].abbreviate = abbreviate;
}


void
Table::set_tree_id(Id id)
{
tree_index = id_to_index(id);
tree_idx = id_to_idx(id);
}


Expand All @@ -367,13 +387,13 @@ namespace barrel
// output header and rows

if (table.show_header)
table.output(s, table.header, output_info, {});
table.output(s, output_info, table.header, {});

if (table.show_header && table.show_grid)
table.output(s, output_info);

for (const Table::Row& row : table.rows)
table.output(s, row, output_info, {});
table.output(s, output_info, row, {});

return s;
}
Expand Down
Loading

0 comments on commit 470596b

Please sign in to comment.