diff --git a/src/libutil/include/nix/util/meson.build b/src/libutil/include/nix/util/meson.build index b6677140e..45b52ff5e 100644 --- a/src/libutil/include/nix/util/meson.build +++ b/src/libutil/include/nix/util/meson.build @@ -74,6 +74,7 @@ headers = files( 'strings.hh', 'suggestions.hh', 'sync.hh', + 'table.hh', 'tarfile.hh', 'terminal.hh', 'thread-pool.hh', diff --git a/src/libutil/include/nix/util/table.hh b/src/libutil/include/nix/util/table.hh new file mode 100644 index 000000000..13e4506d5 --- /dev/null +++ b/src/libutil/include/nix/util/table.hh @@ -0,0 +1,11 @@ +#pragma once + +#include "nix/util/types.hh" + +namespace nix { + +typedef std::vector> Table; + +void printTable(std::ostream & out, Table & table); + +} // namespace nix diff --git a/src/libutil/meson.build b/src/libutil/meson.build index 53a346afb..0b4a0841f 100644 --- a/src/libutil/meson.build +++ b/src/libutil/meson.build @@ -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', diff --git a/src/libutil/table.cc b/src/libutil/table.cc new file mode 100644 index 000000000..fa1bf110d --- /dev/null +++ b/src/libutil/table.cc @@ -0,0 +1,38 @@ +#include "nix/util/table.hh" + +#include +#include +#include +#include + +namespace nix { + +void printTable(std::ostream & out, Table & table) +{ + auto nrColumns = table.size() > 0 ? table.front().size() : 0; + + std::vector 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 diff --git a/src/nix/nix-env/nix-env.cc b/src/nix/nix-env/nix-env.cc index edfccffa6..1c09c1e45 100644 --- a/src/nix/nix-env/nix-env.cc +++ b/src/nix/nix-env/nix-env.cc @@ -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" @@ -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 Table; - -void printTable(Table & table) -{ - auto nrColumns = table.size() > 0 ? table.front().size() : 0; - - std::vector 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 @@ -1093,7 +1062,7 @@ static void opQuery(Globals & globals, Strings opFlags, Strings opArgs) continue; /* For table output. */ - Strings columns; + std::vector columns; /* For XML output. */ XMLAttrs attrs; @@ -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)