diff MoinMoin/support/pygments/formatters/terminal.py @ 6097:815981fad7fd

upgrade pygments from 1.6 to 2.1.3
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 05 Sep 2016 23:55:33 +0200
parents b48a69886ca4
children
line wrap: on
line diff
--- a/MoinMoin/support/pygments/formatters/terminal.py	Mon Sep 05 23:44:04 2016 +0200
+++ b/MoinMoin/support/pygments/formatters/terminal.py	Mon Sep 05 23:55:33 2016 +0200
@@ -5,7 +5,7 @@
 
     Formatter for terminal output with ANSI sequences.
 
-    :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS.
+    :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS.
     :license: BSD, see LICENSE for details.
 """
 
@@ -13,7 +13,7 @@
 
 from pygments.formatter import Formatter
 from pygments.token import Keyword, Name, Comment, String, Error, \
-     Number, Operator, Generic, Token, Whitespace
+    Number, Operator, Generic, Token, Whitespace
 from pygments.console import ansiformat
 from pygments.util import get_choice_opt
 
@@ -49,6 +49,7 @@
     Generic.Inserted:   ('darkgreen',  'green'),
     Generic.Heading:    ('**',         '**'),
     Generic.Subheading: ('*purple*',   '*fuchsia*'),
+    Generic.Prompt:     ('**',         '**'),
     Generic.Error:      ('red',        'red'),
 
     Error:              ('_red_',      '_red_'),
@@ -73,6 +74,10 @@
     `colorscheme`
         A dictionary mapping token types to (lightbg, darkbg) color names or
         ``None`` (default: ``None`` = use builtin colorscheme).
+
+    `linenos`
+        Set to ``True`` to have line numbers on the terminal output as well
+        (default: ``False`` = no line numbers).
     """
     name = 'Terminal'
     aliases = ['terminal', 'console']
@@ -83,6 +88,8 @@
         self.darkbg = get_choice_opt(options, 'bg',
                                      ['light', 'dark'], 'light') == 'dark'
         self.colorscheme = options.get('colorscheme', None) or TERMINAL_COLORS
+        self.linenos = options.get('linenos', False)
+        self._lineno = 0
 
     def format(self, tokensource, outfile):
         # hack: if the output is a terminal and has an encoding set,
@@ -93,20 +100,37 @@
             self.encoding = outfile.encoding
         return Formatter.format(self, tokensource, outfile)
 
+    def _write_lineno(self, outfile):
+        self._lineno += 1
+        outfile.write("%s%04d: " % (self._lineno != 1 and '\n' or '', self._lineno))
+
+    def _get_color(self, ttype):
+        # self.colorscheme is a dict containing usually generic types, so we
+        # have to walk the tree of dots.  The base Token type must be a key,
+        # even if it's empty string, as in the default above.
+        colors = self.colorscheme.get(ttype)
+        while colors is None:
+            ttype = ttype.parent
+            colors = self.colorscheme.get(ttype)
+        return colors[self.darkbg]
+
     def format_unencoded(self, tokensource, outfile):
+        if self.linenos:
+            self._write_lineno(outfile)
+
         for ttype, value in tokensource:
-            color = self.colorscheme.get(ttype)
-            while color is None:
-                ttype = ttype[:-1]
-                color = self.colorscheme.get(ttype)
-            if color:
-                color = color[self.darkbg]
-                spl = value.split('\n')
-                for line in spl[:-1]:
-                    if line:
-                        outfile.write(ansiformat(color, line))
-                    outfile.write('\n')
-                if spl[-1]:
-                    outfile.write(ansiformat(color, spl[-1]))
-            else:
-                outfile.write(value)
+            color = self._get_color(ttype)
+
+            for line in value.splitlines(True):
+                if color:
+                    outfile.write(ansiformat(color, line.rstrip('\n')))
+                else:
+                    outfile.write(line.rstrip('\n'))
+                if line.endswith('\n'):
+                    if self.linenos:
+                        self._write_lineno(outfile)
+                    else:
+                        outfile.write('\n')
+
+        if self.linenos:
+            outfile.write("\n")