summaryrefslogtreecommitdiff
path: root/pygments/formatters
diff options
context:
space:
mode:
authorTim Hatch <tim@timhatch.com>2015-10-13 09:50:10 -0700
committerTim Hatch <tim@timhatch.com>2015-10-13 09:50:10 -0700
commit1e1426b13b56038238d3744b0ca2c66c99b4bedc (patch)
treedf15cc2666e2756108ee69c58cf6b8714ae1ec8d /pygments/formatters
parent63c6e8545a1e6b10958e06cd81a4c50f85c7ccc2 (diff)
parent0e93ca51b2d9e6a5dfcf16c68830230bd2663412 (diff)
downloadpygments-1e1426b13b56038238d3744b0ca2c66c99b4bedc.tar.gz
Merged in jonashaag/pygments-main/jonashaag/fix-typo-1441310321454 (pull request #496)
Fix typo
Diffstat (limited to 'pygments/formatters')
-rw-r--r--pygments/formatters/terminal.py70
1 files changed, 27 insertions, 43 deletions
diff --git a/pygments/formatters/terminal.py b/pygments/formatters/terminal.py
index 3c4b025f..a6eb48a4 100644
--- a/pygments/formatters/terminal.py
+++ b/pygments/formatters/terminal.py
@@ -101,51 +101,35 @@ class TerminalFormatter(Formatter):
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")
+ 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._format_unencoded_with_lineno(tokensource, outfile)
- return
+ 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")