Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions
MoinMoin.support.pygments.formatters.terminal256.Terminal256Formatter Class Reference
Inheritance diagram for MoinMoin.support.pygments.formatters.terminal256.Terminal256Formatter:
Inheritance graph
[legend]
Collaboration diagram for MoinMoin.support.pygments.formatters.terminal256.Terminal256Formatter:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def format
def format_unencoded

Public Attributes

 xterm_colors
 best_match
 style_string
 usebold
 useunderline
 encoding

Static Public Attributes

string name = 'Terminal256'
list aliases = ['terminal256', 'console256', '256']
list filenames = []

Private Member Functions

def _build_color_table
def _closest_color
def _color_index
def _setup_styles

Detailed Description

Definition at line 67 of file terminal256.py.


Constructor & Destructor Documentation

Definition at line 89 of file terminal256.py.

00089 
00090     def __init__(self, **options):
00091         Formatter.__init__(self, **options)
00092 
00093         self.xterm_colors = []
00094         self.best_match = {}
00095         self.style_string = {}
00096 
00097         self.usebold = 'nobold' not in options
00098         self.useunderline = 'nounderline' not in options
00099 
00100         self._build_color_table() # build an RGB-to-256 color conversion table
00101         self._setup_styles() # convert selected style's colors to term. colors


Member Function Documentation

Definition at line 102 of file terminal256.py.

00102 
00103     def _build_color_table(self):
00104         # colors 0..15: 16 basic colors
00105 
00106         self.xterm_colors.append((0x00, 0x00, 0x00)) # 0
00107         self.xterm_colors.append((0xcd, 0x00, 0x00)) # 1
00108         self.xterm_colors.append((0x00, 0xcd, 0x00)) # 2
00109         self.xterm_colors.append((0xcd, 0xcd, 0x00)) # 3
00110         self.xterm_colors.append((0x00, 0x00, 0xee)) # 4
00111         self.xterm_colors.append((0xcd, 0x00, 0xcd)) # 5
00112         self.xterm_colors.append((0x00, 0xcd, 0xcd)) # 6
00113         self.xterm_colors.append((0xe5, 0xe5, 0xe5)) # 7
00114         self.xterm_colors.append((0x7f, 0x7f, 0x7f)) # 8
00115         self.xterm_colors.append((0xff, 0x00, 0x00)) # 9
00116         self.xterm_colors.append((0x00, 0xff, 0x00)) # 10
00117         self.xterm_colors.append((0xff, 0xff, 0x00)) # 11
00118         self.xterm_colors.append((0x5c, 0x5c, 0xff)) # 12
00119         self.xterm_colors.append((0xff, 0x00, 0xff)) # 13
00120         self.xterm_colors.append((0x00, 0xff, 0xff)) # 14
00121         self.xterm_colors.append((0xff, 0xff, 0xff)) # 15
00122 
00123         # colors 16..232: the 6x6x6 color cube
00124 
00125         valuerange = (0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff)
00126 
00127         for i in range(217):
00128             r = valuerange[(i // 36) % 6]
00129             g = valuerange[(i // 6) % 6]
00130             b = valuerange[i % 6]
00131             self.xterm_colors.append((r, g, b))
00132 
00133         # colors 233..253: grayscale
00134 
00135         for i in range(1, 22):
00136             v = 8 + i * 10
00137             self.xterm_colors.append((v, v, v))

Definition at line 138 of file terminal256.py.

00138 
00139     def _closest_color(self, r, g, b):
00140         distance = 257*257*3 # "infinity" (>distance from #000000 to #ffffff)
00141         match = 0
00142 
00143         for i in range(0, 254):
00144             values = self.xterm_colors[i]
00145 
00146             rd = r - values[0]
00147             gd = g - values[1]
00148             bd = b - values[2]
00149             d = rd*rd + gd*gd + bd*bd
00150 
00151             if d < distance:
00152                 match = i
00153                 distance = d
00154         return match

Here is the caller graph for this function:

Definition at line 155 of file terminal256.py.

00155 
00156     def _color_index(self, color):
00157         index = self.best_match.get(color, None)
00158         if index is None:
00159             try:
00160                 rgb = int(str(color), 16)
00161             except ValueError:
00162                 rgb = 0
00163 
00164             r = (rgb >> 16) & 0xff
00165             g = (rgb >> 8) & 0xff
00166             b = rgb & 0xff
00167             index = self._closest_color(r, g, b)
00168             self.best_match[color] = index
00169         return index

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 170 of file terminal256.py.

00170 
00171     def _setup_styles(self):
00172         for ttype, ndef in self.style:
00173             escape = EscapeSequence()
00174             if ndef['color']:
00175                 escape.fg = self._color_index(ndef['color'])
00176             if ndef['bgcolor']:
00177                 escape.bg = self._color_index(ndef['bgcolor'])
00178             if self.usebold and ndef['bold']:
00179                 escape.bold = True
00180             if self.useunderline and ndef['underline']:
00181                 escape.underline = True
00182             self.style_string[str(ttype)] = (escape.color_string(),
00183                                              escape.reset_string())

Here is the call graph for this function:

Definition at line 184 of file terminal256.py.

00184 
00185     def format(self, tokensource, outfile):
00186         # hack: if the output is a terminal and has an encoding set,
00187         # use that to avoid unicode encode problems
00188         if not self.encoding and hasattr(outfile, "encoding") and \
00189            hasattr(outfile, "isatty") and outfile.isatty():
00190             self.encoding = outfile.encoding
00191         return Formatter.format(self, tokensource, outfile)

Definition at line 192 of file terminal256.py.

00192 
00193     def format_unencoded(self, tokensource, outfile):
00194         for ttype, value in tokensource:
00195             not_found = True
00196             while ttype and not_found:
00197                 try:
00198                     #outfile.write( "<" + str(ttype) + ">" )
00199                     on, off = self.style_string[str(ttype)]
00200 
00201                     # Like TerminalFormatter, add "reset colors" escape sequence
00202                     # on newline.
00203                     spl = value.split('\n')
00204                     for line in spl[:-1]:
00205                         if line:
00206                             outfile.write(on + line + off)
00207                         outfile.write('\n')
00208                     if spl[-1]:
00209                         outfile.write(on + spl[-1] + off)
00210 
00211                     not_found = False
00212                     #outfile.write( '#' + str(ttype) + '#' )
00213 
00214                 except KeyError:
00215                     #ottype = ttype
00216                     ttype = ttype[:-1]
00217                     #outfile.write( '!' + str(ottype) + '->' + str(ttype) + '!' )
00218 
00219             if not_found:
00220                 outfile.write(value)

Here is the caller graph for this function:


Member Data Documentation

list MoinMoin.support.pygments.formatters.terminal256.Terminal256Formatter.aliases = ['terminal256', 'console256', '256'] [static]

Definition at line 86 of file terminal256.py.

Definition at line 93 of file terminal256.py.

Definition at line 189 of file terminal256.py.

Definition at line 87 of file terminal256.py.

Definition at line 85 of file terminal256.py.

Definition at line 94 of file terminal256.py.

Definition at line 96 of file terminal256.py.

Definition at line 97 of file terminal256.py.

Definition at line 92 of file terminal256.py.


The documentation for this class was generated from the following file: