ThirdParty/Pygments/pygments/formatters/terminal256.py

changeset 4172
4f20dba37ab6
parent 3145
a9de05d4a22f
child 4697
c2e9bf425554
equal deleted inserted replaced
4170:8bc578136279 4172:4f20dba37ab6
9 tool (http://frexx.de/xterm-256-notes/data/xterm256-conv2.tar.bz2) 9 tool (http://frexx.de/xterm-256-notes/data/xterm256-conv2.tar.bz2)
10 by Wolfgang Frisch. 10 by Wolfgang Frisch.
11 11
12 Formatter version 1. 12 Formatter version 1.
13 13
14 :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. 14 :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS.
15 :license: BSD, see LICENSE for details. 15 :license: BSD, see LICENSE for details.
16 """ 16 """
17
18 from __future__ import unicode_literals
19 17
20 # TODO: 18 # TODO:
21 # - Options to map style's bold/underline/italic/border attributes 19 # - Options to map style's bold/underline/italic/border attributes
22 # to some ANSI attrbutes (something like 'italic=underline') 20 # to some ANSI attrbutes (something like 'italic=underline')
23 # - An option to output "style RGB to xterm RGB/index" conversion table 21 # - An option to output "style RGB to xterm RGB/index" conversion table
66 attrs.append("49") 64 attrs.append("49")
67 if self.bold or self.underline: 65 if self.bold or self.underline:
68 attrs.append("00") 66 attrs.append("00")
69 return self.escape(attrs) 67 return self.escape(attrs)
70 68
69
71 class Terminal256Formatter(Formatter): 70 class Terminal256Formatter(Formatter):
72 r""" 71 r"""
73 Format tokens with ANSI color sequences, for output in a 256-color 72 Format tokens with ANSI color sequences, for output in a 256-color
74 terminal or console. Like in `TerminalFormatter` color sequences 73 terminal or console. Like in `TerminalFormatter` color sequences
75 are terminated at newlines, so that paging the output works correctly. 74 are terminated at newlines, so that paging the output works correctly.
76 75
77 The formatter takes colors from a style defined by the `style` option 76 The formatter takes colors from a style defined by the `style` option
78 and converts them to nearest ANSI 256-color escape sequences. Bold and 77 and converts them to nearest ANSI 256-color escape sequences. Bold and
79 underline attributes from the style are preserved (and displayed). 78 underline attributes from the style are preserved (and displayed).
80 79
81 *New in Pygments 0.9.* 80 .. versionadded:: 0.9
82 81
83 Options accepted: 82 Options accepted:
84 83
85 `style` 84 `style`
86 The style to use, can be a string or a Style subclass (default: 85 The style to use, can be a string or a Style subclass (default:
98 self.style_string = {} 97 self.style_string = {}
99 98
100 self.usebold = 'nobold' not in options 99 self.usebold = 'nobold' not in options
101 self.useunderline = 'nounderline' not in options 100 self.useunderline = 'nounderline' not in options
102 101
103 self._build_color_table() # build an RGB-to-256 color conversion table 102 self._build_color_table() # build an RGB-to-256 color conversion table
104 self._setup_styles() # convert selected style's colors to term. colors 103 self._setup_styles() # convert selected style's colors to term. colors
105 104
106 def _build_color_table(self): 105 def _build_color_table(self):
107 # colors 0..15: 16 basic colors 106 # colors 0..15: 16 basic colors
108 107
109 self.xterm_colors.append((0x00, 0x00, 0x00)) # 0 108 self.xterm_colors.append((0x00, 0x00, 0x00)) # 0
110 self.xterm_colors.append((0xcd, 0x00, 0x00)) # 1 109 self.xterm_colors.append((0xcd, 0x00, 0x00)) # 1
111 self.xterm_colors.append((0x00, 0xcd, 0x00)) # 2 110 self.xterm_colors.append((0x00, 0xcd, 0x00)) # 2
112 self.xterm_colors.append((0xcd, 0xcd, 0x00)) # 3 111 self.xterm_colors.append((0xcd, 0xcd, 0x00)) # 3
113 self.xterm_colors.append((0x00, 0x00, 0xee)) # 4 112 self.xterm_colors.append((0x00, 0x00, 0xee)) # 4
114 self.xterm_colors.append((0xcd, 0x00, 0xcd)) # 5 113 self.xterm_colors.append((0xcd, 0x00, 0xcd)) # 5
115 self.xterm_colors.append((0x00, 0xcd, 0xcd)) # 6 114 self.xterm_colors.append((0x00, 0xcd, 0xcd)) # 6
116 self.xterm_colors.append((0xe5, 0xe5, 0xe5)) # 7 115 self.xterm_colors.append((0xe5, 0xe5, 0xe5)) # 7
117 self.xterm_colors.append((0x7f, 0x7f, 0x7f)) # 8 116 self.xterm_colors.append((0x7f, 0x7f, 0x7f)) # 8
118 self.xterm_colors.append((0xff, 0x00, 0x00)) # 9 117 self.xterm_colors.append((0xff, 0x00, 0x00)) # 9
119 self.xterm_colors.append((0x00, 0xff, 0x00)) # 10 118 self.xterm_colors.append((0x00, 0xff, 0x00)) # 10
120 self.xterm_colors.append((0xff, 0xff, 0x00)) # 11 119 self.xterm_colors.append((0xff, 0xff, 0x00)) # 11
121 self.xterm_colors.append((0x5c, 0x5c, 0xff)) # 12 120 self.xterm_colors.append((0x5c, 0x5c, 0xff)) # 12
122 self.xterm_colors.append((0xff, 0x00, 0xff)) # 13 121 self.xterm_colors.append((0xff, 0x00, 0xff)) # 13
123 self.xterm_colors.append((0x00, 0xff, 0xff)) # 14 122 self.xterm_colors.append((0x00, 0xff, 0xff)) # 14
124 self.xterm_colors.append((0xff, 0xff, 0xff)) # 15 123 self.xterm_colors.append((0xff, 0xff, 0xff)) # 15
125 124
126 # colors 16..232: the 6x6x6 color cube 125 # colors 16..232: the 6x6x6 color cube
127 126
128 valuerange = (0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff) 127 valuerange = (0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff)
129 128
138 for i in range(1, 22): 137 for i in range(1, 22):
139 v = 8 + i * 10 138 v = 8 + i * 10
140 self.xterm_colors.append((v, v, v)) 139 self.xterm_colors.append((v, v, v))
141 140
142 def _closest_color(self, r, g, b): 141 def _closest_color(self, r, g, b):
143 distance = 257*257*3 # "infinity" (>distance from #000000 to #ffffff) 142 distance = 257*257*3 # "infinity" (>distance from #000000 to #ffffff)
144 match = 0 143 match = 0
145 144
146 for i in range(0, 254): 145 for i in range(0, 254):
147 values = self.xterm_colors[i] 146 values = self.xterm_colors[i]
148 147
197 def format_unencoded(self, tokensource, outfile): 196 def format_unencoded(self, tokensource, outfile):
198 for ttype, value in tokensource: 197 for ttype, value in tokensource:
199 not_found = True 198 not_found = True
200 while ttype and not_found: 199 while ttype and not_found:
201 try: 200 try:
202 #outfile.write( "<" + str(ttype) + ">" ) 201 # outfile.write( "<" + str(ttype) + ">" )
203 on, off = self.style_string[str(ttype)] 202 on, off = self.style_string[str(ttype)]
204 203
205 # Like TerminalFormatter, add "reset colors" escape sequence 204 # Like TerminalFormatter, add "reset colors" escape sequence
206 # on newline. 205 # on newline.
207 spl = value.split('\n') 206 spl = value.split('\n')
211 outfile.write('\n') 210 outfile.write('\n')
212 if spl[-1]: 211 if spl[-1]:
213 outfile.write(on + spl[-1] + off) 212 outfile.write(on + spl[-1] + off)
214 213
215 not_found = False 214 not_found = False
216 #outfile.write( '#' + str(ttype) + '#' ) 215 # outfile.write( '#' + str(ttype) + '#' )
217 216
218 except KeyError: 217 except KeyError:
219 #ottype = ttype 218 # ottype = ttype
220 ttype = ttype[:-1] 219 ttype = ttype[:-1]
221 #outfile.write( '!' + str(ottype) + '->' + str(ttype) + '!' ) 220 # outfile.write( '!' + str(ottype) + '->' + str(ttype) + '!' )
222 221
223 if not_found: 222 if not_found:
224 outfile.write(value) 223 outfile.write(value)

eric ide

mercurial