about summary refs log tree commit diff
path: root/nix-repl.cc
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2013-09-09T09·14+0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2013-09-09T09·14+0200
commite587aec1235d7834d04d6d9f7997bc010ef99925 (patch)
tree2811405634111a69c2e1bd949569407518dd36c6 /nix-repl.cc
parent7e3625f924825db2ffa5e58d4b414d93d9af2465 (diff)
printValue: Don't show lists/attribute sets twice
Diffstat (limited to 'nix-repl.cc')
-rw-r--r--nix-repl.cc32
1 files changed, 26 insertions, 6 deletions
diff --git a/nix-repl.cc b/nix-repl.cc
index d2179d77a8e2..a6e5f78ec9ce 100644
--- a/nix-repl.cc
+++ b/nix-repl.cc
@@ -45,7 +45,10 @@ struct NixRepl
     void addVarToScope(const Symbol & name, Value * v);
     Expr * parseString(string s);
     void evalString(string s, Value & v);
+
+    typedef set<Value *> ValuesSeen;
     std::ostream &  printValue(std::ostream & str, Value & v, unsigned int maxDepth);
+    std::ostream &  printValue(std::ostream & str, Value & v, unsigned int maxDepth, ValuesSeen & seen);
 };
 
 
@@ -315,8 +318,15 @@ void NixRepl::evalString(string s, Value & v)
 }
 
 
+std::ostream & NixRepl::printValue(std::ostream & str, Value & v, unsigned int maxDepth)
+{
+    ValuesSeen seen;
+    return printValue(str, v, maxDepth, seen);
+}
+
+
 // FIXME: lot of cut&paste from Nix's eval.cc.
-std::ostream &  NixRepl::printValue(std::ostream & str, Value & v, unsigned int maxDepth)
+std::ostream & NixRepl::printValue(std::ostream & str, Value & v, unsigned int maxDepth, ValuesSeen & seen)
 {
     str.flush();
     checkInterrupt();
@@ -353,6 +363,8 @@ std::ostream &  NixRepl::printValue(std::ostream & str, Value & v, unsigned int
         break;
 
     case tAttrs: {
+        seen.insert(&v);
+
         bool isDrv = state.isDerivation(v);
         if (isDrv) str << "(derivation ";
         str << "{ ";
@@ -379,10 +391,12 @@ std::ostream &  NixRepl::printValue(std::ostream & str, Value & v, unsigned int
             }
 
             foreach (Sorted::iterator, i, sorted)
-                if (hidden.find(i->first) == hidden.end())
-                    printValue(str << i->first << " = ", *i->second, maxDepth - 1) << "; ";
+                if (hidden.find(i->first) != hidden.end())
+                    str << i->first << " = «...»; ";
+                else if (seen.find(i->second) != seen.end())
+                    str << i->first << " = «repeated»; ";
                 else
-                    str << i->first << " = ...; ";
+                    printValue(str << i->first << " = ", *i->second, maxDepth - 1, seen) << "; ";
 
         } else
             str << "... ";
@@ -393,10 +407,16 @@ std::ostream &  NixRepl::printValue(std::ostream & str, Value & v, unsigned int
     }
 
     case tList:
+        seen.insert(&v);
+
         str << "[ ";
         if (maxDepth > 0)
-            for (unsigned int n = 0; n < v.list.length; ++n)
-                printValue(str, *v.list.elems[n], maxDepth - 1) << " ";
+            for (unsigned int n = 0; n < v.list.length; ++n) {
+                if (seen.find(v.list.elems[n]) != seen.end())
+                    str << "«repeated» ";
+                else
+                    printValue(str, *v.list.elems[n], maxDepth - 1, seen) << " ";
+            }
         else
             str << "... ";
         str << "]";