-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6bbb57c
commit b6f6b87
Showing
4 changed files
with
213 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import re | ||
|
||
import pygments.lexer | ||
import pygments.token | ||
from pygments.token import Text | ||
|
||
|
||
# Pygments doesn't have a generic "color" token; instead everything is | ||
# contextual (e.g. "comment" or "variable"). That doesn't make sense for us, | ||
# where the actual colors actually *are* what we care about. | ||
Color = pygments.token.Token.Color | ||
|
||
_ansi_code_to_color = { | ||
0: 'Black', | ||
1: 'Red', | ||
2: 'Green', | ||
3: 'Yellow', | ||
4: 'Blue', | ||
5: 'Magenta', | ||
6: 'Cyan', | ||
7: 'White', | ||
} | ||
|
||
|
||
def token_from_lexer_state(bold, fg_color, bg_color): | ||
"""Construct a token given the current lexer state. | ||
We can only emit one token even though we have a multiple-tuple state. | ||
To do this, we construct tokens like "BoldRed". | ||
""" | ||
token_name = '' | ||
|
||
if bold: | ||
token_name += 'Bold' | ||
|
||
if fg_color: | ||
token_name += fg_color | ||
|
||
if bg_color: | ||
token_name += 'BG' + bg_color | ||
|
||
if token_name == '': | ||
return Text | ||
else: | ||
print(token_name) | ||
return getattr(Color, token_name) | ||
|
||
|
||
def callback(lexer, match): | ||
codes = match.group(1).split(';') | ||
|
||
for code in codes: | ||
try: | ||
code = int(code) | ||
except ValueError: | ||
# This shouldn't ever happen if we're given valid ANSI color codes, | ||
# but people can pastebin junk, and we should tolerate that. | ||
continue | ||
else: | ||
fg_color = _ansi_code_to_color.get(code - 30) | ||
bg_color = _ansi_code_to_color.get(code - 40) | ||
if fg_color: | ||
lexer.fg_color = fg_color | ||
elif bg_color: | ||
lexer.bg_color = bg_color | ||
elif code == 1: | ||
lexer.bold = True | ||
elif code == 22: | ||
lexer.bold = False | ||
elif code == 39: | ||
lexer.fg_color = None | ||
elif code == 49: | ||
lexer.bg_color = None | ||
elif code == 0: | ||
lexer.reset_state() | ||
|
||
yield match.start(), lexer.current_token(), match.group(2) | ||
|
||
|
||
class AnsiColorLexer(pygments.lexer.RegexLexer): | ||
name = 'ANSI Terminal' | ||
flags = re.DOTALL | re.MULTILINE | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.reset_state() | ||
|
||
def current_token(self): | ||
return token_from_lexer_state( | ||
self.bold, self.fg_color, self.bg_color, | ||
) | ||
|
||
def reset_state(self): | ||
self.bold = False | ||
self.fg_color = None | ||
self.bg_color = None | ||
|
||
tokens = { | ||
'root': [ | ||
(r'\x1b\[(.*?)m([^\x1b]*)', callback), | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
total 232K | ||
drwxr-xr-x 2 root root 4.0K Aug 8 14:53 [0m[01;34mbin[0m | ||
drwxr-xr-x 3 root root 4.0K Aug 8 14:53 [01;34mboot[0m | ||
drwxr-xr-x 16 root root 6.4K Aug 7 17:01 [01;34mdev[0m | ||
drwxr-xr-x 2 root root 4.0K Jul 3 15:17 [01;34mdist[0m | ||
drwxr-xr-x 2 root root 4.0K Jul 14 16:25 [01;34mdne[0m | ||
drwxr-xr-x 130 root root 12K Aug 10 13:35 [01;34metc[0m | ||
drwxr-xr-x 3 root root 4.0K Jul 14 16:26 [01;34mfoo[0m | ||
drwxr-xr-x 3 root root 4.0K Jun 22 16:59 [01;34mhome[0m | ||
lrwxrwxrwx 1 root root 30 Jun 30 16:40 [01;36minitrd.img[0m -> boot/initrd.img-4.4.0-1022-aws | ||
drwxr-xr-x 2 root root 4.0K Jul 3 15:17 [01;34mitest[0m | ||
drwxr-xr-x 18 root root 4.0K Jul 4 01:40 [01;34mlib[0m | ||
drwxr-xr-x 2 root root 4.0K Jul 5 08:40 [01;34mlib64[0m | ||
drwx------ 2 root root 16K Jun 27 2016 [01;34mlost+found[0m | ||
drwxr-xr-x 2 root root 4.0K Jun 27 2016 [01;34mmedia[0m | ||
drwxr-xr-x 2 root root 4.0K Jun 27 2016 [01;34mmnt[0m | ||
drwxr-xr-x 23 root root 4.0K Feb 4 2017 [01;34mnail[0m | ||
drwxr-xr-x 11 root root 4.0K Feb 6 2017 [01;34mopt[0m | ||
dr-xr-xr-x 2121 root root 0 Jul 3 10:22 [01;34mproc[0m | ||
drwx------ 5 root root 4.0K Aug 10 08:44 [01;34mroot[0m | ||
drwxr-xr-x 35 root root 1.3K Aug 10 13:41 [01;34mrun[0m | ||
drwxr-xr-x 2 root root 12K Aug 8 14:53 [01;34msbin[0m | ||
lrwxrwxrwx 1 root root 13 Feb 4 2017 [01;36mscratch[0m -> /nail/scratch | ||
drwxr-xr-x 2 root root 4.0K Jun 27 2016 [01;34msrv[0m | ||
dr-xr-xr-x 13 root root 0 Jul 7 22:17 [01;34msys[0m | ||
-rwxr-xr-x 1 ckuehl ckuehl 304 Nov 15 2015 [01;32mtest.py[0m | ||
drwxrwxrwt 2523 root root 120K Aug 10 13:47 [30;42mtmp[0m | ||
drwxr-xr-x 15 root root 4.0K Aug 2 16:40 [01;34musr[0m | ||
drwxr-xr-x 16 root root 4.0K Aug 3 13:25 [01;34mvar[0m | ||
lrwxrwxrwx 1 root root 27 Jun 30 16:40 [01;36mvmlinuz[0m -> boot/vmlinuz-4.4.0-1022-aws | ||
total 1.4G | ||
|
||
[1;30mhello world [1;31mhello world [1;32mhello world [1;33mhello world [1;34mhello world [1;35mhello world [1;36mhello world [1;37mhello world | ||
[22;30mhello world [22;31mhello world [22;32mhello world [22;33mhello world [22;34mhello world [22;35mhello world [22;36mhello world [22;37mhello world | ||
[1;40mhello world [1;41mhello world [1;42mhello world [1;43mhello world [1;44mhello world [1;45mhello world [1;46mhello world [1;47mhello world | ||
[22;40mhello world [22;41mhello world [22;42mhello world [22;43mhello world [22;44mhello world [22;45mhello world [22;46mhello world [22;47mhello world |