1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-12-08 18:11:02 +01:00

Merge pull request #14701 from NixOS/print-table

Move printTable() into libutil
This commit is contained in:
John Ericson 2025-12-03 18:49:51 +00:00 committed by GitHub
commit c7801fc347
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 54 additions and 34 deletions

View file

@ -74,6 +74,7 @@ headers = files(
'strings.hh',
'suggestions.hh',
'sync.hh',
'table.hh',
'tarfile.hh',
'terminal.hh',
'thread-pool.hh',

View file

@ -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

View file

@ -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',

38
src/libutil/table.cc Normal file
View file

@ -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

View file

@ -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<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
@ -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;
@ -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)