diff mercurial/utils/stringutil.py @ 38273:f3033692ccef

stringutil: promote smartset.prettyformat() to utility function It will be used by debugwalk.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 10 Jun 2018 11:50:09 +0900
parents 32bc3815efae
children fbb2eddea4d2
line wrap: on
line diff
--- a/mercurial/utils/stringutil.py	Fri Jun 01 17:27:58 2018 +0200
+++ b/mercurial/utils/stringutil.py	Sun Jun 10 11:50:09 2018 +0900
@@ -45,6 +45,21 @@
     else:
         return pycompat.byterepr(o)
 
+def prettyrepr(o):
+    """Pretty print a representation of a possibly-nested object"""
+    lines = []
+    rs = pycompat.byterepr(o)
+    p = 0
+    while p < len(rs):
+        q = rs.find('<', p + 1)
+        if q < 0:
+            q = len(rs)
+        l = rs.count('<', 0, p) - rs.count('>', 0, p)
+        assert l >= 0
+        lines.append((l, rs[p:q].rstrip()))
+        p = q
+    return '\n'.join('  ' * l + s for l, s in lines)
+
 def binary(s):
     """return true if a string is binary data"""
     return bool(s and '\0' in s)