Commits

Georg Brandl committed dcd47a0

Use a new progress indicator with percents.

  • Participants
  • Parent commits 045178a

Comments (0)

Files changed (2)

File sphinx/builders/__init__.py

 from sphinx import package_dir, locale
 from sphinx.util import SEP, relative_uri
 from sphinx.environment import BuildEnvironment
-from sphinx.util.console import bold, purple, darkgreen
+from sphinx.util.console import bold, purple, darkgreen, term_width_line
 
 # side effect: registers roles and directives
 from sphinx import roles
         """
         raise NotImplementedError
 
-    def status_iterator(self, iterable, length, summary, colorfunc=darkgreen):
-        l = -1
+    def old_status_iterator(self, iterable, summary, colorfunc=darkgreen):
+        l = 0
         for item in iterable:
-            if l == -1:
+            if l == 0:
                 self.info(bold(summary), nonl=1)
-                l = 0
+                l = 1
             self.info(colorfunc(item) + ' ', nonl=1)
             yield item
-        if l == 0:
+        if l == 1:
             self.info()
 
-    ## new version with progress info
-    #def status_iterator(self, iterable, length, summary, colorfunc=darkgreen):
-    #    l = 0
-    #    for item in iterable:
-    #        if l == 0:
-    #            self.info(bold(summary))
-    #        l += 1
-    #        self.info('  [%3d%%] %s\n' % (100*l/length, colorfunc(item)),
-    #                  nonl=1)
-    #        yield item
+    # new version with progress info
+    def status_iterator(self, iterable, summary, colorfunc=darkgreen, length=0):
+        if length == 0:
+            for item in self.old_status_iterator(iterable, summary, colorfunc):
+                yield item
+            return
+        l = 0
+        summary = bold(summary)
+        for item in iterable:
+            l += 1
+            self.info(term_width_line('%s[%3d%%] %s' %
+                                      (summary, 100*l/length,
+                                       colorfunc(item))), nonl=1)
+            yield item
+        if l > 0:
+            self.info()
 
     supported_image_types = []
 
         msg, length, iterator = self.env.update(self.config, self.srcdir,
                                                 self.doctreedir, self.app)
         self.info(msg)
-        for docname in self.status_iterator(iterator, length,
-                                            'reading sources... ', purple):
+        for docname in self.status_iterator(iterator, 'reading sources... ',
+                                            purple, length):
             updated_docnames.add(docname)
             # nothing further to do, the environment has already
             # done the reading
         # write target files
         warnings = []
         self.env.set_warnfunc(lambda *args: warnings.append(args))
-        for docname in self.status_iterator(sorted(docnames), len(docnames),
-                                            'writing output... ', darkgreen):
+        for docname in self.status_iterator(
+            sorted(docnames), 'writing output... ', darkgreen, len(docnames)):
             doctree = self.env.get_and_resolve_doctree(docname, self)
             self.write_doc(docname, doctree)
         for warning in warnings:

File sphinx/util/console.py

 def get_terminal_width():
     """Borrowed from the py lib."""
     try:
-        import os, termios, fcntl, struct
-        call = fcntl.ioctl(0, termios.TIOCGWINSZ, "\000"*8)
-        height, width = struct.unpack("hhhh", call)[:2]
+        import termios, fcntl, struct
+        call = fcntl.ioctl(0, termios.TIOCGWINSZ,
+                           struct.pack('hhhh', 0, 0, 0, 0))
+        height, width = struct.unpack('hhhh', call)[:2]
         terminal_width = width
     except (SystemExit, KeyboardInterrupt):
         raise
     except:
         # FALLBACK
-        terminal_width = int(os.environ.get('COLUMNS', 80))-1
+        terminal_width = int(os.environ.get('COLUMNS', 80)) - 1
     return terminal_width
 
 _tw = get_terminal_width()
 
-def print_and_backspace(text, func):
+def term_width_line(text):
     if not codes:
         # if no coloring, don't output fancy backspaces
-        func(text)
+        return text + '\n'
     else:
-        func(text.ljust(_tw) + _tw * "\b")
+        return text.ljust(_tw) + '\r'
 
 def color_terminal():
     if 'COLORTERM' in os.environ: