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') |