-
Notifications
You must be signed in to change notification settings - Fork 1
/
author_lexer.py
73 lines (62 loc) · 2.06 KB
/
author_lexer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
Base classes for prompt_toolkit lexers.
"""
from typing import Callable
from prompt_toolkit.document import Document
from prompt_toolkit.formatted_text.base import StyleAndTextTuples
from prompt_toolkit.lexers import Lexer
from docengine import Doc
class AuthorLexer(Lexer):
"""
Lexer that highlights each char with it's author color
from the COLORS palette
"""
COLORS = (
'fg:ansired',
'fg:ansigreen',
'fg:ansiyellow',
'fg:ansiblue',
'fg:ansimagenta',
'fg:ansicyan',
'fg:ansibrightblue'
)
NUM_COLORS = len(COLORS)
def __init__(self, doc: Doc) -> None:
self.doc = doc
def __get_author_color(self, site_id) -> str:
"""
Get color for document site id
:param site_id: id of document site (author id)
:return: str with ansi color
"""
return self.COLORS[site_id % self.NUM_COLORS]
def lex_document(self, document: Document)\
-> Callable[[int], StyleAndTextTuples]:
"""
Highlight document
:param document: document object
:return: function that returns line at line number as formatted text
"""
colored_lines = []
line_colors: StyleAndTextTuples = []
closed_line = False
for c, a in zip(self.doc.text, self.doc.authors[1:-1]):
if c == "\n":
colored_lines.append(line_colors)
line_colors = []
closed_line = True
continue
else:
line_colors.append((self.__get_author_color(a), c))
closed_line = False
if not closed_line:
colored_lines.append(line_colors)
def get_line(line_number: int) -> StyleAndTextTuples:
"""
Return the tokens for the provided line.
"""
try:
return colored_lines[line_number]
except IndexError:
return []
return get_line