pmacs3/highlight2.py

49 lines
1.6 KiB
Python

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]
class Highlighter:
def __init__(self, lexer):
self.lexer = lexer
self.tokens = []
def display(self, token_colors={}, debug=False):
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:
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])
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)
for token in self.lexer:
self.tokens[token.y].append(token)
def update(self, lines):
pass