summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJiamin Shen <john980118@163.com>2021-02-06 21:29:59 +0800
committerGitHub <noreply@github.com>2021-02-06 14:29:59 +0100
commitcf27617f4d1e1a41be0d892236de460decd8ede0 (patch)
treec9dc1ba577391bd336beff5dfb18916950e74f0c
parentd09fd511aa4484ae5ac0196041bad1ecba00f5f8 (diff)
downloadpygments-git-cf27617f4d1e1a41be0d892236de460decd8ede0.tar.gz
linenos support for terminal256 (#1674)
-rw-r--r--pygments/formatters/terminal256.py24
1 files changed, 23 insertions, 1 deletions
diff --git a/pygments/formatters/terminal256.py b/pygments/formatters/terminal256.py
index 0c318c5c..7060f5ba 100644
--- a/pygments/formatters/terminal256.py
+++ b/pygments/formatters/terminal256.py
@@ -125,6 +125,10 @@ class Terminal256Formatter(Formatter):
`style`
The style to use, can be a string or a Style subclass (default:
``'default'``).
+
+ `linenos`
+ Set to ``True`` to have line numbers on the terminal output as well
+ (default: ``False`` = no line numbers).
"""
name = 'Terminal256'
aliases = ['terminal256', 'console256', '256']
@@ -144,6 +148,9 @@ class Terminal256Formatter(Formatter):
self._build_color_table() # build an RGB-to-256 color conversion table
self._setup_styles() # convert selected style's colors to term. colors
+ self.linenos = options.get('linenos', False)
+ self._lineno = 0
+
def _build_color_table(self):
# colors 0..15: 16 basic colors
@@ -237,10 +244,17 @@ class Terminal256Formatter(Formatter):
self.style_string[str(ttype)] = (escape.color_string(),
escape.reset_string())
+ def _write_lineno(self, outfile):
+ self._lineno += 1
+ outfile.write("%s%04d: " % (self._lineno != 1 and '\n' or '', self._lineno))
+
def format(self, tokensource, outfile):
return Formatter.format(self, tokensource, outfile)
def format_unencoded(self, tokensource, outfile):
+ if self.linenos:
+ self._write_lineno(outfile)
+
for ttype, value in tokensource:
not_found = True
while ttype and not_found:
@@ -254,7 +268,11 @@ class Terminal256Formatter(Formatter):
for line in spl[:-1]:
if line:
outfile.write(on + line + off)
- outfile.write('\n')
+ if self.linenos:
+ self._write_lineno(outfile)
+ else:
+ outfile.write('\n')
+
if spl[-1]:
outfile.write(on + spl[-1] + off)
@@ -269,6 +287,10 @@ class Terminal256Formatter(Formatter):
if not_found:
outfile.write(value)
+ if self.linenos:
+ outfile.write("\n")
+
+
class TerminalTrueColorFormatter(Terminal256Formatter):
r"""