home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import re
- import sys
- import os
- terminal_escape = re.compile('(\x01?\x1b\\[[0-9;]*m\x02?)')
- escape_parts = re.compile('\x01?\x1b\\[([0-9;]*)m\x02?')
-
- class AnsiState(object):
-
- def __init__(self, bold = False, inverse = False, color = 'white', background = 'black', backgroundbold = False):
- self.bold = bold
- self.inverse = inverse
- self.color = color
- self.background = background
- self.backgroundbold = backgroundbold
-
- trtable = {
- 'black': 0,
- 'red': 4,
- 'green': 2,
- 'yellow': 6,
- 'blue': 1,
- 'magenta': 5,
- 'cyan': 3,
- 'white': 7 }
- revtable = dict(zip(trtable.values(), trtable.keys()))
-
- def get_winattr(self):
- attr = 0
- if self.bold:
- attr |= 8
-
- if self.backgroundbold:
- attr |= 128
-
- if self.inverse:
- attr |= 16384
-
- attr |= self.trtable[self.color]
- attr |= self.trtable[self.background] << 4
- return attr
-
-
- def set_winattr(self, attr):
- self.bold = bool(attr & 8)
- self.backgroundbold = bool(attr & 128)
- self.inverse = bool(attr & 16384)
- self.color = self.revtable[attr & 7]
- self.background = self.revtable[(attr & 112) >> 4]
-
- winattr = property(get_winattr, set_winattr)
-
- def __repr__(self):
- return 'AnsiState(bold=%s,inverse=%s,color=%9s,background=%9s,backgroundbold=%s)# 0x%x' % (self.bold, self.inverse, '"%s"' % self.color, '"%s"' % self.background, self.backgroundbold, self.winattr)
-
-
- def copy(self):
- x = AnsiState()
- x.bold = self.bold
- x.inverse = self.inverse
- x.color = self.color
- x.background = self.background
- x.backgroundbold = self.backgroundbold
- return x
-
-
- defaultstate = AnsiState(False, False, 'white')
- trtable = {
- 0: 'black',
- 1: 'red',
- 2: 'green',
- 3: 'yellow',
- 4: 'blue',
- 5: 'magenta',
- 6: 'cyan',
- 7: 'white' }
-
- class AnsiWriter(object):
-
- def __init__(self, default = defaultstate):
- if isinstance(defaultstate, AnsiState):
- self.defaultstate = default
- else:
- self.defaultstate = AnsiState()
- self.defaultstate.winattr = defaultstate
-
-
- def write_color(self, text, attr = None):
- if isinstance(attr, AnsiState):
- defaultstate = attr
- elif attr is None:
- attr = self.defaultstate.copy()
- else:
- defaultstate = AnsiState()
- defaultstate.winattr = attr
- attr = defaultstate
- chunks = terminal_escape.split(text)
- n = 0
- res = []
- for chunk in chunks:
- m = escape_parts.match(chunk)
- if m:
- parts = m.group(1).split(';')
- if len(parts) == 1 and parts[0] == '0':
- attr = self.defaultstate.copy()
- continue
-
- for part in parts:
- if part == '0':
- attr = self.defaultstate.copy()
- attr.bold = False
- continue
- if part == '7':
- attr.inverse = True
- continue
- if part == '1':
- attr.bold = True
- continue
- if len(part) == 2:
- if part <= part:
- pass
- elif part <= '37':
- attr.color = trtable[int(part) - 30]
- continue
- if len(part) == 2:
- if part <= part:
- pass
- elif part <= '47':
- attr.color = trtable[int(part) - 40]
- continue
-
- continue
-
- n += len(chunk)
- if True:
- res.append((attr.copy(), chunk))
- continue
-
- return (n, res)
-
-
- def parse_color(self, text, attr = None):
- (n, res) = self.write_color(text, attr)
- return ([], [ attr.winattr for attr, text in res ])
-
-
-
- def write_color(text, attr = None):
- a = AnsiWriter(defaultstate)
- return a.write_color(text, attr)
-
-
- def write_color_old(text, attr = None):
- res = []
- chunks = terminal_escape.split(text)
- n = 0
- if attr is None:
- attr = 15
-
- for chunk in chunks:
- m = escape_parts.match(chunk)
- if m:
- for part in m.group(1).split(';'):
- if part == '0':
- attr = 0
- elif part == '7':
- attr |= 16384
-
- if part == '1':
- attr |= 8
- continue
- if len(part) == 2:
- if part <= part:
- pass
- elif part <= '37':
- part = int(part) - 30
- attr = attr & -8 | (part & 1) << 2 | part & 2 | (part & 4) >> 2
- continue
- if len(part) == 2:
- if part <= part:
- pass
- elif part <= '47':
- part = int(part) - 40
- attr = attr & -113 | (part & 1) << 6 | (part & 2) << 4 | (part & 4) << 2
- continue
-
- continue
-
- n += len(chunk)
- if chunk:
- res.append(('0x%x' % attr, chunk))
- continue
-
- return res
-
- if __name__ == '__main__':
- import pprint
- pprint = pprint.pprint
- s = '\x1b[0;31mred\x1b[0;32mgreen\x1b[0;33myellow\x1b[0;34mblue\x1b[0;35mmagenta\x1b[0;36mcyan\x1b[0;37mwhite\x1b[0m'
- pprint(write_color(s))
- pprint(write_color_old(s))
- s = '\x1b[1;31mred\x1b[1;32mgreen\x1b[1;33myellow\x1b[1;34mblue\x1b[1;35mmagenta\x1b[1;36mcyan\x1b[1;37mwhite\x1b[0m'
- pprint(write_color(s))
- pprint(write_color_old(s))
- s = '\x1b[0;7;31mred\x1b[0;7;32mgreen\x1b[0;7;33myellow\x1b[0;7;34mblue\x1b[0;7;35mmagenta\x1b[0;7;36mcyan\x1b[0;7;37mwhite\x1b[0m'
- pprint(write_color(s))
- pprint(write_color_old(s))
- s = '\x1b[1;7;31mred\x1b[1;7;32mgreen\x1b[1;7;33myellow\x1b[1;7;34mblue\x1b[1;7;35mmagenta\x1b[1;7;36mcyan\x1b[1;7;37mwhite\x1b[0m'
- pprint(write_color(s))
- pprint(write_color_old(s))
-
- if __name__ == '__main__':
- import console
- c = console.Console()
- c.write_color('dhsjdhs')
- c.write_color('\x1b[0;32mIn [\x1b[1;32m1\x1b[0;32m]:')
- print
- pprint(write_color('\x1b[0;32mIn [\x1b[1;32m1\x1b[0;32m]:'))
-
-