|
| 1 | + |
| 2 | +from bpython.formatter import BPythonFormatter |
| 3 | +from bpython._py3compat import PythonLexer |
| 4 | +from bpython.config import Struct, loadini, default_config_path |
| 5 | + |
| 6 | +from curtsies.termformatconstants import FG_COLORS, BG_COLORS, colors |
| 7 | +from curtsies.formatstring import fmtstr, FmtStr |
| 8 | + |
| 9 | +from pygments import format |
| 10 | +from functools import partial |
| 11 | + |
| 12 | +import re |
| 13 | + |
| 14 | +cnames = dict(list(zip('krgybmcwd', colors + ('default',)))) |
| 15 | + |
| 16 | +def func_for_letter(l, default='k'): |
| 17 | + """Returns FmtStr constructor for a bpython-style color code""" |
| 18 | + if l == 'd': |
| 19 | + l = default |
| 20 | + elif l == 'D': |
| 21 | + l = default.upper() |
| 22 | + return partial(fmtstr, fg=cnames[l.lower()], bold=(l.lower() != l)) |
| 23 | + |
| 24 | +def color_for_letter(l, default='k'): |
| 25 | + if l == 'd': |
| 26 | + l = default |
| 27 | + return cnames[l.lower()] |
| 28 | + |
| 29 | +def parse(s): |
| 30 | + """Returns a FmtStr object from a bpython-formatted colored string""" |
| 31 | + rest = s |
| 32 | + stuff = [] |
| 33 | + while True: |
| 34 | + if not rest: |
| 35 | + break |
| 36 | + d, rest = peel_off_string(rest) |
| 37 | + stuff.append(d) |
| 38 | + return (sum([fs_from_match(d) for d in stuff[1:]], fs_from_match(stuff[0])) |
| 39 | + if len(stuff) > 0 |
| 40 | + else FmtStr()) |
| 41 | + |
| 42 | +def fs_from_match(d): |
| 43 | + atts = {} |
| 44 | + if d['fg']: |
| 45 | + |
| 46 | + # this isn't according to spec as I understand it |
| 47 | + if d['fg'] != d['fg'].lower(): |
| 48 | + d['bold'] = True |
| 49 | + #TODO figure out why boldness isn't based on presence of \x02 |
| 50 | + |
| 51 | + color = cnames[d['fg'].lower()] |
| 52 | + if color != 'default': |
| 53 | + atts['fg'] = FG_COLORS[color] |
| 54 | + if d['bg']: |
| 55 | + if d['bg'] == 'I': |
| 56 | + color = colors[(colors.index(color) + (len(colors) // 2)) % len(colors)] # hack for finding the "inverse" |
| 57 | + else: |
| 58 | + color = cnames[d['bg'].lower()] |
| 59 | + if color != 'default': |
| 60 | + atts['bg'] = BG_COLORS[color] |
| 61 | + if d['bold']: |
| 62 | + atts['bold'] = True |
| 63 | + return fmtstr(d['string'], **atts) |
| 64 | + |
| 65 | +def peel_off_string(s): |
| 66 | + r""" |
| 67 | + >>> r = peel_off_string('\x01RI\x03]\x04asdf') |
| 68 | + >>> r == ({'bg': 'I', 'string': ']', 'fg': 'R', 'colormarker': '\x01RI', 'bold': ''}, 'asdf') |
| 69 | + True |
| 70 | + """ |
| 71 | + p = r"""(?P<colormarker>\x01 |
| 72 | + (?P<fg>[krgybmcwdKRGYBMCWD]?) |
| 73 | + (?P<bg>[krgybmcwdKRGYBMCWDI]?)?) |
| 74 | + (?P<bold>\x02?) |
| 75 | + \x03 |
| 76 | + (?P<string>[^\x04]*) |
| 77 | + \x04 |
| 78 | + (?P<rest>.*) |
| 79 | + """ |
| 80 | + m = re.match(p, s, re.VERBOSE | re.DOTALL) |
| 81 | + assert m, repr(s) |
| 82 | + d = m.groupdict() |
| 83 | + rest = d['rest'] |
| 84 | + del d['rest'] |
| 85 | + return d, rest |
| 86 | + |
| 87 | +def string_to_fmtstr(x): |
| 88 | + config = Struct() |
| 89 | + loadini(config, default_config_path()) |
| 90 | + return parse(format(PythonLexer().get_tokens(x), BPythonFormatter(config.color_scheme))) |
0 commit comments