pmacs3/highlight2.py

54 lines
1.8 KiB
Python
Raw Normal View History

2007-03-29 18:37:34 -04:00
import sys
color_list = []
color_list.extend(['\033[3%dm' % x for x in range(0, 8)])
color_list.extend(['\033[3%d;1m' % x for x in range(0, 8)])
color_list.append('\033[0m')
color_names = [
'black', 'dred', 'dgreen', 'brown', 'dblue', 'dpurple', 'dcyan', 'lgrey',
'dgrey', 'lred', 'lgreen', 'yellow', 'lblue', 'lpurple', 'lcyan', 'white',
'unset',
]
color_dict ={}
for i in range(0, len(color_list)):
color_dict[color_names[i]] = color_list[i]
2007-03-28 18:38:32 -04:00
class Highlighter:
def __init__(self, lexer):
self.lexer = lexer
self.tokens = []
2007-03-29 18:37:34 -04:00
def display(self, token_colors={}, debug=False):
2007-03-28 18:38:32 -04:00
for group in self.tokens:
for token in group:
color_name = None
name_parts = token.name.split('.')
for i in range(0, len(name_parts)):
if '.'.join(name_parts[i:]) in token_colors:
color_name = token_colors['.'.join(name_parts[i:])]
break
if color_name is not None:
2007-03-29 18:37:34 -04:00
sys.stdout.write(color_dict[color_name])
elif debug:
raise Exception, "no highlighting for %r" % token.name
else:
color_name = 'white'
sys.stdout.write(color_dict[color_name])
2007-03-28 18:38:32 -04:00
sys.stdout.write(token.string)
sys.stdout.write('\n')
def highlight(self, lines):
self.tokens = [[] for l in lines]
self.lexer.lex(lines, y=0, x=0)
y = 0
for token in self.lexer:
assert y <= token.y, "oh no %d %d" % (y, token.y)
assert token.y < len(lines), "uh oh %d %d" % (token.y, len(lines))
self.tokens[token.y].append(token)
y = token.y
def update(self):
pass