Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/libutil/include/nix/util/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ headers = files(
'strings.hh',
'suggestions.hh',
'sync.hh',
'table.hh',
'tarfile.hh',
'terminal.hh',
'thread-pool.hh',
Expand Down
11 changes: 11 additions & 0 deletions src/libutil/include/nix/util/table.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once

#include "nix/util/types.hh"

namespace nix {

typedef std::vector<std::vector<std::string>> Table;

void printTable(std::ostream & out, Table & table);

} // namespace nix
1 change: 1 addition & 0 deletions src/libutil/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ sources = [ config_priv_h ] + files(
'source-path.cc',
'strings.cc',
'suggestions.cc',
'table.cc',
'tarfile.cc',
'tee-logger.cc',
'terminal.cc',
Expand Down
38 changes: 38 additions & 0 deletions src/libutil/table.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "nix/util/table.hh"

#include <algorithm>
#include <cassert>
#include <iostream>
#include <vector>

namespace nix {

void printTable(std::ostream & out, 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);
size_t column = 0;
for (auto j = i.begin(); j != i.end(); ++j, ++column)
if (j->size() > widths[column])
widths[column] = j->size();
}

for (auto & i : table) {
size_t column = 0;
for (auto j = i.begin(); j != i.end(); ++j, ++column) {
std::string s = *j;
replace(s.begin(), s.end(), '\n', ' ');
out << s;
if (column < nrColumns - 1)
out << std::string(widths[column] - s.size() + 2, ' ');
}
out << std::endl;
}
}

} // namespace nix
37 changes: 3 additions & 34 deletions src/nix/nix-env/nix-env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "nix/util/xml-writer.hh"
#include "nix/cmd/legacy.hh"
#include "nix/expr/eval-settings.hh" // for defexpr
#include "nix/util/table.hh"
#include "nix/util/terminal.hh"
#include "man-pages.hh"

Expand Down Expand Up @@ -822,38 +823,6 @@ static bool cmpElemByName(const PackageInfo & a, const PackageInfo & b)
return lexicographical_compare(a_name.begin(), a_name.end(), b_name.begin(), b_name.end(), cmpChars);
}

typedef std::list<Strings> Table;

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', ' ');
cout << s;
if (column < nrColumns - 1)
cout << std::string(widths[column] - s.size() + 2, ' ');
}
cout << std::endl;
}
}

/* This function compares the version of an element against the
versions in the given set of elements. `cvLess' means that only
lower versions are in the set, `cvEqual' means that at most an
Expand Down Expand Up @@ -1093,7 +1062,7 @@ static void opQuery(Globals & globals, Strings opFlags, Strings opArgs)
continue;

/* For table output. */
Strings columns;
std::vector<std::string> columns;

/* For XML output. */
XMLAttrs attrs;
Expand Down Expand Up @@ -1281,7 +1250,7 @@ static void opQuery(Globals & globals, Strings opFlags, Strings opArgs)
}

if (!xmlOutput)
printTable(table);
printTable(std::cout, table);
}

static void opSwitchProfile(Globals & globals, Strings opFlags, Strings opArgs)
Expand Down
Loading