Source

py-patches / tw-reline-remove

# HG changeset patch
# Parent 2c042feb5b3b1acebc7ceb565cac0a3b72d2a732
terminalwriter: support marking the reline output as removable

diff --git a/py/_io/terminalwriter.py b/py/_io/terminalwriter.py
--- a/py/_io/terminalwriter.py
+++ b/py/_io/terminalwriter.py
@@ -183,12 +183,24 @@ class TerminalWriter(object):
 
     def line(self, s='', **kw):
         if self._newline == False:
-            self.write("\n")
+            if getattr(self, '_remove', False):
+                self.write('\r')
+                lenline = len(s)
+                try:
+                    lastlen = self._lastlinelen
+                except AttributeError:
+                    pass
+                else:
+                    if lenline < lastlen:
+                        self.write(" " * (lastlen - lenline + 1))
+                self.write('\r')
+            else:
+                self.write("\n")
         self.write(s, **kw)
         self.write('\n')
         self._newline = True
 
-    def reline(self, line, **opts):
+    def reline(self, line, remove=False, **opts):
         if not self.hasmarkup:
             raise ValueError("cannot use rewrite-line without terminal")
         if not self._newline:
@@ -205,6 +217,7 @@ class TerminalWriter(object):
             if lenline < lastlen:
                 self.write(" " * (lastlen - lenline + 1))
         self._lastlinelen = lenline
+        self._remove = remove
         self._newline = False