| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
""" |
| 4 |
A small utility to colorize variable names in Futhark output. |
| 5 |
""" |
| 6 |
|
| 7 |
import re |
| 8 |
from sys import stdin, argv |
| 9 |
|
| 10 |
# All Futhark-generated variables have this form |
| 11 |
var = re.compile(r'[\w_]+_\d+') |
| 12 |
|
| 13 |
|
| 14 |
if __name__ == "__main__": |
| 15 |
args = argv[1:] |
| 16 |
if len(args) == 0: |
| 17 |
files = [stdin] |
| 18 |
else: |
| 19 |
files = map(open, args) |
| 20 |
|
| 21 |
for f in files: |
| 22 |
content = f.read() |
| 23 |
all_vars = var.findall(content) |
| 24 |
all_vars = sorted(set(all_vars), key=all_vars.index) |
| 25 |
for i, v in enumerate(all_vars): |
| 26 |
color = (7*i) % 256 |
| 27 |
colored_v = "\033[38;5;{}m{}\033[0m".format(color, v) |
| 28 |
content = content.replace(v, colored_v) |
| 29 |
print(content) |