From 863f6811e42196ce2a3b0de04747b6d1b7b4f540 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 1 Dec 2025 20:09:05 +0100 Subject: [PATCH 1/3] Move table stuff into libutil --- src/libutil/include/nix/util/meson.build | 1 + src/libutil/include/nix/util/table.hh | 13 ++++++++ src/libutil/meson.build | 1 + src/libutil/table.cc | 40 ++++++++++++++++++++++++ src/nix/nix-env/nix-env.cc | 33 +------------------ 5 files changed, 56 insertions(+), 32 deletions(-) create mode 100644 src/libutil/include/nix/util/table.hh create mode 100644 src/libutil/table.cc 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..07abe1bb7 --- /dev/null +++ b/src/libutil/include/nix/util/table.hh @@ -0,0 +1,13 @@ +#pragma once + +#include "nix/util/types.hh" + +#include + +namespace nix { + +typedef std::list Table; + +void printTable(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..a6fc09798 --- /dev/null +++ b/src/libutil/table.cc @@ -0,0 +1,40 @@ +#include "nix/util/table.hh" + +#include +#include +#include +#include + +namespace nix { + +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', ' '); + std::cout << s; + if (column < nrColumns - 1) + std::cout << std::string(widths[column] - s.size() + 2, ' '); + } + std::cout << std::endl; + } +} + +} // namespace nix diff --git a/src/nix/nix-env/nix-env.cc b/src/nix/nix-env/nix-env.cc index edfccffa6..f544e0c5a 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 From ab6dcf20473f4ea998650f542e94db1d9a609629 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 1 Dec 2025 20:12:15 +0100 Subject: [PATCH 2/3] Table: Use std::vectors --- src/libutil/include/nix/util/table.hh | 4 +--- src/libutil/table.cc | 10 ++++------ src/nix/nix-env/nix-env.cc | 2 +- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/libutil/include/nix/util/table.hh b/src/libutil/include/nix/util/table.hh index 07abe1bb7..c42cc1d8f 100644 --- a/src/libutil/include/nix/util/table.hh +++ b/src/libutil/include/nix/util/table.hh @@ -2,11 +2,9 @@ #include "nix/util/types.hh" -#include - namespace nix { -typedef std::list Table; +typedef std::vector> Table; void printTable(Table & table); diff --git a/src/libutil/table.cc b/src/libutil/table.cc index a6fc09798..a64c37315 100644 --- a/src/libutil/table.cc +++ b/src/libutil/table.cc @@ -16,17 +16,15 @@ void printTable(Table & table) 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) + 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) { - Strings::iterator j; - size_t column; - for (j = i.begin(), column = 0; j != i.end(); ++j, ++column) { + size_t column = 0; + for (auto j = i.begin(); j != i.end(); ++j, ++column) { std::string s = *j; replace(s.begin(), s.end(), '\n', ' '); std::cout << s; diff --git a/src/nix/nix-env/nix-env.cc b/src/nix/nix-env/nix-env.cc index f544e0c5a..aa5fa23c4 100644 --- a/src/nix/nix-env/nix-env.cc +++ b/src/nix/nix-env/nix-env.cc @@ -1062,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; From c4dc42f306bbc7498daa936ed8a96f00d4dd9815 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 1 Dec 2025 20:15:23 +0100 Subject: [PATCH 3/3] printTable(): Make destination stream explicit --- src/libutil/include/nix/util/table.hh | 2 +- src/libutil/table.cc | 8 ++++---- src/nix/nix-env/nix-env.cc | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/libutil/include/nix/util/table.hh b/src/libutil/include/nix/util/table.hh index c42cc1d8f..13e4506d5 100644 --- a/src/libutil/include/nix/util/table.hh +++ b/src/libutil/include/nix/util/table.hh @@ -6,6 +6,6 @@ namespace nix { typedef std::vector> Table; -void printTable(Table & table); +void printTable(std::ostream & out, Table & table); } // namespace nix diff --git a/src/libutil/table.cc b/src/libutil/table.cc index a64c37315..fa1bf110d 100644 --- a/src/libutil/table.cc +++ b/src/libutil/table.cc @@ -7,7 +7,7 @@ namespace nix { -void printTable(Table & table) +void printTable(std::ostream & out, Table & table) { auto nrColumns = table.size() > 0 ? table.front().size() : 0; @@ -27,11 +27,11 @@ void printTable(Table & table) for (auto j = i.begin(); j != i.end(); ++j, ++column) { std::string s = *j; replace(s.begin(), s.end(), '\n', ' '); - std::cout << s; + out << s; if (column < nrColumns - 1) - std::cout << std::string(widths[column] - s.size() + 2, ' '); + out << std::string(widths[column] - s.size() + 2, ' '); } - std::cout << std::endl; + out << std::endl; } } diff --git a/src/nix/nix-env/nix-env.cc b/src/nix/nix-env/nix-env.cc index aa5fa23c4..1c09c1e45 100644 --- a/src/nix/nix-env/nix-env.cc +++ b/src/nix/nix-env/nix-env.cc @@ -1250,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)