summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2004-07-01 13:35:10 +0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2004-07-01 13:35:10 +0000
commit8f6254e8234c5f4a16b1b7d632065ebb0162b307 (patch)
treed2c507511704ad19aea467fe25a39ada5143e389 /src
parent593bc23d8b310b32b66227b36b72fd525087df83 (diff)
downloadguix-8f6254e8234c5f4a16b1b7d632065ebb0162b307.tar.gz
* Align the columns in the output of `nix-env -q'.
Diffstat (limited to 'src')
-rw-r--r--src/nix-env/main.cc40
1 files changed, 36 insertions, 4 deletions
diff --git a/src/nix-env/main.cc b/src/nix-env/main.cc
index fa9e3d17ef..a80f882dec 100644
--- a/src/nix-env/main.cc
+++ b/src/nix-env/main.cc
@@ -460,6 +460,36 @@ static bool cmpDrvByName(const DrvInfo & a, const DrvInfo & b)
 }
 
 
+typedef list<Strings> Table;
+
+
+void printTable(Table & table)
+{
+    int nrColumns = table.front().size();
+    
+    vector<int> widths;
+    widths.resize(nrColumns);
+    
+    for (Table::iterator i = table.begin(); i != table.end(); ++i) {
+        assert(i->size() == nrColumns);
+        Strings::iterator j; int column;
+        for (j = i->begin(), column = 0; j != i->end(); ++j, ++column)
+            if (j->size() > widths[column]) widths[column] = j->size();
+    }
+
+    for (Table::iterator i = table.begin(); i != table.end(); ++i) { 
+        Strings::iterator j; int column;
+        for (j = i->begin(), column = 0; j != i->end(); ++j, ++column)
+        {
+            cout << *j;
+            if (column < nrColumns - 1)
+                cout << string(widths[column] - j->size() + 2, ' ');
+        }
+        cout << endl;
+    }
+}
+
+
 static void opQuery(Globals & globals,
     Strings opFlags, Strings opArgs)
 {
@@ -519,6 +549,8 @@ static void opQuery(Globals & globals,
     }
             
     /* Print the desired columns. */
+    Table table;
+    
     for (DrvInfoList::iterator i = drvs2.begin(); i != drvs2.end(); ++i) {
 
         Strings columns;
@@ -537,11 +569,11 @@ static void opQuery(Globals & globals,
         if (printSystem) columns.push_back(i->system);
 
         if (printDrvPath) columns.push_back(i->drvPath);
-        
-        for (Strings::iterator j = columns.begin(); j != columns.end(); ++j)
-            cout << *j << " ";
-        cout << endl;
+
+        table.push_back(columns);
     }
+
+    printTable(table);
 }