ThirdParty/Pygments/pygments/formatters/terminal.py

changeset 4172
4f20dba37ab6
parent 3145
a9de05d4a22f
child 4697
c2e9bf425554
--- a/ThirdParty/Pygments/pygments/formatters/terminal.py	Wed Mar 11 18:25:37 2015 +0100
+++ b/ThirdParty/Pygments/pygments/formatters/terminal.py	Wed Mar 11 18:32:27 2015 +0100
@@ -5,17 +5,15 @@
 
     Formatter for terminal output with ANSI sequences.
 
-    :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS.
+    :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS.
     :license: BSD, see LICENSE for details.
 """
 
-from __future__ import unicode_literals
-
 import sys
 
 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
 
@@ -75,6 +73,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']
@@ -85,6 +87,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,
@@ -95,7 +99,40 @@
             self.encoding = outfile.encoding
         return Formatter.format(self, tokensource, outfile)
 
+    def _write_lineno(self, outfile):
+        self._lineno += 1
+        outfile.write("\n%04d: " % self._lineno)
+
+    def _format_unencoded_with_lineno(self, tokensource, outfile):
+        self._write_lineno(outfile)
+
+        for ttype, value in tokensource:
+            if value.endswith("\n"):
+                self._write_lineno(outfile)
+                value = value[:-1]
+            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]:
+                    self._write_lineno(outfile)
+                    if line:
+                        outfile.write(ansiformat(color, line[:-1]))
+                if spl[-1]:
+                    outfile.write(ansiformat(color, spl[-1]))
+            else:
+                outfile.write(value)
+
+        outfile.write("\n")
+
     def format_unencoded(self, tokensource, outfile):
+        if self.linenos:
+            self._format_unencoded_with_lineno(tokensource, outfile)
+            return
+
         for ttype, value in tokensource:
             color = self.colorscheme.get(ttype)
             while color is None:

eric ide

mercurial