Skip to content

Commit

Permalink
Merge pull request #95 from aschnell/master
Browse files Browse the repository at this point in the history
- mark inactive mount points
  • Loading branch information
aschnell authored Mar 15, 2024
2 parents 470596b + 58e5705 commit adfd103
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .codespellrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Ref: https://github.com/codespell-project/codespell#using-a-config-file
[codespell]
skip = po
106 changes: 96 additions & 10 deletions barrel/Utils/Table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,18 @@ namespace barrel
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);
void calculate_trims(const Table& table, const Table::Row& row);
void calculate_widths(const Table& table, const Table::Row& row, bool is_header, unsigned indent);
size_t calculate_total_width(const Table& table) const;
void calculate_abbriviated_widths(const Table& table);

string trimmed(const string& s, Align align, size_t trim) const;

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

vector<ColumnVars> column_vars;
Expand All @@ -70,16 +74,21 @@ namespace barrel
for (const Table::Row& row : table.rows)
calculate_hidden(table, row);

// calculate trims

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

// calculate 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);
calculate_widths(table, table.header, true, 0);

for (const Table::Row& row : table.rows)
calculate_widths(table, row, 0);
calculate_widths(table, row, false, 0);

calculate_abbriviated_widths(table);
}
Expand All @@ -105,7 +114,67 @@ namespace barrel


void
Table::OutputInfo::calculate_widths(const Table& table, const Table::Row& row, unsigned indent)
Table::OutputInfo::calculate_trims(const Table& table, const Table::Row& row)
{
const vector<string>& columns = row.get_columns();

for (size_t idx = 0; idx < columns.size(); ++idx)
{
if (!table.column_params[idx].trim || column_vars[idx].hidden)
continue;

if (column_vars[idx].trim == 0)
continue;

const string& column = columns[idx];
if (column.empty())
continue;

size_t trim = 0;

switch (table.column_params[idx].align)
{
case Align::RIGHT:
trim = column.size() - column.find_last_not_of(" ") - 1;
break;

case Align::LEFT:
trim = column.find_first_not_of(" ");
break;
}

column_vars[idx].trim = min(column_vars[idx].trim, trim);
}

for (const Table::Row& subrow : row.get_subrows())
calculate_trims(table, subrow);
}


string
Table::OutputInfo::trimmed(const string& s, Align align, size_t trim) const
{
string ret = s;

switch (align)
{
case Align::RIGHT:
if (ret.size() >= trim)
ret.erase(ret.size() - trim, trim);
break;

case Align::LEFT:
if (ret.size() >= trim)
ret.erase(0, trim);
break;
}

return ret;
}


void
Table::OutputInfo::calculate_widths(const Table& table, const Table::Row& row, bool is_header, unsigned indent)
{
const vector<string>& columns = row.get_columns();

Expand All @@ -114,7 +183,12 @@ namespace barrel
if (column_vars[idx].hidden)
continue;

size_t width = mbs_width(columns[idx]);
string column = columns[idx];

if (!is_header && table.column_params[idx].trim)
column = trimmed(column, table.column_params[idx].align, column_vars[idx].trim);

size_t width = mbs_width(column);

if (idx == table.tree_idx)
width += 2 * indent;
Expand All @@ -123,7 +197,7 @@ namespace barrel
}

for (const Table::Row& subrow : row.get_subrows())
calculate_widths(table, subrow, indent + 1);
calculate_widths(table, subrow, false, indent + 1);
}


Expand Down Expand Up @@ -179,7 +253,8 @@ namespace barrel


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

Expand All @@ -192,6 +267,9 @@ namespace barrel

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

if (!is_header && column_params[idx].trim)
column = output_info.trimmed(column, column_params[idx].align, output_info.column_vars[idx].trim);

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

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

Expand Down Expand Up @@ -370,6 +448,14 @@ namespace barrel
}


void
Table::set_trim(Id id, bool trim)
{
size_t idx = id_to_idx(id);
column_params[idx].trim = trim;
}


void
Table::set_tree_id(Id id)
{
Expand All @@ -387,13 +473,13 @@ namespace barrel
// output header and rows

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

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

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

return s;
}
Expand Down
11 changes: 10 additions & 1 deletion barrel/Utils/Table.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,17 @@ namespace barrel
void set_style(Style style) { Table::style = style; }
void set_global_indent(size_t global_indent) { Table::global_indent = global_indent; }
void set_screen_width(size_t screen_width) { Table::screen_width = screen_width; }

void set_min_width(Id id, size_t min_width);
void set_visibility(Id id, Visibility visibility);
void set_abbreviate(Id id, bool abbreviate);

/**
* Allow to trim a column without breaking alignment by removing the same amount of
* spaces from all rols (excluding the header).
*/
void set_trim(Id id, bool trim);

void set_tree_id(Id id);

friend std::ostream& operator<<(std::ostream& s, const Table& Table);
Expand Down Expand Up @@ -179,6 +187,7 @@ namespace barrel
size_t min_width = 0;
Visibility visibility = Visibility::ON;
bool abbreviate = false;
bool trim = false;
};

vector<ColumnParams> column_params;
Expand All @@ -187,7 +196,7 @@ namespace barrel

struct OutputInfo;

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

void output(std::ostream& s, const OutputInfo& output_info) const;
Expand Down
3 changes: 3 additions & 0 deletions barrel/show-filesystems.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ namespace barrel
tmp = "[" + rootprefix + "] " + tmp;
}

if (!mount_point->is_active())
tmp += " !";

row[Id::MOUNT_POINT] = tmp;
}

Expand Down
5 changes: 5 additions & 0 deletions package/barrel.changes
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
-------------------------------------------------------------------
Fri Mar 15 09:21:52 CET 2024 - [email protected]

- mark inactive mount points

-------------------------------------------------------------------
Mon Feb 05 11:23:57 CET 2024 - [email protected]

Expand Down
2 changes: 1 addition & 1 deletion testsuite/real1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@
<rootprefixed>true</rootprefixed>
<mount-by>uuid</mount-by>
<mount-type>swap</mount-type>
<active>true</active>
<active>false</active>
<in-etc-fstab>true</in-etc-fstab>
<freq>0</freq>
<passno>0</passno>
Expand Down
2 changes: 1 addition & 1 deletion testsuite/show1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ BOOST_AUTO_TEST_CASE(test5)
"─────┼───────┼────────────────┼───────────┼────────────",
"ext4 │ │ /dev/sda2 │ 29.99 GiB │ /",
"xfs │ │ /dev/data/home │ 20.00 GiB │ /home",
"swap │ │ /dev/sda3 │ 2.00 GiB │ swap"
"swap │ │ /dev/sda3 │ 2.00 GiB │ swap !"
};

Testsuite testsuite;
Expand Down

0 comments on commit adfd103

Please sign in to comment.