mirror of
https://github.com/NixOS/nix.git
synced 2025-12-15 13:31:05 +01:00
Move table stuff into libutil
This commit is contained in:
parent
96d8b54e42
commit
863f6811e4
5 changed files with 56 additions and 32 deletions
40
src/libutil/table.cc
Normal file
40
src/libutil/table.cc
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#include "nix/util/table.hh"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
namespace nix {
|
||||
|
||||
void printTable(Table & table)
|
||||
{
|
||||
auto nrColumns = table.size() > 0 ? table.front().size() : 0;
|
||||
|
||||
std::vector<size_t> widths;
|
||||
widths.resize(nrColumns);
|
||||
|
||||
for (auto & i : table) {
|
||||
assert(i.size() == nrColumns);
|
||||
Strings::iterator j;
|
||||
size_t column;
|
||||
for (j = i.begin(), column = 0; j != i.end(); ++j, ++column)
|
||||
if (j->size() > widths[column])
|
||||
widths[column] = j->size();
|
||||
}
|
||||
|
||||
for (auto & i : table) {
|
||||
Strings::iterator j;
|
||||
size_t column;
|
||||
for (j = i.begin(), column = 0; j != i.end(); ++j, ++column) {
|
||||
std::string s = *j;
|
||||
replace(s.begin(), s.end(), '\n', ' ');
|
||||
std::cout << s;
|
||||
if (column < nrColumns - 1)
|
||||
std::cout << std::string(widths[column] - s.size() + 2, ' ');
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace nix
|
||||
Loading…
Add table
Add a link
Reference in a new issue